Skip to content

Instantly share code, notes, and snippets.

@rosskevin
Last active December 19, 2015 14:48
Show Gist options
  • Save rosskevin/5971508 to your computer and use it in GitHub Desktop.
Save rosskevin/5971508 to your computer and use it in GitHub Desktop.
Cucumber Capybara fill_in '' triggering client_side_validations. I fill in happy path values in the Background, and use Scenario Outline examples to create invalid submissions. This will properly trigger validations for both :selenium (firefox and chrome) and :webkit (capybara-webkit) drivers. Note that this is recently changed in capybara >= 2.…
When /^I fill in "([^"]*)" with "([^"]*)"$/ do |field, value|
# special - if ! ignore filling in
if value.eql? '!'
return
else
f = find_field(field)
current_value = f.value
# fill in unless it is already the same, then just leave it as-is
# to make sure we behave like a user would in a pre-populated scenario
fill_in(field, :with => value) unless value.eql? current_value
# fixed in capybara >= 2.2.0.rc1
## trigger the change event when clearing a field.
#if value.eql? ''
# trigger_event_for("##{f[:id]}", :change)
#end
# reliably trigger validation in webkit for client_side_validations
if webkit?
page.execute_script %Q{ $('form[data-validate]').validate() }
end
end
end
# snip
def webkit?()
[:webkit, :webkit_debug].include? Capybara.javascript_driver
end
# /snip
@rosskevin
Copy link
Author

capybara-webkit seems to be the outlier here, as some fields would trigger validation like first name, but other fields would not like email. The above code seems to obtain consistency across browsers with capybara 2.2.0.rc1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment