如何使用水豚+无头铬测试测试确认/警报消息

时间:2018-11-27 10:46:16

标签: capybara alert confirm google-chrome-headless

最近,我将验收测试从capybara-webkit切换到了无头Chrome。在某些情况下,我需要检查警报消息(例如,确认在用户离开页面时放弃更改)。使用capybara-webkit,我可以通过以下方式完成

confirm_messages = page.driver.browser.confirm_messages
expect(confirm_messages.first).to include "Do you want to discard your changes?"

# or

expect(page.driver.browser.confirm_messages).to be_empty

现在,当我尝试获取带有chrome / headless chrome的确认消息时,出现以下错误:

undefined method `confirm_messages' for #<Selenium::WebDriver::Chrome::Driver:0x007fa5478d8a08> (NoMethodError)

如何使用水豚和无头铬测试警报?

1 个答案:

答案 0 :(得分:1)

您需要使用Capybaras模态处理方法(text / accept_confirm / etc)的accept_alert参数-https://www.rubydoc.info/github/jnicklas/capybara/Capybara/Session#accept_confirm-instance_method-在接受/之前会检查消息/取消系统模态

accept_confirm "Do you want to discard your changes?" do
  # whatever action triggers the modal to be shown
  click_link("Go somewhere else")
end

从技术上讲accept_confirm还会返回框的文本,因此您可以执行类似的操作

msg = accept_confirm do
  # action which triggers modal to be shown
end
expect(msg).to eq "Do you want to discard your changes?"

尽管如果您确切地知道消息的文本,则第一个示例的阅读效果更好。请注意,这也可以与capybara-webkit一起使用,而无需使用特定于驱动程序的方法。

相关问题