Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I confirm a javascript popup with Capybara?

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 
like image 866
Eric M. Avatar asked Aug 03 '11 17:08

Eric M.


2 Answers

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 
like image 99
Lee Dykes Avatar answered Sep 20 '22 23:09

Lee Dykes


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

like image 24
Peter Nixey Avatar answered Sep 21 '22 23:09

Peter Nixey