Selenium Grid made the automation execution job much easier. Using Selenium Grid one can run multiple tests on multiple machines in parallel, which reduces execution time from days to hours.
Docker helps a lot in setting up test labs using Grid in very easy and simple steps and helping to remove all complexities.
For more information on docker please visit https://www.docker.com/
In this article, all the methods are explained to make use of Docker from setting up Grid on your local machine for debugging purpose to set it up on Docker Swarm cluster at production level distributed testing.
For all the methods below, you should have Docker installed on your system. You can find docker installation guide here: https://docs.docker.com/engine/installation/
Selenium provides standalone grid server docker images with Chrome and Firefox browsers already installed on it. You can download the images from Selenium repository on Docker hub: https://hub.docker.com/u/selenium/
Pull or run the standalone server docker image using the command:
docker run -d -P -p 4444:4444 --name standalone_grid selenium/standalone-chrome
With this command, Selenium Standalone chrome image will be downloaded and the server will be started on your local machine on port 4444.
You can check the running container using the following command:
docker ps -a
Similarly, if you want to use Firefox you can run a standalone server for Firefox.The execution happens on these images is headless using Xvfb.
If you want to see your execution or debug your script step by step, you can use selenium/standalone-chrome-debug or firefox images. These images have VNC server installed so you can use VNC viewer or any other VNC client.
Docker compose is the tool using which you can Selenium Grid on multiple containers. You can deploy Selenium Grid with hub and nodes for parallel execution. Docker-compose uses YAML file to configure the application services like a hub, chrome and Firefox will be the services in this case.
Create a docker-compose.yml file on your machine.
If you want to use any specific version of the browser, you can use image available with that browser. All images are maintained with the tag on the selenium docker hub. For e.g. If you want to use Chrome 61.0.3163.91 you can use the image with the tag 3.5 next to the image name. If you skip the tag it will take the latest image with the latest browser version.
‘Environment’ parameter is used to configure environment variables for hub and nodes.
‘Links’ parameter is used to connect nodes to the hub with its service name.
Save the docker-compose.yml file and start the grid using the command:
docker-compose up -d
This will pull and run the images, hub, and nodes will be up, nodes will start communicating with the hub. You can check if everything is running fine with the command:
docker ps -a
If you want to increase the number of browser containers you can use the command:
docker-compose scale chrome=5
Now you can check grid console: http://[yourMachineIP]:4444/grid/console.
Docker-compose deploys grid on the single machine only, so it has performance limitations.
To avoid this problem, we can deploy hub and nodes on different machines using Docker Swarm.
Using Docker swarm we can deploy Grid on multiple machines as a cluster which solves the performance issue.
To provision Docker swarm cluster you need to add separate machines with sufficient memory for each browser container. You can manually setup machines in your test lab with docker installed on each machine or you can also use the docker-machine command to setup Docker hosts on your local machine, datacenter or on the cloud like AWS/Azure. To know more about docker-machine please visit: https://docs.docker.com/machine/overview/
A. Initializing manager node
To initialize docker swarm run the command on one of the hosts:
docker swarm init --advertise-addr <manager-ip>
You will see the output:
B. Add worker nodes to the swarm
Run the command on the worker node to join the swarm cluster:
docker swarm join --token <token>
You can add maximum two nodes in this case to the cluster each for Chrome and Firefox.
Check if all nodes are added to the cluster running the command on swarm manager:
docker node ls
Your cluster is ready in these simple steps to deploy Selenium Grid.
C. Create YAML file to deploy Selenium grid.
Create docker-compose.yml file on the machine which you want to set up as a grid hub.
This file looks similar to the one we have created in docker compose section but it has some extra configuration to tell docker to deploy grid in the swarm mode.
Links will not be used in this case instead nodes will be communicated to the hub using HOSTNAME which is provided in SE_OPTS to entry_opint.sh on each selenium node. Entry_point.sh file available on each node to start the node and allows it to communicate to the hub. This is just like configuring selenium node using JSON file.
D. Deploy Selenium grid on Docker swarm
Run the command where you have the .yml file or provide file path using –compose-file parameter
docker stack deploy -c docker-compose.yml selenium
You can check if all services are running on nodes using the command:
docker service ls
See the selenium grid is running on swarm using http://[manager-ip]:4444/grid/console
You can also scale the browser containers using the command on swarm manager:
docker service scale chrome=5
Selenium Grid is now ready to run scripts in parallel without any performance issues.