防止onbeforeunload对话框干扰测试 [英] Prevent onbeforeunload dialog from interfering with tests

查看:48
本文介绍了防止onbeforeunload对话框干扰测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在Capybara启动的Chrome实例中禁用从onbeforeunload生成的对话框.我的测试套件非常大,修改每次访问/刷新以包含代码以单击离开"确认是不切实际的.

I am looking to disable the dialog that spawns from onbeforeunload in the instance of Chrome spun up by Capybara. I have a very large test suite and it would be impractical to modify every visit/refresh to include code to click the ‘Leave’ confirmation.

我曾尝试在访问和刷新之前添加 page.execute_script'window.onbeforeunload ='undefined;',尽管这样做有效,但它也存在必须修改所有访问和刷新调用的问题.

I have tried adding page.execute_script ‘window.onbeforeunload = undefined;’ before visits and refreshes, and while this works, it shares the problem of having to modify all my visit and refresh calls.

我还检查了是否可以添加任何Chrome CLI选项来禁用此功能,但没有发现任何相关信息.我在这里检查过: https://peter.sh/experiments/chromium-command-line-switches/,但是在搜索卸载,警报和对话框后,似乎没有任何匹配项.

I also checked to see if there are any Chrome CLI options I could add to disable this functionality, but I didn’t find anything relevant. I checked here: https://peter.sh/experiments/chromium-command-line-switches/ but after searching for unload, alert, and dialog, nothing that matched seemed relevant.

我使用的是Chromedriver v2.33,Chrome 62,Capybara 2.5和Selenium-webdriver 2.53.4.如果需要,将用更多信息更新此帖子.

I am on Chromedriver v2.33, Chrome 62, Capybara 2.5 and Selenium-webdriver 2.53.4. Will update this post with more info if needed.

这可能吗?

推荐答案

要使Capybaras进行 visit refresh 方法,请始终先调用 execute_script 最简单的解决方案是

To make Capybaras visit and refresh methods always call your execute_script first the easiest solution would be something like

module BeforeUnloadDisabler
  def visit(*args)
    execute_script 'window.onbeforeunload = undefined;'
    super
  end

  def refresh(*args)
    execute_script 'window.onbeforeunload = undefined;'
    super
  end
end

::Capybara::Session.prepend(BeforeUnloadDisabler)

尽管,如果您的应用正在注册卸载警报,那么您可能想重新考虑自己的设计选择,这对于用户来说确实很烦人.

Although, if your app is registering beforeunload alerts that much you might want to reconsider your design choices -- that can be really annoying for users.

这篇关于防止onbeforeunload对话框干扰测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