Skip to content

Instantly share code, notes, and snippets.

@coliver
Last active September 1, 2020 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coliver/e37ec63f11d728a78178341a0410f8c0 to your computer and use it in GitHub Desktop.
Save coliver/e37ec63f11d728a78178341a0410f8c0 to your computer and use it in GitHub Desktop.
Rails Headful (Not Headless) Selenium Chrome Tests in Docker

This was tested on a Mac running 10.13.6 High Sierra.

Why? I wanted to be able to stop a system spec mid-run if I was having issues with it.

On the mac host:

Install brew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Install xQuartz (an X server for mac)

brew cask install xquartz

Run xquartz. In Preferences > Security > Allow Connections from network clients <- Make sure this is checked.

Log out, log in your mac.

Open xquartz terminal:

xhost +127.0.0.1

Share the host's X dir and DISPLAY var with the container. In docker-compose.yml, add this volume:

containername:
  volumes:
    - /tmp/.X11-unix:/tmp/.X11-unix
  ...
  environment:
    DISPLAY: host.docker.internal:0

Add this somewhere before your tests run:

require 'selenium/webdriver'

Capybara.register_driver(:selenium_chrome) do |app|
  options = Selenium::WebDriver::Chrome::Options.new(
    args: %w[
      no-sandbox
      disable-setuid-sandbox
      disable-extensions
      window-size=1366,768
      use-gl=swiftshader
    ]
  )

  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    service: Selenium::WebDriver::Service.new(path: '/usr/local/bin/chromedriver', port: 3333), # I had to specify the chromedriver location. YMMV
    options: options
  )
end

Capybara.javascript_driver = :selenium_chrome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment