RSpec capybara with Chromium

Viewed 1600

I'm trying to configure RSpec suite to run against Chromium browser (instead of google-chrome)... The test suite fails with the following error

     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

I've installed ChromeDriver and chromium browser (via brew cask install chromedriver chromium)...

here is the spec_helper.rb configurations for capybara

require "capybara/rails"
require "capybara/rspec"
require "capybara-screenshot/rspec"
require "selenium/webdriver"

# Capybara.default_driver = :selenium_chrome_headless
Capybara.default_driver = :selenium_chrome

This works perfectly when chrome is installed (same v.82 as chromium) but doesn't work with chromium... is there any way to get the selenium chrome driver to work with chromium?

2 Answers

Here's one way to do it:

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('/')

As you discovered, the Chrome/Chromium version needs to match the ChromeDriver version. This means that brew cask install chromium will probably not work because it will install the latest nightly build.

To get a working Chromium build, first check your ChromeDriver version:

$ chromedriver --version
ChromeDriver 81.0.4044.69 (6813546031a4bc83f717a2ef7cd4ac6ec1199132-refs/branch-heads/4044@{#776})

Next, you need to find a stable Chromium build with the same major version. I'd suggest grabbing a build from https://chromium.woolyss.com/ -- it will have a tag like:

81.0.4044.129 (737173) • Tuesday, 28 Apr 2020

If you know the build number (737173 in this example), you can also download a compatible snapshot from the Chromium build repository:

https://commondatastorage.googleapis.com/chromium-browser-snapshots/Mac/737173/chrome-mac.zip

The simplest way I found to get RSpec + Capybara working on Alpine Docker image:

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
Related