There are three things necessary to get this working:
- Use selenium-webdriver instead of webdrivers
- Install chromium, chromium-chromedriver and selenium on your Docker image
- Configure a Capybara driver
Details below.
Use selenium-webdriver instead of webdrivers
If you happen to be using the webdrivers gem in your Gemfile
, replace it with selenium-webdriver
.
Install chromium, chromium-chromedriver and selenium on your Docker image
Alter your Dockerfile
so that the following packages are being installed:
chromium chromium-chromedriver python3 python3-dev py3-pip
Then, after that, include the line RUN pip3 install -U selenium
.
Below is a full sample Dockerfile
.
FROM ruby:2.7.2-alpine AS builder
RUN apk add --no-cache \
build-base libffi-dev \
nodejs yarn tzdata \
postgresql-dev postgresql-client zlib-dev libxml2-dev libxslt-dev readline-dev bash \
#
# For testing
chromium chromium-chromedriver python3 python3-dev py3-pip \
#
# Nice-to-haves
git vim \
#
# Fixes watch file issues with things like HMR
libnotify-dev
RUN pip3 install -U selenium
FROM builder AS development
# Add the current apps files into docker image
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install any extra dependencies via Aptfile - These are installed on Heroku
# COPY Aptfile /usr/src/app/Aptfile
# RUN apk add --update $(cat /usr/src/app/Aptfile | xargs)
ENV PATH /usr/src/app/bin:$PATH
# Install latest bundler
RUN bundle config --global silence_root_warning 1
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0", "-p", "3000"]
FROM development AS production
COPY Gemfile /usr/src/app
COPY .ruby-version /usr/src/app
COPY Gemfile.lock /usr/src/app
COPY package.json /usr/src/app
COPY yarn.lock /usr/src/app
# Install Ruby Gems
RUN bundle config set deployment 'true'
RUN bundle config set without 'development:test'
RUN bundle check || bundle install --jobs=$(nproc)
# Install Yarn Libraries
RUN yarn install --check-files
# Copy the rest of the app
COPY . /usr/src/app
# Precompile the assets
RUN RAILS_SERVE_STATIC_FILES=enabled SECRET_KEY_BASE=secret-key-base RAILS_ENV=production RACK_ENV=production NODE_ENV=production bundle exec rake assets:precompile
# Precompile Bootsnap
run RAILS_SERVE_STATIC_FILES=enabled SECRET_KEY_BASE=secret-key-base RAILS_ENV=production RACK_ENV=production NODE_ENV=production bundle exec bootsnap precompile --gemfile app/ lib/
Configure a Capybara driver
Put the following in spec/support/chrome.rb
.
# spec/support/chrome.rb
driver = :selenium_chrome_headless
Capybara.server = :puma, {Silent: true}
Capybara.register_driver driver 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=1400,1400")
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
Capybara.javascript_driver = driver
RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by driver
end
end
I wrote up a little more detail in a blog post I wrote on this topic.
/usr/bin/chromium-browser
. Switching towebdrivers
/selenium-webdriver
gives the error you're seeing. Even withWD_CHROME_PATH
set. The filechromedriver
is actually there any the Jenkins user has permiissions for it too. – Kmesonwhich chromium
.presence ||which chromium-browser
).chomp" – Yale