In the previous article, we saw how we can use a Docker stand-alone container with selenium. In this article, we will learn how to run selenium tests in parallel using a Selenium Grid on multiple containers.
What is Selenium Grid?
Selenium Grid is part of the selenium suite which allows users to run selenium tests on different machines in parallel. It works on HUB and NODE architecture.
The user needs to run a test on his single machine which is HUB, and it is HUB’s responsibility to distribute the test on a different node connected to the HUB.
A Hub is a central point where all your tests are sent. Each Selenium Grid consists of exactly one hub. The hub needs to be reachable from the respective clients (i.e. CI server, Developer machine, etc.) The hub will connect one or more nodes that tests will be delegated to.
Nodes are different Selenium instances that will execute tests on individual computer systems. There can be many nodes in a grid. The machines which are nodes, do not need to be on the same platform or have the same browser selection like that of the hub or the other nodes. A node on Windows might have the capability of offering Internet Explorer as a browser option, whereas this wouldn’t be possible on Linux or Mac.
Challenges in using Selenium Grid without Docker
To run the selenium tests on multiple machines in parallel using selenium grid, there are few setbacks –
We need to have multiple machines/VMs up and running.
We need to set up the different browsers on different machines.
We need to download Selenium jar file on all the machines and need to run the command on all the machine which we are using as hub and node.
In case we want to scale the number of nodes for more parallel sessions, we need more machines.
Considering the above points, this setup requires cost and is not very scalable.
How does docker helps in overcoming above challenges?
We don’t need multiple machines. We just need to start the container with the required configuration in the image. This configuration includes different browsers, browser versions, different OS, etc.
Docker is highly scalable and you can run as many nodes as required with just single command.
Setting up Docker Container Grid
We need to follow the below steps to setup Selenium Grid on Docker Containers –
Pull the following docker images using commands given below:
Execute the above selenium test and see the result in your IDE. Test results will look like below:
Launching Selenium Grid on docker using docker compose
In the above approach, we launched the selenium hub and container using separate commands. There is another way of doing all this process by just one command. We need to use docker-compose (a component of docker). Docker-compose comes bundled with Docker desktop on Mac and Windows. If you are using Linux, please install docker-compose separately.
Refer to this page for the docker-compose installation process.
With docker-compose, we can start or stop the whole architecture with just one command. We need to configure all application services in the YAML file and need to start this using the docker-compose command.
Lets create our YAML file to start the Selenium Grid on DOCKER.
Create docker-compose YAML file and save it in .yml format. For this demo, the file name is docker-compose.yml
In the above YAML file, 3 containers will start. One will be the hub and one will be chrome node and one will be firefox node.
services: It contains the list of all containers.
image: It defines image which will be used to start container.
container_name: Name of the container
ports: ports in host:container format
volume: It is used to mount path of shared memory in host:container format.
depends_on: This is used to define the dependency of one container to another. In this file, the chrome and Firefox node will spin up after the selenium hub.
environment: This is used to define environment variables.
NODE_MAX_SESSION And NODE_MAX_INSTANCES: These variables are used to define how many different browsers can be opened in a node container. NODE_MAX_SESSION is used to define how many different browsers can open in the container. NODE_MAX_INSTANCES is used to define how many different instances of the same browser we can open in the container.
Run the below command to start containers –
docker-compose -f /pathtoYAMLfile/filename.yml up -d