81

I've tried several examples found online, but with no luck. I am looking to confirm the confirm message of a delete link. The last attempt was the code below, but that resulted in an Capybara::NotSupportedByDriverError error.

def confirm_dialog
  page.evaluate_script('window.confirm = function() { return true; }')
end
Eric M.
  • 5,399
  • 6
  • 41
  • 67
  • possible duplicate of [How to test a confirm dialog with Cucumber?](http://stackoverflow.com/questions/2458632/how-to-test-a-confirm-dialog-with-cucumber). The accepted answer's author agrees with duplicate. – Ciro Santilli OurBigBook.com Feb 08 '14 at 09:00
  • See my answer here for the newer way to do this: http://stackoverflow.com/a/26472467/2170577 – Jillian Foley Oct 20 '14 at 18:35

8 Answers8

85

Adding an answer for those hitting this in 2016 and beyond. You can now use Capybara directly to accept a confirmation box. You do this by wrapping the code that causes the confirmation box to appear in the accept_confirm function.

accept_confirm do
  click_link 'Destroy'
end
Lee Dykes
  • 1,017
  • 8
  • 16
  • In addition to wrapping your action in a block like this, for me simply calling `accept_confirm` after the action which caused the confirmation dialog also successfully proceeded past it. – Bo Jeanes Jun 08 '21 at 06:43
57

First of all switch to using Selenium as the driver by putting an @javascript tag in front of your scenario.

The following code in your cucumber step will then confirm the dialogue:

page.driver.browser.switch_to.alert.accept
# or
page.driver.browser.switch_to.alert.dismiss
# or
page.driver.browser.switch_to.alert.text

As @NobbZ said, this question has been asked and answered before here: How to test a confirm dialog with Cucumber?.

More selenium documentation available here too: http://code.google.com/p/selenium/wiki/RubyBindings#JavaScript_dialogs

Community
  • 1
  • 1
Peter Nixey
  • 16,187
  • 14
  • 79
  • 133
  • 4
    undefined method `switch_to' for # – Dima Mar 28 '13 at 13:23
  • 1
    I got that undefined method error and then discovered that I didn't need the switch_to call at all because selenium (I think) is automatically returning true on any confirms. That won't help everyone, but might help some. – elc Sep 24 '13 at 19:17
  • 1
    @DimaGoltsman You're using RackTest. the answer refers to the selenium driver. You need to set the driver before the test is run by ``Capybara.current_driver = :selenium`` or else add js: true to the example which should set the driver to ``:selenium`` by default as it is the default ``Capybara.javascript_driver`` – iNulty Nov 21 '13 at 11:41
  • Works with Capybara using selenium driver in Rails. Thanks – luigi7up Sep 29 '14 at 22:16
  • I tried to use `page.driver.browser.switch_to` but got a DEPRECATION warning but was brought to this: http://stackoverflow.com/questions/26275359/deprecation-warning-from-capybara – echo Oct 06 '15 at 22:28
16

for capybara-webkit:

page.driver.browser.accept_js_confirms
page.driver.browser.reject_js_confirms

which is still working, but the documentation says also:

page.driver.accept_js_confirms!
page.driver.accept_js_confirms!

See https://github.com/thoughtbot/capybara-webkit , search "accept_js_confirms"

Lars Schirrmeister
  • 2,215
  • 1
  • 22
  • 23
10

I've had timing issues with browser dialogs in a CI environment so I'm polling for a dialog before accepting it:

def accept_browser_dialog
  wait = Selenium::WebDriver::Wait.new(:timeout => 30)
  wait.until {
    begin
      page.driver.browser.switch_to.alert
      true
    rescue Selenium::WebDriver::Error::NoAlertPresentError
      false
    end
  }
  page.driver.browser.switch_to.alert.accept
end
seidtgeist
  • 101
  • 1
  • 3
6

I had to use a sleep in the webkit test since it would fail everynow and then otherwise.

Here is what I came up with after reading everyones posts:

if page.driver.class == Capybara::Selenium::Driver
  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.accept_js_confirms
else
  raise "Unsupported driver"
end
Michael Yagudaev
  • 6,049
  • 3
  • 48
  • 53
  • Yep, just be really careful not to add too many of those, it makes your tests really expensive to run (i.e. they will take longer because of the sleeps) – Michael Yagudaev Aug 18 '13 at 21:10
3

In Capybara its very simple to accept the model window. Even we can do the same in selenium but its little tough for people who are not aware about selenium.

page.accept_modal #This will accept the modal window

page.dismiss_modal #This will Reject/Dismiss the modal window

Jagan
  • 79
  • 3
3

try to add :js => true to your test.

RSpec’s metadata feature can be used to switch to a different driver. Use :js => true to switch to the javascript driver, or provide a :driver option to switch to one specific driver. For example:

it 'will use the default js driver' :js => true do
  ...
end
Vasiliy Ermolovich
  • 24,459
  • 5
  • 79
  • 77
2

I would guess that you have to add selenium to your gem-file and configure it and capybara that capybara uses selenium as the driver.

I think also that How to test a confirm dialog with Cucumber? is very similar to your question, especially the accepted answer.

Community
  • 1
  • 1
NobbZ
  • 1,360
  • 3
  • 15
  • 30