Amit Kulkarni
Amit Kulkarni

Reputation: 481

Rspec + Capybara : How to click on alert box

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

Upvotes: 48

Views: 39754

Answers (6)

stujo
stujo

Reputation: 2109

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

page.accept_alert

Upvotes: 7

HectorPerez
HectorPerez

Reputation: 774

For WebKit:

page.accept_confirm { click_button "Upgrade" }

For Selenium:

page.driver.browser.switch_to.alert.accept

Upvotes: 17

B Seven
B Seven

Reputation: 45943

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.

Upvotes: 26

Jillian Foley
Jillian Foley

Reputation: 373

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

Upvotes: 35

chrisvanhill
chrisvanhill

Reputation: 217

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

Upvotes: 13

solnic
solnic

Reputation: 5743

You can click on an alert box like this:

page.driver.browser.switch_to.alert.accept

Upvotes: 38

Related Questions