Introduction

When talking about automation testing, one of the first things that comes to mind is Selenium.

Selenium is a free, open-source automated testing framework used to validate web applications across different browsers and platforms. It is not just a single tool but a suite of software. Every component of Selenium solves a different problem, and one of the components is Selenium Grid.

Selenium Grid is the edition of Selenium you use to perform grid testing.

What is Grid testing? 

Google Chrome is the most popular web browser. However, people use other browsers like Mozilla, Opera, and others. Ensuring that a web application works on every browser is very important, but on the other hand it consumes a lot of time when being tested manually. 

The other thing to consider is that people use different versions of system software on their computers like Windows, macOS X, Linux, and IOS or Android on mobile devices.

If you would like to test a web application on the above mentioned environments, it would require a lot of resources, which is pretty hard to ensure, considering that software delivery is limited by the duration of the sprint.

The solution to the problem above is Grid Testing, which enables you to run automation tests simultaneously on a grid with a combination of different browsers and OS versions.

Selenium Grid is a tool based on the principle of hub-node. The Selenium Hub acts as the central coordination point for test distribution, and the execution of the test cases takes place in different Selenium nodes/browsers.

Selenium Grid 4

Selenium Grid 4 is a new version of Selenium Grid tools. It is important to note that Selenium Grid 4 does not share the codebase of the previous versions. Instead of two, it supports four distinct processes: Router, Node, Distributor, and Session Map. Moreover, Selenium Grid 4 can be set up in 3 modes:

  • Standalone Mode
  • Fully Distributed (Router, Distributor, Node, and Session)
  • Classical Grid (Hub and Node)

Standalone Mode combines all Grid components in one, while others require running multiple processes to set up a fully functioning Grid.

Benefits of Selenium Grid

The major benefits would be:

  • Enables testing across different browsers and different browser versions
  • Enables testing across different operating systems
  • It’s an open-source solution

To top it off, the main benefit of Selenium Grid is that it allows you to run concurrent tests on several different machines easily.

Set-up Selenium Grid 4 with Docker

Selenium Grid offers Docker support, and in this example, we will set up Selenium Classical Grid (Hub and Node) with Docker compose.

First of all, you need to download Desktop Docker from this link.

Then, you need to create a docker-compose.yml file.

Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, we can create a YAML file to define the services, and with a single command, we can spin everything up or tear it all down.

An example of the docker-compose.yml file: 

version: "3"
services:
  chrome:
    image: selenium/node-chrome:4.1.4-20220427
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
      - SE_NODE_MAX_INSTANCES=1
      - SE_NODE_MAX_SESSIONS=1

  edge:
    image: selenium/node-edge:4.1.4-20220427
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
      - SE_NODE_MAX_INSTANCES=1
      - SE_NODE_MAX_SESSIONS=1

  firefox:
    image: selenium/node-firefox:4.1.4-20220427
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
      - SE_NODE_MAX_INSTANCES=1
      - SE_NODE_MAX_SESSIONS=1

  selenium-hub:
    image: selenium/hub:4.1.4-20220427
    container_name: selenium-hub
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"

 

Let’s explain what we defined in this file:

  • versionversion of the docker-compose files
  • services/containers – hub and nodes with their images and configurations
  • imagedefines which image will be used to spin up a container
  • shm_size – shared memory, used to increase memory and avoid crashing in docker containers because default shm_size is 64 mb
  • ports – used to access the services running inside a Docker container
  • container_name – name of container
  • depends_on – defines the required dependency before spinning up the container. In the docker-compose.yml file above, containers Chrome, Edge and Firefox depend on the selenium-hub container.
  • SE_NODE_MAX_INSTANCES – defines how many instances of the same version of the browser can run over the Remote System
  • SE_NODE_MAX_SESSIONS – defines the maximum number of concurrent sessions that will be allowed.

After we configured Hub and Nodes and the docker-compose.yml file is ready to start Selenium Grid, we need to navigate to the folder where the docker-compose.yml file is located and run the command:

docker compose -f docker-compose.yml up

-f options take the file name as a parameter, and docker-compose.yml is the name of the file.

When, when Grid is up and running, according to the configurations defined in the docker-compose.yml file, we can check it on the url: http://localhost:4444 .

Also, you can check it on Desktop Docker:

With the command docker ps, you can check all running containers.


Configure your tests for parallel execution and monitor them on the selenium grid dashboard:

Since we configured one instance for each Chrome, FireFox, and Edge, it has reached maximum concurrency, because we have only one respective instance for these browsers.

Selenium Grid 4 offers a Sessions tab to monitor parallel execution live. There is some additional information like Start time, Duration, etc.

The picture below shows that the execution on the Edge browser is finished, and executions on Chrome and Firefox are still running. When execution is done, the session is automatically closed, and if there is any session in the queue, it will be picked up for execution.


Also, we can monitor sessions live on the browser. Just click on the video icon on the browser session you want to monitor and enter the password “secret”.

seleniumIn the docker-compose.yml file, we configured one instance per browser, but if we want, we can scale any browser service and have more instances up and running.

This is possible with the command below:

docker compose -f docker-compose.yml up --scale chrome=3

and/or

docker compose -f docker-compose.yml up --scale firefox=2

and/or

docker compose -f docker-compose.yml up --scale edge=4

Check the Selenium Grid dashboard, and you can see that now we have two more Chrome instances.

You can do the same with other browsers.

selenium
Grid can also be stopped by pressing ctrl+c in the terminal. Also, you can remove containers and images with the command:

docker compose -f docker-compose.yml down

Conclusion

Selenium Grid is a valuable tool for staying competitive in the fast-paced world of software development. The major gain it provides is cross-browser testing, which improves test coverage, elevates the quality of the project, and ensures a consistent user experience.

Headless Selenium tests can be run in any environment, locally or on a Jenkins server, with configuration set up only once. It requires careful setup and maintenance, even though it’s simplified with a Docker compose file, which means all parameters are set up in one file and can be started with one command, as mentioned.

Using Selenium grid can save time and resources in projects where running multiple tests at the same time is needed. It enables efficient testing across various browsers, operating systems, and devices, leading to faster test execution and improved test coverage. Additionally, collaboration is enhanced, resulting in more robust web applications.


“Selenium Grid 4 with Docker” Tech Bite was brought to you by Semir Hajdar, Medior Quality Assurance Engineer at Atlantbh.

Tech Bites are tips, tricks, snippets or explanations about various programming technologies and paradigms, which can help engineers with their everyday job.

selenium
QA/Test AutomationTech Bites
December 22, 2023

Selenium Grid 4 with Docker

Introduction When talking about automation testing, one of the first things that comes to mind is Selenium. Selenium is a free, open-source automated testing framework used to validate web applications across different browsers and platforms. It is not just a single tool but a suite of software. Every component of…

Want to discuss this in relation to your project? Get in touch:

Leave a Reply