本文介绍了Rspec + Capybara:如何点击警报框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了水豚的帖子+点击警报框,但似乎没有任何效果.以下是我的场景:

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. 我正在编写请求规范,即使用 rspec 和水豚.主要问题是点击警告框:以下是我的代码:

  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:

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

  • 对于第二个例子,即

  • For the second example i.e.

    page.driver.browser.switch_to.alert.accept

    我的代码:

     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
    

  • 我收到错误:

    失败/错误:page.driver.browser.switch_to.alert.accept Selenium::WebDriver::Error::UnhandledError:

    请让我知道如何进一步

    推荐答案

    您可以像这样点击警告框:

    You can click on an alert box like this:

    page.driver.browser.switch_to.alert.accept
    

    这篇关于Rspec + Capybara:如何点击警报框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-30 23:32