Capybara 和 Javascript onbefoureunload
我有一个“ask_a_question”页面,其中包含一个 onbeforeunload 函数,可以在未保存某些内容之前提醒用户(嘿!就像这样:P)。我正在使用带有水豚和 webdriver 的黄瓜来测试它,添加 @javascript 标签,因为它使用了大量的 javascript。黄瓜功能可能如下所示:
@javascript
Scenario: add a question
Given I login as "Mauricio"
And I go to the create question page
Then I should see "Ask a Question" within "header"
但是一旦测试通过,Capybara(或 WebDriver,我不知道)会尝试重用同一浏览器窗口进行其他测试,然后显示 onbeforeunload 警报,从而搞乱以下测试。
因为我的功能不是关闭或退出页面本身。我认为添加一些内容来接受警报可能不是个好主意。但老实说我很迷失。
如何告诉水豚为每个 @javascript 测试使用新的浏览器窗口或自动关闭 onbeforeunload 警报?
谢谢
I have an 'ask_a_question' page with an onbeforeunload function to alert the user before leaving something unsaved (hey! like SO :P). I'm testing it using cucumber with capybara and webdriver, adding the @javascript tag because it uses a lot of javascript. A cucumber feature might look like this:
@javascript
Scenario: add a question
Given I login as "Mauricio"
And I go to the create question page
Then I should see "Ask a Question" within "header"
But once the test pases, Capybara (or WebDriver, I dunno) tries to reuse the same browser window for other tests then the onbeforeunload alert is displayed screwing the following test.
As my feature is not closing or exiting the page per se. I don't think it might be good idea to add something to accept the alert. But honestly I'm quite lost.
How can I tell capybara to use a new browser window for each @javascript test or automatically close the onbeforeunload alert?
Thanx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 page.driver.browser.switch_to.alert.dismiss(此处的文档)摆脱警报。
你可以把它放在钩子后的黄瓜里
you can use page.driver.browser.switch_to.alert.dismiss (docs here) to get rid of the alert.
You could put this in a cucumber after hook
对于任何使用 Rspec 和 Capybara 的人来说都有类似的答案。此代码在每次测试后都会访问“about:blank”。
A similar answer for anyone using Rspec and Capybara. This code visits 'about:blank' after each test.