public
Created

JS confirm dialog testing with Cucumber, Capybara, Rails 2 and 3

  • Download Gist
rails2_feature.feature
Cucumber
1 2 3
Then I expect to click "OK" on a confirmation box saying "Are you sure?"
And I press "Delete"
And the confirmation box should have been displayed
rails2_steps.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
When /^I expect to click "([^"]*)" on a confirmation box saying "([^"]*)"$/ do |option, message|
retval = (option == 'OK') ? 'true' : 'false'
page.evaluate_script("window.confirm = function (msg) {
document.cookie = msg
return #{retval}
}")
@expected_message = message.gsub("\\n", "\n")
end
 
When /^the confirmation box should have been displayed$/ do
assert page.evaluate_script('document.cookie').include?(@expected_message),
"Expected confirm box with message: '#{@expected_message}'" +
" got: '#{page.evaluate_script('document.cookie')}'"
end
rails3_feature.feature
Cucumber
1
Then "Delete" should have a confirmation saying "Are you sure?"
rails3_steps.rb
Ruby
1 2 3 4
When /^"([^\"]*)" should have a confirmation saying "([^\"]*)"$/ do |locator, message|
el = find_button(locator) || find_link(locator)
el['data-confirm'].should eql(message.gsub("\\n", "\n"))
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.