Skip to content

Using docker compose with selenium standalone *

Henk van der Veen edited this page Mar 23, 2021 · 3 revisions

Teaspoon under docker-compose with the selenium/standalone-* images makes sure you always have a nice up-to-date official release to test against.

Setup

You'll need to include the selenium-webdriver gem in your Gemfile.

gem "selenium-webdriver"

You'll need a docker-compose that looks a bit like this:

version: '3.4'

services:
  test:
    build:
      context: .
    command: bundle exec teaspoon
    environment:
      SELENIUM_HOST: selenium
      SELENIUM_PORT: 4444
      TEST_APP_PORT: 3000
      RAILS_ENV: test
      TEASPOON_RAILS_ENV: test
    depends_on:
    - selenium

  selenium:
    image: selenium/standalone-chrome
    shm_size: '2gb'
    logging: # Disable spamming irrelevant messages from this container
      driver: none
    environment:
      TZ: "Europe/Amsterdam"

Configure Teaspoon to use the selenium driver and point to the right url.

spec/javascripts/teaspoon_env.rb

require 'selenium-webdriver'

Teaspoon.configure do |config|
  config.driver = :selenium
  docker_ip = `hostname -i`.strip
  config.server_host = docker_ip
  config.server_port = ENV['TEST_APP_PORT']

  desired_capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chrome_options: Selenium::WebDriver::Chrome::Options.new(args: ['headless', 'disable-gpu'])
  )
  http_client = Selenium::WebDriver::Remote::Http::Default.new(read_timeout: 20)
  config.driver_options = {
    client_driver: :remote,
    selenium_options: {
      url: "http://#{ENV['SELENIUM_HOST']}:#{ENV['SELENIUM_PORT']}/wd/hub",
      http_client: http_client,
      desired_capabilities: desired_capabilities
    }
  }
end

Now when you run docker-compose run test it will run your specs.

gitlab-ci

You can also use this setup on GitLab CI:

.gitlab-ci.yml

teaspoon:
  image: ruby:2.7
  variables:
    SELENIUM_HOST: gitlab-selenium-server
    SELENIUM_PORT: 4444
    TEST_APP_PORT: 3000
  services:
    - name: selenium/standalone-chrome
      alias: gitlab-selenium-server