Selenium Grid combined with Docker is a simple but powerful solution to getting Selenium based infrastructure up and running quickly and easily.
Rather than dealing with the hassle of manually configuring and installing Selenium Grid we decided to go with Docker, it’s quicker and easier to use.
Our Selenium grid is used heavily for Protractor testing and to run our Jasmine unit tests on a constant cycle making sure we don’t introduce issues into our product.
The docker selenium image repositories are located at:
https://github.com/SeleniumHQ/docker-selenium
It includes instructions on how to get the containers up and running, which is great as it’s 1 docker run
comamnd per container and you are up and running.
However, let’s take this further and combine it with docker compose to make using it even easier to use.
This compose file is designed to:
- Pull down the required images from docker hub for you.
- Allow easy create, start, stop and destroying the grid.
- Do the hard work of joining the containers together in a docker network (docker link is deprecated).
- Easily scale the number of Chrome or FireFox nodes.
version: '2'
services:
selenium_hub:
image: selenium/hub:3.0.1-aluminum
container_name: selenium_hub
privileged: true
ports:
- 4444:4444
environment:
- GRID_TIMEOUT=120000
- GRID_BROWSER_TIMEOUT=120000
networks:
- selenium_grid_internal
nodechrome:
image: selenium/node-chrome-debug:3.0.1-aluminum
privileged: true
depends_on:
- selenium_hub
ports:
- 5900
environment:
- no_proxy=localhost
- TZ=Europe/London
- HUB_PORT_4444_TCP_ADDR=selenium_hub
- HUB_PORT_4444_TCP_PORT=4444
networks:
- selenium_grid_internal
nodefirefox:
image: selenium/node-firefox-debug:3.0.1-aluminum
privileged: true
depends_on:
- selenium_hub
ports:
- 5900
environment:
- no_proxy=localhost
- TZ=Europe/London
- HUB_PORT_4444_TCP_ADDR=selenium_hub
- HUB_PORT_4444_TCP_PORT=4444
networks:
- selenium_grid_internal
networks:
selenium_grid_internal:
To use this docker compose file you could issue commands like:
docker-compose up -d
to bring up the 3 containers and run as a background process.
However, @Yantrio, our Linux magician, a while ago came up with the idea of using Makefiles to help simplify the commands.
https://www.yantr.io/post/docker-makefiles/
Take this Makefile:
NODECHROME=5
NODEFIREFOX=1
.PHONY: default up down start stop scale
default: up
up:
docker-compose up -d
docker-compose scale nodechrome=$(NODECHROME) nodefirefox=$(NODEFIREFOX)
down:
docker-compose down
start:
docker-compose start
stop:
docker-compose stop
# Have to restart hub due to it trying to connect to destroyed nodes
scale:
docker-compose scale nodechrome=$(NODECHROME) nodefirefox=$(NODEFIREFOX)
docker restart selenium_hub
It abstracts away having to remember to say put -d
on the up compose command to run as a background process.
You simply can do:
make up
to create and start all the containers
make down
to stop and remove all the containers
make stop
to just stop the containers
make start
to just start the containers
But, the key win is how to scale the selenium grid.
You simply call:
make scale NODECHROME=10 NODEFIREFOX=5
To bring up another 5 Chrome containers and another 4 FireFox containers.
As docker compose will see you already have 5 Chrome containers and scale up to the requested 10 containers by making another 5.
Scaling down your grid is as simple as lowering the number of containers you require.
As the comment suggests, we restart the hub container to get around network connection timeout issues when containers disappear.
Simples!