Take the 2-minute tour ×

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?

share|improve this question
If you are using Capybara-webkit, you will find your answer there : stackoverflow.com/questions/6930927/… –  Adrien Feb 18 at 15:42

8 Answers 8

up vote 53 down vote accepted

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.

share|improve this answer
awesome! thank you! –  yuval Apr 10 '10 at 0:56
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). –  carpeliam Apr 6 '11 at 17:34
See answer below for using "page.driver.browser.switch_to ..." –  Thorbjørn Hermansen Feb 24 '12 at 9:01

The selenium driver now supports this

From Capybara you would access it like this:





share|improve this answer
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 answer –  Peter Nixey Jul 31 '11 at 13:59
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! –  Derek Ekins Aug 1 '11 at 12:27
Ahh. Yes, good point and completely missed by me. In that case thank you for both examples. –  Peter Nixey Aug 1 '11 at 13:48
thank you, this works! –  deb Oct 26 '11 at 20:54

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

When /^I confirm popup$/ do

When /^I dismiss popup$/ do
share|improve this answer
sweet! it works! –  corroded Jun 1 '11 at 11:41

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

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}

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)")
share|improve this answer
Cool solution! I flipped it around a little which feels more natural to me: gist.github.com/727614 –  Mat Schaffer Dec 3 '10 at 21:48
And here's another version of that code which supports both alert and confirm boxes, gist.github.com/919116 –  Matthew O'Riordan Apr 14 '11 at 12:46

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

share|improve this answer

The capybara-webkit driver supports this as well.

share|improve this answer

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.

share|improve this answer
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)
  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)
   raise "Unsupported driver"
share|improve this answer

Your Answer


