Rspec + Capybara : How to click on alert box
Asked Answered
C

6

47

I have gone through the post of capybara + click on alert box but nothing seems to be work. Following is my scenario:

Scenario: When I click update button An alert box appears which contains "OK" and "Cancel" button. Click on "Ok" then new form appears.

  1. I am writing request specs i.e. using rspec and capybara. Main problem is to click on the alert box: Following is my code:

     context "update" do
       before(:all) do
         Capybara.current_driver = :selenium
       end
       after(:all) do
         Capybara.use_default_driver
       end
    
       it "update user to trainer" do
         click_button('Search')
         sleep 3 
         page.evaluate_script('data-confirm = function() { return true; }')
         page.click('OK')      
         click_button('Upgrade')
       end
     end
    

    But executing script gives me following error:

    Failure/Error: page.evaluate_script('data-confirm = function() { return true; }') Selenium::WebDriver::Error::UnexpectedJavascriptError: invalid assignment left-hand side # ./spec/requests/user_upgrades_spec.rb:30

  2. For the second example i.e.

    page.driver.browser.switch_to.alert.accept

    My code :

     context "update" do
       before(:all) do
         Capybara.current_driver = :selenium
       end
       after(:all) do
         Capybara.use_default_driver
       end
       it "update user to trainer" do
         click_button('Search')
         sleep 3   
         click_button('Upgrade') 
         page.driver.browser.switch_to.alert.accept
       end
     end 
    

I get error:

Failure/Error: page.driver.browser.switch_to.alert.accept Selenium::WebDriver::Error::UnhandledError:

Please let me know how to proceed further

Cumshaw answered 3/8, 2011 at 5:54 Comment(2)
You can't name a JavaScript variable with dashes. data-confirm = function ... is illegal. It looks like (foo - bar) = function() { ... }. That's the left-hand side # ...user_upgrades_spec.rb:30 error. Name your JavaScript variables with camelCase.Endocentric
#27384540Rigel
O
38

You can click on an alert box like this:

page.driver.browser.switch_to.alert.accept
Overrun answered 3/8, 2011 at 9:30 Comment(9)
Thanks a lot for the feedback.but i had already used the above code but no success.Cumshaw
Not working for me either; plus, I believe the question addresses this method explicitly ( see last error message listed ). So the answer seems to suggest using what the question already states does not work for Amit in his current set up. Considering the context of the question, I have to down vote. Sorry >.<Shelf
Works fine for me using capybara (1.1.2).Viscus
This is NOT working with Capybara-webkit, just like for the OPVelours
Worked for me capybara 1.1.4. Selenium driverTestosterone
For capybara-webkit: expect(page.driver.alert_messages.last).to eq textKurdistan
See @JillianFoley's answer below. Comes straight out of the current Capybara README and works great.Therron
This doesn't work. Both firefox and chrome under selenium pop out extra alert boxes with a checkbox of 'prevent this dialog from opening new dialogs' ie. UnhandledAlertErrorAirspeed
Worked for capybara-3.4.2, selenium driverDorotea
S
35

Updated answer here, since the options above seem to have all been deprecated.

Capybara::Session#accept_alert seems to be the best way to accomplish this now, where the action that will trigger the alert should be passed in a block. http://www.rubydoc.info/github/jnicklas/capybara/Capybara/Session:accept_alert

e.g.:

page.accept_alert 'Alert text here' do
    click_button('Search')
end
Substitution answered 20/10, 2014 at 18:29 Comment(2)
I'm getting an undefined method accept_alert' for #<Capybara::Session>` even though per the docs, this is a method for Capybara::Session. Any thoughts?Ensilage
Could be an older version of Capybara, accept_alert was introduced in Capybara 2.4.1. Consider running bundle update capybaraRicci
E
26
page.accept_alert

worked for me using Selenium. Other drivers will probably have their own syntax.

As Jillian Foley mentioned, it seems that other answers have been deprecated.

Empennage answered 6/12, 2014 at 2:40 Comment(2)
As @b-seven mentioned other drivers, at the time of writing, the Poltergeist driver for PhantomJS doesn't yet support accept_alert.Ricci
Great! Worked fine for me! @B SevenTorruella
B
16

For WebKit:

page.accept_confirm { click_button "Upgrade" }

For Selenium:

page.driver.browser.switch_to.alert.accept
Biggers answered 3/11, 2015 at 12:3 Comment(0)
M
13

Try this line if you want to click on the ok button of the alert box:

page.evaluate_script('window.confirm = function() { return true; }')

Don't forget to mark your test with the javascript flag

it "update user to trainer", js: true do
    ...
end

and enable Capybara.javascript_driver with :webkit or :selenium in your spec_helper file

Moustache answered 27/12, 2011 at 20:35 Comment(2)
Works with Capybara-Webkit :)Manuscript
Of all the answers, this is the only one that worked for me.Bentwood
C
7

I know this is old but this now works in poltergeist too:

page.accept_alert
Conk answered 13/6, 2017 at 1:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.