Parallel Execution of Tests using Selenium Grid 4 with Docker Compose

Mohammad Faisal Khatri
10 min readMay 7, 2022

--

Cover image

In my last article End to End Testing using Selenium WebDriver and Java, I discussed about how to write end to end tests using Selenium WebDriver with Java where we went in depth understanding the features of Selenium and how to create an automation test strategy to write end to end tests.

In this post, We will be discussing about how to run the tests parallelly using Selenium Grid 4. Before we begin discussing about the details in depth lets first understand what Selenium Grid is and what problem it solves?

What is Selenium Grid?

Selenium Grid allows the execution of WebDriver scripts on remote machines (virtual or real) by routing commands sent by the client to remote browser instances. It aims to provide an easy way to run tests in parallel on multiple machines.

Selenium Grid allows us to run tests in parallel on multiple machines, and to manage different browser versions and browser configurations centrally (instead of in each individual test).

Selenium Grid is not a silver bullet. It solves a subset of common delegation and distribution problems, but will for example not manage your infrastructure, and might not suit your specific needs.

What’s new in Selenium Grid 4?

Grid 4 takes advantage of a number of new technologies in order to facilitate scaling up while allowing local execution.

Selenium Grid 4 is a fresh implementation and does not share the codebase the previous version had.

I like the way noVNC is bundled with selenium grid. There is a Sessions tab in the dashboard which when clicked will show you the links of the browser sessions that are in play with a video icon beside it. On clicking the video icon, it will ask you to enter the password which is (“secret”) after which we will be navigated to the browser session currently in play and watch what the test running live.

In the previous version of the grid, we had to explicit make the configurations for noVNC viewer if we had to see what’s cooking inside the browser session. From the grid point of view, we only had the browser icons getting highlighted so we came to know that the tests are getting executed in so and so browser.

When to Use a Grid?

In general, there are two reasons why you might want to use Grid:

  • To run your tests against multiple browsers, multiple versions of browser, and browsers running on different operating systems.
  • To reduce the time it takes for the test suite to complete a test pass.

Selenium Grid Node and Hub Configuration flags:

This website has all the related flags that could be used while configuring the hub and node for selenium grid 4.

Getting Started

Configuration

We will be using docker-compose to start the selenium grid as I find it more easy and maintainable. It also has the option to scale the services on real time as and when required.

What is docker compose?

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, can spin everything up or tear it all down.

The big advantage of using Compose is you can define your application stack in a file, keep it at the root of your project repo (it’s now version controlled), and easily enable someone else to contribute to your project. Using the file and typing the command docker-compose -f <filename> up you can spin everything up within minutes, and that is the part about docker-compose which I like the most.

Test Strategy

Whenever it comes to testing, we should have a clear goal set of the things which we need to perform. It helps in narrowing down the tasks and picking up the things easily and in specific order.

There are 2 instances using Selenium Grid which we can apply to our tests:

  1. Running the tests parallel in cross browsers.
  2. Running the tests parallel in same browser.

I have written end to end tests for OWASP Juice Shop website which we would be running in parallel in cross browsers as mentioned in point 1 above. Browsers selected for running this test are Google Chrome, Mozilla Firefox and Microsoft Edge.

For Point 2, I have written multiple tests for various scenarios for the-internet website which we will be using to run in parallel, there are 3 tests which we need to run in parallel on Google Chrome Browser.

Lets Get Started

Setting up Selenium Grid 4 with Docker Compose

The most important part in setting up the grid is docker compose file, lets discuss about the components of the file:

docker-compose-v3.yml
  • version: 3. It is the latest version of the docker-compose files.
  • 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.
  • container_name: You can give name to your containers.
  • 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.
  • SE_NODE_MAX_INSTANCES: This defines how many instances of same version of browser can run over the Remote System.
  • SE_NODE_MAX_SESSIONS: This defines maximum number of concurrent sessions that will be allowed.

Here, as per the configurations in the docker compose file, we would be spinning up 3 browsers in the grid

  1. Google Chrome
  2. Mozilla Firefox
  3. Microsoft Edge

In the docker compose file, I have set 3 service 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, firefox and edge will be connected to the hub.(Check the image of the docker compose file above)

Starting Selenium Grid

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 our docker compose file is located and run the following command:

docker compose -f docker-compose-v3.yml up

Here, docker-compose-v3.yml is the filename. -f options take filename as the parameter.

Once the grid is up and running, we can navigate to http://localhost:4444 and checkout the instances which are up and running as per the configurations we gave in the docker compose file.

Now, if we want to scale up the chrome services and have 3 more instances up and running, we can do this by stopping the current docker compose session and spinning up again using the below command:

docker compose -f docker-compose-v3.yml up --scale chrome=4

Scaling 4 chrome services.

