ruby capybara.rb(无头铬)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby capybara.rb(无头铬)相关的知识,希望对你有一定的参考价值。

# frozen_string_literal: true

require 'capybara/rails'
require 'selenium-webdriver'

def create_headless_chrome_driver(app, width: 1680, height: 1050)
  client = Selenium::WebDriver::Remote::Http::Default.new
  client.read_timeout = 10 # instead of the default 60

  # https://peter.sh/experiments/chromium-command-line-switches/
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    'goog:chromeOptions': { args: ['headless', "window-size=#{width},#{height}"] },
    loggingPrefs: { browser: 'ALL' },
  )
  Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: capabilities, http_client: client)
end

Capybara.register_driver :headless_chrome_small do |app|
  create_headless_chrome_driver(app, width:  375, height: 667)
end
Capybara.register_driver :headless_chrome_medium do |app|
  create_headless_chrome_driver(app, width:  960, height: 544)
end
Capybara.register_driver :headless_chrome_large do |app|
  create_headless_chrome_driver(app, width: 1024, height: 768)
end
Capybara.register_driver :headless_chrome_xlarge do |app|
  create_headless_chrome_driver(app, width: 1440, height: 900)
end

if ENV['CHROMEDRIVER_LOGLEVEL']
  Selenium::WebDriver.logger.level = ENV['CHROMEDRIVER_LOGLEVEL']
  Selenium::WebDriver.logger.output = '/tmp/test-results/chromedriver.log'
end

# FIXME
#  以下の設定は実際には効いていない(defaultの`selenium`が使われてしまう)。
#  仕方がないので各system specに
#  `before { Capybara.current_driver = :headless_chrome_xlarge }`
#  をいれて暫定対応とする。
Capybara.current_driver    = :headless_chrome_xlarge
Capybara.javascript_driver = :headless_chrome_xlarge

以上是关于ruby capybara.rb(无头铬)的主要内容,如果未能解决你的问题,请参考以下文章

ruby capybara.rb

ruby capybara_wait_until.rb

ruby capybara_wait_until.rb

强制无头铬/铬使用实际 gpu 而不是 Google SwiftShader

无头铬 - 性能

在无头模式下运行 Chrome