Deploying Selenium Grid Using Docker
Deploying Selenium Grid Using Docker
Want to set up Selenium Grid in parallel to avoid performance issues? See how Docker Swarm and Compose help you with your testing.
Join the DZone community and get the full member experience.Join For Free
Selenium Grid makes automation execution jobs much easier. Using Selenium Grid, one can run multiple tests on multiple machines in parallel, which reduces execution times from days to hours.
Docker helps a lot in setting up test labs using Grid in very easy and simple steps while 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 to set up Grid on your local machine for debugging purposes. We'll also look at setting up a Docker Swarm cluster for production-level distributed testing.
For all the methods below, you should have Docker installed on your system. You can find the Docker installation guide here: https://docs.docker.com/engine/installation/
Deploying Selenium Grid on a Standalone Docker Container
Selenium provides standalone Grid server Docker images with Chrome and Firefox browsers already installed on it. You can download the images from the Selenium repository on Docker Hub: https://hub.docker.com/u/selenium/
Pull or run the standalone server Docker image using this command:
docker run -d -P -p 4444:4444 --name standalone_grid selenium/standalone-chrome
With this command, the Selenium standalone Chrome image will be downloaded and the server will be started on your local machine on port 4444.
You can check on the running container using the following command:
docker ps -a
Similarly, if you want to use Firefox, you can run a standalone server for it. The execution that 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 the selenium/standalone-chrome-debug or Firefox images. These images have a VNC server installed so you can use VNC viewer or any other VNC client.
Deploying Selenium Grid on Multiple Containers Using Docker Compose
Docker Compose is the tool that lets you deploy Selenium Grid in multiple containers. You can deploy Selenium Grid with a hub and nodes for parallel execution. Docker Compose uses YAML files to configure 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 example, if you want to use Chrome 61.0.3163.91, you can use the image with the 3.5 tag next to the image name. If you skip the tag, it will take the latest image with the latest browser version.
The ‘Environment’ parameter is used to configure environment variables for the hub and nodes.
The ‘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. The nodes will start communicating with the hub. You can check whether 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 a single machine only, so it has performance limitations.
To avoid this problem, we can deploy the hub and nodes on different machines using Docker Swarm.
Deploying Selenium Grid on Multi-Host Environments Using Docker Swarm
Using Docker Swarm, we can deploy Grid on multiple machines as a cluster, which solves the performance issue.
To provision a Docker Swarm cluster, you need to add separate machines with sufficient memory for each browser container. You can manually set up machines in your test lab with Docker installed on each machine, or you can also use the docker-machine command to set up Docker hosts on your local machine, datacenter, or in the cloud like AWS/Azure. To know more about Docker Machine, please visit: https://docs.docker.com/machine/overview/
Initializing the Manager Node
To initialize Docker Swarm, run this command on one of the hosts:
docker swarm init --advertise-addr <manager-ip>
You will see this output:
Add Worker Nodes to the Swarm
Run this command on the worker node to join the swarm cluster:
docker swarm join --token <token>
You can add a maximum of two nodes in this case to the cluster each for Chrome and Firefox.
Check if all nodes have been added to the cluster by running this command on the Swarm manager:
docker node ls
Your cluster is ready to deploy Selenium Grid.
Create a YAML File to Deploy Selenium Grid
Create a docker-compose.yml file on the machine that you want to set up as a Grid hub.
This file looks similar to the one we created in th Docker Compose section, but it has some extra configuration to tell Docker to deploy Grid in swarm mode.
Links will not be used in this case. Instead, nodes will be communicated to the hub using their HOSTNAMEs, which are provided in SE_OPTS to entry_opint.sh on each Selenium node. The Entry_point.sh file is available on each node to start the node and allow it to communicate to the hub. This is just like configuring a Selenium node using a JSON file.
Deploy Selenium Grid on Docker swarm
Run a command where you have the .yml file or provide the file path using the –compose-file parameter:
docker stack deploy -c docker-compose.yml selenium
You can check if all services are running on nodes using this command:
docker service ls
See? Selenium Grid is running on the swarm using http://[manager-ip]:4444/grid/console
You can also scale browser containers using this command on the Swarm manager:
docker service scale chrome=5
Selenium Grid is now ready to run scripts in parallel without any performance issues.
Published at DZone with permission of Milind Gholap , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.