We can see there are total 4 chrome services instances up and running and each service has 4 instances of Chrome Browser, so in total we have 16 chrome browsers up and running and we can take leverage and run 16 parallel chrome sessions using this now!

Amazing!! It is so easy to scale up and use the services.

Here, chromeis the name of the service mentioned in the docker compose file. Likewise, we can do the same for other services in the docker compose file as well. For eg. to scale up firefox we can use the below command:

docker compose -f docker-compose-v3.yml up --scale firefox=4

Stopping the Grid

To stop the grid, we can press ctrl+c in the command prompt/terminal and it will instantly stop the containers. To Stop and remove containers, networks, volumes, and images created by docker compose up we can use the following command:

docker compose -f docker-compose-v3.yml down

Setting up the tests

Grid is up and all the browsers are up and running, the next step is to tweak the code so we could run our tests on selenium grid parallelly.

This is the minimal code setup that would be required to run the tests on selenium grid, here is an example for chrome browser:

Remote WebDriver setup

Note:

Since I am using ThreadLocal hence I have used driver.set() to set the value of the driver. In case of implementation without ThreadLocal we can use driver = new RemoteWebDriver() to initialize the driver.

We need to use RemoteWebDriver class in order to run the tests on selenium grid. We need to pass 2 parameters to the RemoteWebDriver class:

  1. URL to the selenium grid hub.
  2. Capabilities which will help in setting the browser within the grid.

Using DesiredCapabilities class we can setup Browser, Platform and version. In the screenshot image above, I have showed the implementation for Chrome browser. Likewise, we can do the same for firefox and edge browser as well.

Running the Tests

As discussed above in the point 1 of Test Strategy , lets now talk about running the test parallelly in cross browsers:

Running the tests parallel in cross browsers.

We would be using testng to run the tests parallelly.

In thetestng.xml we need to set the value for parallel attribute to tests and also set the thread-count. Now, since, I have 3 tests, I have kept the value of thread-count to 3.

Also, to note here, is the value for parameter attribute which is set to different browser names, remote-chrome , remote-firefox and remote-edge which will run the tests in the respective browsers in grid.

Checkout the image below, this is how the selenium grid dashboard looks when the tests execution gets started. Since we have 4 instances of chrome browsers, 1 is busy executing our tests and for Firefox and edge it has reached it maximum concurrency as we had only 1 instances respectively for those browsers.

Tests running in grid
Browser Sessions in progress.

As mentioned earlier, in selenium grid 4, we have the sessions overview as well. So, when we click on the Session Tab we can see the parallel execution happening live and there are other details as well displayed in the Dashboard like Capabilities, Start time, Duration and Node URI which are useful metrics from the test automation reporting.

Lets now navigate inside the browser sessions to check if it is executing what we actually want.

We need to click on the video icon beside the browser session to watch the session live and it will ask for password here. The password is “secret”

This is how the test execution in Chrome looks like and its actually executing the tests on juice-shop website.

Test Execution in Chrome Browser

Next lets move to Firefox:

Test Execution in Firefox Browser

And this is how it looks in Microsoft Edge:

Test Execution in Microsoft Edge.

Once the tests execution gets completed successfully, the sessions are automatically closed and next tests if any in queue will be picked up for execution.

Running the tests parallel in same browser.

Now, let me show you the other point in Test Strategy where we need to execute the tests parallelly on the same browser.

As mentioned earlier we would be using testng for this as well.

The browser name for all the tests are set to remote-chrome and the value for parallel attribute to tests and also set the thread-count. to 3as we would be executing tests parallelly.

Chrome parallel session

On executing the tests, we can see that 3 sessions are in progress for chrome browser and also the graph for concurrency shows 50% considering the browser sessions available. Out of 6 browser instances available, 3 are in progress, hence the concurrency is shown as 50%.

3 Parallel Chrome Browser sessions running

We can also see the 3 parallel chrome sessions running successfully. As said earlier, we can click on video icons to check the live browser session.

Github Repository

Github — Selenium4poc

All of the code which is showcased above is located in this github repository. Do mark a ⭐ and make the project popular so it reaches to the people who want to learn and upgrade.

Conclusion

In this blog, we discussed about Selenium Grid 4 and how to set it up using docker compose.

We derived a test automation test strategy to run the tests in parallel in cross browsers as well as running the tests parallelly in same browser with multiple instances.

I hope this blog gives you a better understanding of Selenium Grid 4 and practically you can also set up the grid and use it in your test automation.

Please do let me know if you have any queries, I would be happy to help.

Happy Testing!

Paid Trainings

Contact me for Paid trainings related to Test Automation and Software Testing, ping me using my website.

References:

--

--

Mohammad Faisal Khatri

QA with 14+ years of experience in automation as well as manual testing. Freelancer, blogger and open source contributor.