我正在尝试将RSpec套件配置为在Chromium
浏览器(而不是google-chrome
(上运行。。。测试套件失败,出现以下错误
1.2) Failure/Error: Unable to infer file and line number from backtrace
Selenium::WebDriver::Error::SessionNotCreatedError:
session not created: This version of ChromeDriver only supports Chrome version 80
# 0 chromedriver 0x0000000103190329 chromedriver + 3838761
# 1 chromedriver 0x000000010312a123 chromedriver + 3420451
# yada yada
我已经安装了ChromeDriver
和chromium浏览器(通过brew cask install chromedriver chromium
(。。。
这是水豚的spec_helper.rb
配置
require "capybara/rails"
require "capybara/rspec"
require "capybara-screenshot/rspec"
require "selenium/webdriver"
# Capybara.default_driver = :selenium_chrome_headless
Capybara.default_driver = :selenium_chrome
当安装铬(与铬相同的v.82
(时,这非常有效,但不适用于铬。。。有什么方法可以让硒铬驱动器与铬一起工作吗?
这里有一种方法:
require 'capybara'
require 'capybara/dsl'
require 'selenium-webdriver'
include Capybara::DSL
Capybara.register_driver :selenium_chromium do |app|
options = Selenium::WebDriver::Chrome::Options.new(binary: "/Applications/Chromium.app/Contents/MacOS/Chromium")
Capybara::Selenium::Driver.new(app, :browser => :chrome, options: options)
end
Capybara.current_driver = :selenium_chromium
Capybara.app_host = 'https://example.org'
visit('/')
正如您所发现的,Chrome/Chrome版本需要与ChromeDriver版本相匹配。这意味着brew cask install chromium
可能无法工作,因为它将安装最新的夜间版本。
要获得一个有效的Chromium版本,请首先检查您的ChromeDriver版本:
$ chromedriver --version
ChromeDriver 81.0.4044.69 (6813546031a4bc83f717a2ef7cd4ac6ec1199132-refs/branch-heads/4044@{#776})
接下来,您需要找到一个具有相同主要版本的稳定Chromium构建。我建议从https://chromium.woolyss.com/--它将有一个类似于的标签
81.0.4044.129(737173(•2020年4月28日,星期二
如果您知道内部版本号(本例中为737173(,您还可以从Chromium构建存储库下载兼容的快照:
https://commondatastorage.googleapis.com/chromium-browser-snapshots/Mac/737173/chrome-mac.zip
我发现让RSpec+Capybara在Alpine Docker图像上工作的最简单方法:
Dockerfile
FROM ruby:3.0.2-alpine
RUN apk --update add --no-cache chromium chromium-chromedriver
Gemfile
group :test do
gem 'capybara' # 3.36.0 as of writing this
gem 'selenium-webdriver' # 4.0.3 as of writing this
# Make sure no other gems are installed, such as `webdrivers`
end
rails_helper.rb
Capybara.register_driver :headless_chromium do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--window-size=1280,900")
Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: options)
end
Capybara.javascript_driver = :headless_chromium