ruby – 如何使用无头铬与水豚和硒

前端之家收集整理的这篇文章主要介绍了ruby – 如何使用无头铬与水豚和硒前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Chrome版本:59.0.3071.104

使用Cucumber,Capybara,Selenium实现无头Chrome的自动化测试.

features/support/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

在进行黄瓜测试时,它说:

WARN Selenium [DEPRECATION] :args or :switches is deprecated. Use
Selenium::WebDriver::Chrome::Options#add_argument instead.

使用Headless Chrome的水豚和硒的正确方法是什么?谢谢.

编辑1:我使用Ruby-Binding,CapybaraWorking 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我收到一个错误.

Net::ReadTimeout (Net::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,desired_capabilities: caps
  )
end

Capybara.default_driver = :selenium_chrome

解决方法

1)确保你没有其他注册的驱动程序,我自己犯了这个错误并且有一个iphone驱动程序,它以旧方式使用args,这就是我收到警告的原因.

2)确保你在Linux上有Chrome版本57,在macOS上有59或在Windows上有60;

3)添加/更新gem selenium-webdriver;

4)将以下驱动程序添加到spec_helper.rb或rails_helper.rb:

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new app,options: Selenium::WebDriver::Chrome::Options.new(args: %w[headless disable-gpu])
end

Capybara.javascript_driver = :chrome
原文链接:https://www.f2er.com/ruby/270464.html

猜你在找的Ruby相关文章