Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec with headless Chrome/Chromium on Alpine Linux

On Alpine 3.10.1, I'm trying to run RSpec with headless_chrome.

 config.before(:each, type: :system, js: true) do
    driven_by :selenium, using: :headless_chrome
  end

I have installed capybara and webdrivers.

Since I haven't found a way to install Chrome on Alpine, I have tried with Chromium (76.0.3809.87-r0). But, when I run the spec, it doesn't find the driver.

ChildProcess::LaunchError: No such file or directory - /root/.webdrivers/chromedriver

I have tried also to install chromium-chromedriver directly via apk but the outcome has been the same.

Is there a way to install Chrome on Alpine or to use Chromium with Capybara?

like image 452
Sig Avatar asked Aug 16 '19 22:08

Sig


1 Answers

There are three things necessary to get this working:

  1. Use selenium-webdriver instead of webdrivers
  2. Install chromium, chromium-chromedriver and selenium on your Docker image
  3. 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.

like image 142
Jason Swett Avatar answered Sep 17 '22 18:09

Jason Swett