How to confirm javascript popup with Capybara?

I tried a few examples found on the internet but no luck. I want to confirm the deletion link confirmation message. The last attempt was the code below, but this led to a Capybara :: NotSupportedByDriverError error.

def confirm_dialog page.evaluate_script('window.confirm = function() { return true; }') end 
+55
ruby-on-rails testing capybara
Aug 03 2018-11-18T00:
source share
8 answers

First of all, switch to using Selenium as a driver by placing the @javascript tag in front of your script.

The following code at the cucumber step will then confirm the dialog:

 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 was asked and answered before this: How to check the confirmation dialog with Cucumber? .

Additional selenium documentation is also available: http://code.google.com/p/selenium/wiki/RubyBindings#JavaScript_dialogs

+47
Aug 03 '11 at 18:29
source share

Adding an answer for those who do this in 2016 onwards. Now you can use Capybara to accept the confirmation field. You do this by wrapping code that calls up the confirmation window displayed in accept_confirm .

 accept_confirm do click_link 'Destroy' end 
+20
Jul 20 '16 at 18:50
source share

for capybara-webkit:

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

which still works, but the documentation also says:

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

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

+14
Apr 20 '13 at 15:13
source share

I had to use hibernation in the webkit test, as it failed each and then otherwise.

Here is what I came up with after reading all kinds of 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 
+6
Aug 01 '13 at
source share

I had problems synchronizing with browser dialogs in the CI environment, so I have to poll the 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 
+6
Sep 11 '13 at 12:26
source share

I would suggest that you should add selenium to your gem file and configure it and capybara, that capybara uses selenium as a driver.

I also think that How to check confirmation dialog with Cucumber? very similar to your question, especially the accepted answer.

+2
Aug 03 '11 at 18:05
source share

try adding :js => true to your test.

RSpecs metadata function can be used to switch to another driver. Use: js => true to switch to the javascript driver or: driver to switch to one specific driver. For example:

 it 'will use the default js driver' :js => true do ... end 
+2
Aug 03 '11 at 18:08
source share

In Capybara, it is very simple to accept a model window. Even we can do the same in selenium, but its a little difficult for people who do not know about selenium.

page.accept_modal # This will take a modal window

page.dismiss_modal # This will reject / reject the modal window

+2
Apr 04 '17 at 13:22
source share



All Articles