如何在 rails 5.1 中为 chrome headless 系统测试设置屏幕尺寸?
How to set screen size for chrome headless system test in rails 5.1?
为了使用和测试我网站的新响应式前端,我正在尝试使用 Rails 的新系统测试(规范)以及 javascript 和 Chrome无头。不过,我无法在规范中找到设置浏览器屏幕大小的方法。
这是我在 spec/rails_helper.rb
中的设置
config.before(:each, type: :system, js: true) do
driven_by :selenium_chrome_headless, screen_size: [1900, 800]
end
然后我创建屏幕截图:
page.driver.save_screenshot(the_uri)
但是渲染截图的尺寸还是默认的,小了很多。理想情况下,我希望看到整个呈现的页面,但在这一点上,我很乐意只使用我提供的尺寸。
关于我在这里缺少什么的想法?
我今天刚 运行 进入这个,我认为答案是......你不能 :(
来自文档:
driven_by has a required argument for the driver name. The keyword arguments are :using for the browser and :screen_size to change the size of the browser screen. These two options are not applicable for headless drivers and will be silently ignored if passed.
太可惜了。测试响应式网站几乎是不可能的,因为某些元素会根据屏幕宽度显示和隐藏。
您只需重新定义传递无头和屏幕尺寸参数的驱动程序。
Capybara.register_driver :selenium_chrome_headless do |app|
options = Selenium::WebDriver::Chrome::Options.new
[
"headless",
"window-size=1280x1280",
"disable-gpu" # https://developers.google.com/web/updates/2017/04/headless-chrome
].each { |arg| options.add_argument(arg) }
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
RSpec.configure do |config|
config.before(:each, type: :system, js: true) do
driven_by :selenium_chrome_headless
end
end
有同样的问题。
更改了这一行
driven_by :selenium_chrome_headless, screen_size: [1400, 1400]
至此
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
解决问题。
为了使用和测试我网站的新响应式前端,我正在尝试使用 Rails 的新系统测试(规范)以及 javascript 和 Chrome无头。不过,我无法在规范中找到设置浏览器屏幕大小的方法。
这是我在 spec/rails_helper.rb
config.before(:each, type: :system, js: true) do
driven_by :selenium_chrome_headless, screen_size: [1900, 800]
end
然后我创建屏幕截图:
page.driver.save_screenshot(the_uri)
但是渲染截图的尺寸还是默认的,小了很多。理想情况下,我希望看到整个呈现的页面,但在这一点上,我很乐意只使用我提供的尺寸。
关于我在这里缺少什么的想法?
我今天刚 运行 进入这个,我认为答案是......你不能 :(
来自文档:
driven_by has a required argument for the driver name. The keyword arguments are :using for the browser and :screen_size to change the size of the browser screen. These two options are not applicable for headless drivers and will be silently ignored if passed.
太可惜了。测试响应式网站几乎是不可能的,因为某些元素会根据屏幕宽度显示和隐藏。
您只需重新定义传递无头和屏幕尺寸参数的驱动程序。
Capybara.register_driver :selenium_chrome_headless do |app|
options = Selenium::WebDriver::Chrome::Options.new
[
"headless",
"window-size=1280x1280",
"disable-gpu" # https://developers.google.com/web/updates/2017/04/headless-chrome
].each { |arg| options.add_argument(arg) }
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
RSpec.configure do |config|
config.before(:each, type: :system, js: true) do
driven_by :selenium_chrome_headless
end
end
有同样的问题。
更改了这一行
driven_by :selenium_chrome_headless, screen_size: [1400, 1400]
至此
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
解决问题。