Selenium Grid with Docker

Shiksha Engineering
3 min readJan 30, 2023

Author : Geetu Sadana

Selenium Grid uses Remote Web driver to run multiple test cases on different operating systems, browsers and machines simultaneously. It has a client-server architecture in which the client is known as a node and the Server is known as a hub. It aims to provide an easy way to run tests in parallel on multiple machines.

When to Use a Grid?

  1. To run tests against multiple browsers, multiple versions of browsers, and browsers running on different operating systems.
  2. To reduce the execution time for the test suite.

Why we moved to Docker

Earlier in Shiksha we were using Grid without docker.

Setting up Selenium Grid architecture can be a lengthy process. We need to download browser drivers and the Selenium server, configure hub and nodes for each machine and run multiple commands. It’s really hard to manage our hub and nodes when the number of instances gets increases. This is very time-consuming and tedious process.

Things become more difficult to manage when tests are required to be run on different versions of a browser. Managing multiple browser versions on a system and running tests on them is an exhausting process.

Advantages of setting the selenium grid using Docker:

1. Easy to Scale: Scalability is one of the main advantages of having selenium grid build using Docker.

2. Easy to Maintain: Reduces the overhead of Downloading and setting up the class paths for Web- drivers.

3. Easy to Set up and Trash: With Docker compose, one can bring up the grid set-up when needed and can trash once done. Lesser chances of discrepancies are here.

4. Highly Secure: The benefit of better security is another notable aspect of using Docker as all the hub and nodes are in containers.

5. Better Speed.

Setting up selenium Grid with Docker-

We will be using docker-compose to start the selenium grid as it is easy and maintainable. docker-compose is a tool for defining and running multi-container docker applications. With Compose, we use a Compose file to configure the application’s services. Then, using a single command, we can create and start all the services from configuration. It also has the option to scale the services as and when required. With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

Install docker-compose with below command

Docker-compose YAML file-

version: 3. It is the version of the docker-compose file.

services(containers): This contains the list of the images and their configurations.

image: It defines which image will be used to spin up container.

ports: Published ports with host:container format.

depends_on: This defines the required dependency before spinning up the container. In our docker-compose.yml file, containers Chrome and Firefox are dependent upon container hub to spin up.

As per the configurations in the docker compose file, we would be spinning up 2 browsers in the grid so we have set 2 services accordingly for the browsers and their corresponding images have been pulled for starting their instance. Also, selenium-hub service is created to pull the image for creating the hub. Nodes of the services namely, chrome and firefox will be connected to the hub.

Now we have the docker compose file ready with all the configurations required to start the selenium grid. To start the grid, we need to navigate to the folder where docker-compose file is located and run the following command:

— scale is used to node up at run time

This will create 7 containers consists of 3 Chrome and Firefox instances and 1 hub container.

Now just go to http://localhost:4444/grid/console and you will be able to see grid is up and running with three chrome and firefox nodes.

Few useful commands-

docker-compose restart : to restart the grid
docker-compose down : to tear down the grid

docker images : to show all the images

docker ps : to show all the containers

docker ps –a : to show all the containers including stopped containers

docker system prune –f : to remove all stopped containers

docker system prune –a : to remove all stopped containers +unused images

docker logs containerName : to check container logs

Future Scope: Dynamic docker — Grid 4 has the ability to start Docker containers on demand, this means that it starts a Docker container in the background for each new session request, the test gets executed there, and when the test completes, the container tear down itself.

References:

https://github.com/SeleniumHQ/docker-selenium

--

--