Last active
December 19, 2015 14:48
-
-
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.…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# snip | |
def webkit?() | |
[:webkit, :webkit_debug].include? Capybara.javascript_driver | |
end | |
# /snip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
capybara-webkit seems to be the outlier here, as some fields would trigger validation like
first name
, but other fields would not likeemail
. The above code seems to obtain consistency across browsers with capybara 2.2.0.rc1