How do I confirm a javascript popup with Capybara?

Ruby on-RailsTestingCapybara

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

[1]: http://www.rubydoc.info/github/jnicklas/capybara/Capybara%2FSession%3Aaccept_confirm "accept_conform"

Solution 2 - Ruby on-Rails

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: https://stackoverflow.com/questions/2458632/how-to-test-a-confirm-dialog-with-cucumber/5002648#comment-8214000.

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

Solution 3 - Ruby on-Rails

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"

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

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

Solution 7 - Ruby on-Rails

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.

Solution 8 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEric M.View Question on Stackoverflow
Solution 1 - Ruby on-RailsLee DykesView Answer on Stackoverflow
Solution 2 - Ruby on-RailsPeter NixeyView Answer on Stackoverflow
Solution 3 - Ruby on-Railsuser1083709View Answer on Stackoverflow
Solution 4 - Ruby on-RailsseidtgeistView Answer on Stackoverflow
Solution 5 - Ruby on-RailsMichael YagudaevView Answer on Stackoverflow
Solution 6 - Ruby on-RailsJaganView Answer on Stackoverflow
Solution 7 - Ruby on-RailsNobbZView Answer on Stackoverflow
Solution 8 - Ruby on-RailsVasiliy ErmolovichView Answer on Stackoverflow