How to test a confirm dialog with Cucumber?
Asked Answered
A

10

86

I am using Ruby on Rails with Cucumber and Capybara.

How would I go about testing a simple confirm command ("Are you sure?")?

Also, where could I find further documentation on this issue?

Antonio answered 16/3, 2010 at 22:31 Comment(2)
If you are using Capybara-webkit, you will find your answer there : #6931427Retinue
Any one pls help with this: #77977993Capstone
G
62

Seems like there's no way to do it in Capybara, unfortunately. But if you're running your tests with the Selenium driver (and probably other drivers that support JavaScript), you can hack it. Just before performing the action that would bring up the confirm dialog, override the confirm method to always return true. That way the dialog will never be displayed, and your tests can continue as if the user had pressed the OK button. If you want to simulate the reverse, simply change it to return false.

page.evaluate_script('window.confirm = function() { return true; }')
page.click('Remove')
Gilud answered 9/4, 2010 at 16:44 Comment(2)
This doesn't seem to work anymore in Firefox 4... @derek-ekins solution below, from what Google tells me, seems to be more forward-compatible, though I can't confirm just yet (I'm stuck on Capybara 0.3.9).Andantino
See answer below for using "page.driver.browser.switch_to ..."Laterality
S
136

The selenium driver now supports this

From Capybara you would access it like this:

page.driver.browser.switch_to.alert.accept

or

page.driver.browser.switch_to.alert.dismiss

or

 page.driver.browser.switch_to.alert.text
Sowell answered 15/2, 2011 at 10:43 Comment(3)
For anyone else following this - please note that Derek's answer does indeed work where I found that the code in the official Selenium documentation did not (cucumber / Selenium). Note the presence of page.driver.browser in Derek's answerOccupational
Peter - the code here is tailored specifically for using capybara whereas the code in the docs are for when you are using selenium-webdriver directly - I wrote that example as well so I hope it works!Sowell
Ahh. Yes, good point and completely missed by me. In that case thank you for both examples.Occupational
G
62

Seems like there's no way to do it in Capybara, unfortunately. But if you're running your tests with the Selenium driver (and probably other drivers that support JavaScript), you can hack it. Just before performing the action that would bring up the confirm dialog, override the confirm method to always return true. That way the dialog will never be displayed, and your tests can continue as if the user had pressed the OK button. If you want to simulate the reverse, simply change it to return false.

page.evaluate_script('window.confirm = function() { return true; }')
page.click('Remove')
Gilud answered 9/4, 2010 at 16:44 Comment(2)
This doesn't seem to work anymore in Firefox 4... @derek-ekins solution below, from what Google tells me, seems to be more forward-compatible, though I can't confirm just yet (I'm stuck on Capybara 0.3.9).Andantino
See answer below for using "page.driver.browser.switch_to ..."Laterality
G
39

I've implemented these two web steps in /features/step_definitions/web_steps.rb:

When /^I confirm popup$/ do
  page.driver.browser.switch_to.alert.accept    
end

When /^I dismiss popup$/ do
  page.driver.browser.switch_to.alert.dismiss
end
Gerundive answered 12/5, 2011 at 9:46 Comment(0)
G
8

If you want to specifically test the message being displayed, here's a particularly hacky way to do so. I don't endorse it as beautiful code, but it gets the job done. You'll need to load http://plugins.jquery.com/node/1386/release, or change it to do cookies natively if you don't want jQuery.

Use this sort of story:

Given I am on the menu page for the current booking
And a confirmation box saying "The menu is £3.50 over budget. Click Ok to confirm anyway, or Cancel if you want to make changes." should pop up
And I want to click "Ok"
When I press "Confirm menu"
Then the confirmation box should have been displayed

And these steps

Given /^a confirmation box saying "([^"]*)" should pop up$/ do |message|
  @expected_message = message
end

Given /^I want to click "([^"]*)"$/ do |option|
  retval = (option == "Ok") ? "true" : "false"

  page.evaluate_script("window.confirm = function (msg) {
    $.cookie('confirm_message', msg)
    return #{retval}
  }")
end

Then /^the confirmation box should have been displayed$/ do
  page.evaluate_script("$.cookie('confirm_message')").should_not be_nil
  page.evaluate_script("$.cookie('confirm_message')").should eq(@expected_message)
  page.evaluate_script("$.cookie('confirm_message', null)")
end
Gid answered 15/11, 2010 at 17:5 Comment(2)
Cool solution! I flipped it around a little which feels more natural to me: gist.github.com/727614Peptic
And here's another version of that code which supports both alert and confirm boxes, gist.github.com/919116Tirzah
C
3

Updating this for current releases of Capybara. Most Capybara drivers today support the modal API. To accept a confirm modal you would do

accept_confirm do  # dismiss_confirm if not accepting
  click_link 'delete'  # whatever action triggers the modal to appear
end

This can be used in Cucumber with something like

When /^(?:|I )press "([^"]*)" and confirm "([^"]*)"$/ do |button, msg|
  accept_confirm msg do
    click_button(button)
  end
end

which will click the named button and then accept a confirm box with text matching msg

Cressida answered 26/1, 2016 at 9:45 Comment(0)
A
2

The capybara-webkit driver supports this as well.

Alpine answered 15/12, 2012 at 8:54 Comment(0)
C
2
Scenario: Illustrate an example has dialog confirm with text
    #     
    When I confirm the browser dialog with tile "Are you sure?"
    #
=====================================================================
my step definition here:

And(/^I confirm the browser dialog with title "([^"]*)"$/) do |title|
  if page.driver.class == Capybara::Selenium::Driver
    page.driver.browser.switch_to.alert.text.should eq(title)
    page.driver.browser.switch_to.alert.accept
  elsif page.driver.class == Capybara::Webkit::Driver
    sleep 1 # prevent test from failing by waiting for popup
    page.driver.browser.confirm_messages.should eq(title)
    page.driver.browser.accept_js_confirms
  else
   raise "Unsupported driver"
 end
end
Chipman answered 26/5, 2014 at 9:46 Comment(0)
G
1

Prickle adds some handy convenience methods for working with popups in selenium and webkit

Gaud answered 15/5, 2012 at 4:0 Comment(0)
A
0

This gist has steps to test a JS confirm dialog in Rails 2 and 3 with any Capybara driver.

It's an adaptation of a previous answer, but doesn't need the jQuery Cookie plugin.

Aekerly answered 27/9, 2011 at 14:58 Comment(0)
D
0

Tried the above answers with no luck. In the end this worked for me:

@browser.alert.ok
Daniels answered 16/9, 2015 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.