Migrating from PhantomJS to google-chrome headless using Docker

#automated-test, #dev, #docker, #ruby

I have been using PhantomJS for a long time it still works, but it has become abandonware. Better migrate now than in a time of need.

Dockerfile change

You have to add this to your Dockerfile:

run wget -q -O- https://dl.google.com/linux/linux_signing_key.pub | apt-key add - && \
  echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list && \
  apt-get update && apt-get install -y libxss1 libappindicator1 libindicator7 google-chrome-stable

Change based on your current Dockerfile, but if you want to test, add that code at the bottom. You can take a look at the diff of my gist to start a Ruby project using docker (for more info about this gist read my post Zero to Up and Running Ruby Project (Short Version)).

Change to spec/support/capybara.rb

My final spec/support/capybara.rb is this:

Capybara.server = :puma, { Silent: true }

Capybara.register_driver :chrome do |app|
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: {
      args: %w[no-sandbox headless disable-popup-blocking disable-gpu]
    }
  )

  Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: capabilities)
end

Capybara.javascript_driver = :chrome

If you need to change the size of the windows, use window-size=1280,1024 in the args. I participated only in one project that the size of the windows has had to change.

References