Capybara 和 Javascript onbefoureunload

发布于 2024-10-15 04:21:27 字数 584 浏览 3 评论 0原文

我有一个“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里的微风 2024-10-22 04:21:27

您可以使用 page.driver.browser.switch_to.alert.dismiss(此处的文档)摆脱警报。

你可以把它放在钩子后的黄瓜里

After "@javascript" do
  page.driver.browser.switch_to.alert.dismiss
  # or accept it if that is what you prefer
  page.driver.browser.switch_to.alert.accept
end

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

After "@javascript" do
  page.driver.browser.switch_to.alert.dismiss
  # or accept it if that is what you prefer
  page.driver.browser.switch_to.alert.accept
end
在巴黎塔顶看东京樱花 2024-10-22 04:21:27

对于任何使用 Rspec 和 Capybara 的人来说都有类似的答案。此代码在每次测试后都会访问“about:blank”。

context 'page that displays an alert when navigating away' do

  after(:each) do
    begin
      accept_confirm { visit('about:blank') }
    rescue Capybara::ModalNotFound
      # For examples that don't end with an alert
    end
  end

  ...
end

A similar answer for anyone using Rspec and Capybara. This code visits 'about:blank' after each test.

context 'page that displays an alert when navigating away' do

  after(:each) do
    begin
      accept_confirm { visit('about:blank') }
    rescue Capybara::ModalNotFound
      # For examples that don't end with an alert
    end
  end

  ...
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文