R规格+ Capybara:如何单击警报框
我已经浏览了水豚的帖子+单击警报框,但似乎没有任何效果。以下是我的场景:
场景:当我单击更新按钮时,会出现一个警告框,其中包含“确定”和“取消”按钮。单击“确定”,然后出现新表格。
我正在编写请求规范,即使用 rspec 和 capybara。主要问题是单击警报框:以下是我的代码:
上下文“更新”执行 在(:全部)执行之前 水豚.current_driver = :selenium 结尾 之后(:全部)做 水豚.use_default_driver 结尾 它“将用户更新为培训师” click_button('搜索') 睡觉 3 page.evaluate_script('data-confirm = function() { return true; }') page.click('确定') click_button('升级') 结尾 结尾
但是执行脚本会出现以下错误:
<块引用>失败/错误:page.evaluate_script('data-confirm = function() { return true; }') Selenium::WebDriver::Error::UnexpectedJavascriptError: 无效赋值左侧 # ./spec/requests/ user_upgrades_spec.rb:30
对于第二个例子,即
<块引用>page.driver.browser.switch_to.alert.accept
我的代码:
上下文“更新”执行 在(:全部)执行之前 水豚.current_driver = :selenium 结尾 之后(:全部)做 水豚.use_default_driver 结尾 它“将用户更新为培训师” click_button('搜索') 睡觉 3 click_button('升级') page.driver.browser.switch_to.alert.accept 结尾 结尾
我收到错误:
失败/错误:page.driver.browser.switch_to.alert.accept Selenium::WebDriver::Error::UnhandledError:
请让我知道如何进一步进行
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.
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以单击这样的警报框:
You can click on an alert box like this:
在这里更新了答案,因为上面的选项似乎都已被弃用。
Capybara::Session#accept_alert 似乎是现在实现此目的的最佳方法,其中触发警报的操作应在块中传递。
http://www.rubydoc.info/github/jnicklas/capybara/Capybara/Session:accept_alert< /a>
例如:
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.:
使用 Selenium 为我工作。其他驱动程序可能有自己的语法。
正如 Jillian Foley 提到的,其他答案似乎已被弃用。
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.
对于 WebKit:
对于 Selenium:
For WebKit:
For Selenium:
如果您想单击警报框的“确定”按钮,请尝试此行:
不要忘记使用 javascript 标记标记您的测试
,并在您的 spec_helper 文件中使用 :webkit 或 :selenium 启用 Capybara.javascript_driver
Try this line if you want to click on the ok button of the alert box:
Don't forget to mark your test with the javascript flag
and enable Capybara.javascript_driver with :webkit or :selenium in your spec_helper file
我知道这已经很旧了,但现在也适用于吵闹鬼:
I know this is old but this now works in poltergeist too: