SOLVED: running Capybara with headless Chrome

This is specific for the Ruby environment, but perhaps you can translate to other environments.

The main issue I was trying to solve: How to run headless Chrome for system tests without using Docker chaos.

Here is what your Render build script needs to do:

  1. make some directories
  2. download and unpack Chrome AND chromedriver
  3. add that directory to your PATH so Capybara can find it later
  4. (eventually, you can also add a check to not install Chrome each time if it’s already there, but I would recommend adding this later once you get it working. You can reference Alan’s original gist.)

Here is the build script:

# exit on error
set -o errexit

 # do a bunch of Rails stuff beforehand
bundle install
bundle exec rake assets:precompile
bundle exec rake assets:clean
bundle exec rake db:migrate

 # then download and install Chrome and chromedriver
STORAGE_DIR=/opt/render/project/.render
mkdir -p $STORAGE_DIR/chrome
cd $STORAGE_DIR/chrome
wget -P ./ https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
dpkg -x ./google-chrome-stable_current_amd64.deb $STORAGE_DIR/chrome
rm ./google-chrome-stable_current_amd64.deb
export PATH="${PATH}:/opt/render/project/.render/chrome/opt/google/chrome"
cd $HOME/project/src 

 # run tests (if these fail, then the deploy won't happen. Excellent!)
bundle exec rails test:system

Gotcha #1

Be sure you do NOT have a trailing slash at the end of the “export PATH” line. I’ve seen that in some references.

And wget and dpkg is all you’ve got to install stuff. So don’t try using apt or other things. You’ll get permission errors. If you have no choice but to use apt, you’ll have to go the Docker route.

Gotcha #2

If you’ve been playing around with similar setups like this, be sure to remove any creating directories and use Render’s “Manual Deploy > Clear build cache & deploy”. I believe this helped me start fresh a few times with troubleshooting.

Gotcha #3

The next part is Rails specific, but vital if you are using Rails or Ruby. Freaking Selenium doesn’t work. Not sure if it’s not passing the correct arguments to Chrome when it launches, but there are some session issues. Instead try using Cuprite. Your application_system_test_case.rb would then look like this:

require "test_helper"
require "minitest/rails/capybara"
require "capybara/cuprite"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  # driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
  driven_by :cuprite

  register_spec_type(self) do |desc, *addl|
    addl.include? :system
  end
end

Profit!

Now we have system tests running in a headless browser that must pass for our app to be deployed.

In this screenshot you can see the install finishing, Capybara and Cuprite starting, running the tests, passing, and then continuing on to the deploy. Wonderful!

Consideration

Keep an eye on your build minutes if you end up having a lot of tests. I’m planning on having a “test” Render environment which has RAILS_ENV=test and Auto Deploys off a different branch. That way I am only running my full test suite when I’m planning on releasing a major set of new features.

(apologies for typos, I’m writing this after about 6 hours of trying to get this to work).

Enjoy, and please post if this worked for you!

And this is the final script which checks the RAILS_ENV environment variable set within the Render environment variables.

# exit on error
set -o errexit

bundle install
bundle exec rake assets:precompile
bundle exec rake assets:clean
bundle exec rake db:migrate

if [[ "${RAILS_ENV}" == "test" ]]; then
  STORAGE_DIR=/opt/render/project/.render
  mkdir -p $STORAGE_DIR/chrome
  cd $STORAGE_DIR/chrome
  wget -P ./ https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
  dpkg -x ./google-chrome-stable_current_amd64.deb $STORAGE_DIR/chrome
  rm ./google-chrome-stable_current_amd64.deb
  export PATH="${PATH}:/opt/render/project/.render/chrome/opt/google/chrome"
  cd $HOME/project/src # Make sure we return to where we were

  rails test:system
fi

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.