Loading...

Setting up Selenium Grid with Docker

 Some time ago I have written article about setting up Selenium Grid on Aws EC2. Process described in that article maybe is not super complicated but to make it working correctly some commands need to be executed and some configuration done. It become more complicated when we need to change the browser version because driver version usually also has to be changed. In this article I will describe how to set up Selenium Grid environment with use of Docker and show how easy it is.

First of all we need to decide on which platform we want to set it up. For purpose of this article I will use Ubuntu and assume that Docker and Compose are installed in the system. Note that you have to install Docker Engine with at least version 17.12.0 to support compose file in version 3.5. Compose and Docker compatibility matrix shows which compose file versions support specific Docker release.

When Docker and Compose are installed we need to create compose file which will keep our configuration. The name of this file is 'docker-compose.yml'.

version: "3.5"
services:
  selenium-hub:
    image: selenium/hub
    container_name: selenium-hub
    ports:
      - "4444:4444"
    environment:
      - GRID_TIMEOUT=300
      - GRID_BROWSER_TIMEOUT=300
  chrome:
    image: selenium/node-chrome
    restart: always
    depends_on:
      - selenium-hub
    environment:
      - HUB_PORT_4444_TCP_ADDR=selenium-hub
      - HUB_PORT_4444_TCP_PORT=4444
      - SCREEN_WIDTH=1600
      - SCREEN_HEIGHT=1200
    shm_size: 2gb
  firefox:
      image: selenium/node-firefox
    depends_on:
      - selenium-hub
    environment:
      - HUB_PORT_4444_TCP_ADDR=selenium-hub
      - HUB_PORT_4444_TCP_PORT=4444
      - SCREEN_WIDTH=1600
      - SCREEN_HEIGHT=1200
    shm_size: 2gb


There are a few things that are worth to be mentioned about the configuration above. Variable HUB_PORT_4444_TCP_ADDR has assigned selenium hub container name and on this way docker indicates to selenium node which IP it should connect to. On both selenium grid nodes I have set the value of shared memory to 2GB(shm_size: 2gb) - it is known workaround to avoid the browser crashing inside a docker container (more information here). Now by executing command 'docker-compose up -d' we are setting everything up. Selenium grid hub is available on this machine on port 4444 and we have two connected nodes, one firefox node and one chrome node - both with latest version of Firefox and Chrome. If we want to test something on other version of browser e.g. Firefox 65 then with Docker it is super simple. We just need to update the image version in the docker-compose.yml to image: selenium/node-firefox:3.141.59-gold and run the docker-compose again. The whole list of available images with other browsers versions is available here.

With use of Docker we can easily solve problem of setting up environment for files downloading and selenium grid. We can do that by adding the new container with apache server and share the folder with downloaded files on selenium nodes to the www folder on container with apache server. On this way every downloaded file in any node with selenium grid node is automatically accessible on apache server. Updated docker-compose.yml file can look like below.

version: "3.5"
services:
  selenium-hub:
    image: selenium/hub
    container_name: selenium-hub
    ports:
      - "4444:4444"
    environment:
      - GRID_TIMEOUT=300
      - GRID_BROWSER_TIMEOUT=300
  chrome:
    image: selenium/node-chrome
    restart: always
    depends_on:
      - selenium-hub
    environment:
      - HUB_PORT_4444_TCP_ADDR=selenium-hub
      - HUB_PORT_4444_TCP_PORT=4444
      - SCREEN_WIDTH=1600
      - SCREEN_HEIGHT=1200
    shm_size: 2gb
    volumes:
      - ./shared:/home/seluser/Downloads
  firefox:
      image: selenium/node-firefox
    depends_on:
      - selenium-hub
    environment:
      - HUB_PORT_4444_TCP_ADDR=selenium-hub
      - HUB_PORT_4444_TCP_PORT=4444
      - SCREEN_WIDTH=1600
      - SCREEN_HEIGHT=1200
    shm_size: 2gb
    volumes:
      - ./shared:/home/seluser/Downloads

  web:
    image: apache
    container_name: apache_web
    restart: always
    volumes:
      - ./shared:/var/www/html
    ports:
      - "80:80"


Now all the puzzles are together so we just need to execute the command 'docker-compose up -d'. By doing that we run selenium grid hub, selenium grid node with chrome, selenium grid node with firefox and apache server with shared drive available in current machine in 'shared' folder.