如何在Rails 5.1中运行无头浏览器系统测试? [英] How can I run headless browser system tests in Rails 5.1?

查看:68
本文介绍了如何在Rails 5.1中运行无头浏览器系统测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关Rails 5.1系统测试的文档有点疏。我无法执行无人运行的测试来运行javascript。我认为一种方法需要安装和运行xvf。但这比我在其他版本的rails中运行水豚时要更加手动。

The documentation for Rails 5.1 system tests is a bit sparse. I'm unable to get headless tests that execute javascript running. I think one approach entails installing and running xvf. But this is more manual setup than I'm used to when running capybara in other versions of rails.

实现此目的最直接的方法是什么?

What's the most straightforward way to achieve this?

推荐答案

在Rails 5.1系统测试中,所使用的驱动程序由ApplicationSystemTestCase中的 driven_by 调用设置(测试/application_system_test_case.rb)。假设您已经将capybara-webkit驱动程序注册为 webkit,那么您应该能够做到

In Rails 5.1 system tests the driver used is set by the driven_by call in ApplicationSystemTestCase (test/application_system_test_case.rb). Assuming you have registered your capybara-webkit driver as 'webkit' you should be able to do

driven_by :webkit

如果您在Linux / Mac上使用Chrome 59+,另一个可能的选择是使用无头镀铬

Another potential option if you use Chrome 59+ on linux/mac is to use headless chrome

Capybara.register_driver :headless_chrome do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome, :args => ['headless'])
end

,然后在您的测试用例类中

and then in your test case class

driven_by :headless_chrome

这为您提供了无头的chrome版本,因此capybara-webkit / Poltergeist不支持当前的网络标准。不幸的是,当前chromedriver在JS系统模式方面存在问题(警报,确认,提示-水豚主分支中的解决方法),如果您在测试期间尝试关闭窗口,则挂起。希望这两个问题将很快得到解决。

That gives you a headless version of chrome so none of the issues of capybara-webkit/poltergeist not supporting current web standards. Unfortunately currently chromedriver has issues with JS system modals (alert, confirm, prompt - workaround in capybara master branch) and hangs if you attempt to close windows during your tests. Hopefully those 2 issues will be fixed soon.

还要注意,对于大多数人来说,rails 5.1应该已经消除了对database_cleaner的需求,因为它已经处理了单个数据库连接的共享。在测试模式下多个线程之间。

Also note that rails 5.1 should have removed the need for database_cleaner for most peoples testing since it already handles sharing of a single database connection between multiple threads in test mode.

这篇关于如何在Rails 5.1中运行无头浏览器系统测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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