Skip to content

Getting Started with Docker Compose

Diego Molina edited this page Nov 5, 2019 · 17 revisions

Getting Started with Hub & Nodes

This is a step-by-step introduction to using the official Selenium Docker images using a basic hub/node configuration and docker-compose.

Step 1: Write the docker-compose.yml file

To begin, create a new file called docker-compose.yml in an empty directory with the following contents:

version: '2'
services:
  firefox:
    image: selenium/node-firefox:3.14.0-gallium
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - hub
    environment:
      HUB_HOST: hub

  chrome:
    image: selenium/node-chrome:3.14.0-gallium
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - hub
    environment:
      HUB_HOST: hub

  hub:
    image: selenium/hub:3.14.0-gallium
    ports:
      - "4444:4444"

Open a shell, navigate to the directory of the newly created YAML file, and use the following command:

$ docker-compose up -d

This will pull the official images for hub, node-chrome, and node-firefox from Docker and start a grid with one instance of each browser available. Note: when you omit a version at the end, it will default to "latest" tag. To specify a version, simply add :<version> at the end. E.g: selenium/hub:3.3.1

Step 2: Scaling the grid on-demand

Now that we have the grid up and running, let's use Docker scaling to spin up additional nodes.

Scaling up Chrome nodes

$ docker-compose scale chrome=5
# Spawns four additional node-chrome instances linked to the hub

Scaling up Firefox nodes

$ docker-compose scale firefox=5
# Spawns four additional node-firefox instances linked to the hub

Step 3: Customizing with environment variables

Configuration of the selenium hub and nodes is handled primarily through docker environment variables. For a full list of environment variables supported on each image, find your image at Docker and check the "Selenium Configuration" section of the "Dockerfile" tab. Note: The node-chrome-debug and node-firefox-debug images inherit their Selenium Configuration options from their non-debug variants.

Step 4: Running tests

Note: Please consult the official documentation if you are having issues with the following code.

Java

import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class MyTest {

  Capabilities chromeCapabilities = DesiredCapabilities.chrome();
  Capabilities firefoxCapabilities = DesiredCapabilities.firefox();

  public static void main() {
    RemoteWebDriver chrome = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), chromeCapabilities);
    RemoteWebDriver firefox = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), firefoxCapabilities);

    // run against chrome
    chrome.get("https://www.google.com");
    System.out.println(chrome.getTitle());

    // run against firefox
    firefox.get("https://www.google.com");
    System.out.println(firefox.getTitle());
  
    chrome.quit();
    firefox.quit();
  }
}

Ruby

require 'selenium-webdriver'

chrome_capabilities = Selenium::WebDriver::Remote::Capabilities.chrome()
firefox_capabilities = Selenium::WebDriver::Remote::Capabilities.firefox()

chrome = Selenium::WebDriver.for(:remote, :url => 'http://localhost:4444/wd/hub', :desired_capabilities => chrome_capabilities)
firefox = Selenium::WebDriver.for(:remote, :url => 'http://localhost:4444/wd/hub', :desired_capabilities => firefox_capabilities)

chrome.get('http://google.com')
puts chrome.title

firefox.get('http://google.com')
puts firefox.title

chrome.quit
firefox.quit

Python

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

chrome = webdriver.Remote(
          command_executor='http://localhost:4444/wd/hub',
          desired_capabilities=DesiredCapabilities.CHROME)
firefox = webdriver.Remote(
          command_executor='http://localhost:4444/wd/hub',
          desired_capabilities=DesiredCapabilities.FIREFOX) 

chrome.get('https://www.google.com')
print(chrome.title)

firefox.get('https://www.google.com')
print(firefox.title)

chrome.quit()
firefox.quit()

JavaScript

/**
* Refer: [here](https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/example/google_search_test.js)
*/
const {Builder, By, until} = require('selenium-webdriver');
const test = require('../testing');

test.describe('Google Search', function() {
  let driver;

  test.before(function *() {
    driver = yield new Builder().forBrowser('firefox').usingServer('http://localhost:4444/wd/hub').build();
  });

  test.it('works with generators', function*() {
    yield driver.get('http://www.google.com/ncr');
    yield driver.findElement(By.name('q')).sendKeys('webdriver');
    yield driver.findElement(By.name('btnG')).click();
    yield driver.wait(until.titleIs('webdriver - Google Search'), 1000);
  });

  test.after(() => driver.quit());
});