铬版本:59.0.3071.104
使用 Cucumber、Capybara、Selenium 通过 Headless Chrome 实现自动化测试。
功能/支持/env.rb
require 'rubygems'
require 'capybara/cucumber'
Capybara.register_driver :selenium_chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome, args: ['headless'])
end
Capybara.default_driver = :selenium_chrome
运行黄瓜测试时,它说:
警告 Selenium [DEPRECATION] :args 或 :switches 已弃用。请改用 Selenium::WebDriver::Chrome::Options#add_argument。
将 capybara 和 selenium 与 Headless Chrome 一起使用的正确方法是什么。谢谢你。
编辑 1:我使用Ruby-Binding、Capybara和Working Example尝试了以下操作。但它也给出了错误。
require 'rubygems'
require 'capybara/cucumber'
require 'selenium-webdriver'
Capybara.register_driver :selenium_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
driver = Selenium::WebDriver.for :chrome, options: options
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: driver
)
end
Capybara.default_driver = :selenium_chrome
错误:
> undefined method `each' for
> #<Selenium::WebDriver::Remote::Capabilities:0xd13baac> (NoMethodError)
编辑 2:下载最新的ChromeDriver 2.30
使用ChromeDriver Capabilities & Chrome Command Line Switches中的示例我得到一个错误。
网络::ReadTimeout (网络::ReadTimeout)
require 'rubygems'
require 'capybara/cucumber'
require 'selenium-webdriver'
Capybara.register_driver :selenium_chrome do |app|
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
"chromeOptions" => {
"binary" => "/chromedriver_win32/chromedriver.exe",
"args" => [ "--disable-web-security", "--headless" ]
}
)
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: caps
)
end
Capybara.default_driver = :selenium_chrome