Rspec + Capybara : How to click on alert box

后端 未结 6 1259
梦谈多话
梦谈多话 2020-12-29 19:15

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

相关标签:
6条回答
  • 2020-12-29 19:26

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

    page.accept_alert
    
    0 讨论(0)
  • 2020-12-29 19:29

    You can click on an alert box like this:

    page.driver.browser.switch_to.alert.accept
    
    0 讨论(0)
  • 2020-12-29 19:33

    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
    
    0 讨论(0)
  • 2020-12-29 19:33
    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.

    0 讨论(0)
  • 2020-12-29 19:34

    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

    0 讨论(0)
  • 2020-12-29 19:39

    For WebKit:

    page.accept_confirm { click_button "Upgrade" }
    

    For Selenium:

    page.driver.browser.switch_to.alert.accept
    
    0 讨论(0)
提交回复
热议问题