Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

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

question from:https://stackoverflow.com/questions/6922241/rspec-capybara-how-to-click-on-alert-box

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can click on an alert box like this:

page.driver.browser.switch_to.alert.accept

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...