The Talent500 Blog
Docker Integration with Selenium Grid 1

Docker Integration with Selenium Grid

Another word for QA is scalability. Quick feature changes are inevitable in today’s technology that’s why companies have turned to automation testing. Selenium Grid is at the forefront of automation implementation. Read on to see what Docker Integration with Selenium Grid can bring about.

In the present QA world, scalability is the main focus area for many companies. Due to the vast implementation of the Agile model, it is inevitable to go through quick feature changes which result in reduced testing time. To resolve this, automation testing is now taking on the world, and we all know that Selenium is leading in this automation implementation. 

As the testing time has been reduced for multiple releases and frequent feature change, QA teams have decided to find ways on reducing execution time of automation and provide parallel execution for cross browser support. That’s where the use of Selenium Grid and Docker comes into the picture.

Now to perform cross browser testing or running a huge automation suite in parallel to reduce the overall execution time, we need to make sure that execution shouldn’t occur in sequence. We should rather execute them in parallel. The main reason behind it is time. Let’s take an example – if the test suite execution time is 40 minutes, then if we execute in sequence across 3 different browsers, then the total time for entire execution would be 120 minutes. However, if parallel execution is supported with Docker containers, we can save 80 minutes in one test run!

As we have got a basic idea about the importance of Docker and Selenium Grid in automation testing, let’s try to implement the same in the existing test suite.

Docker Integration with Selenium Grid 2

Overview

Let’s first get an overview of Docker and Selenium Grid. 

Docker Overview

Docker is an open platform for developing, shipping and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same way you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

To know more about Docker, please refer to https://docs.docker.com/.

Selenium Grid Overview

Want to run tests in parallel across multiple machines? Then Grid is for you.

Selenium Grid allows the execution of WebDriver scripts on remote machines by routing commands sent by the client to remote browser instances.

Grid aims to:

  • Provide an easy way to run tests in parallel on multiple machines
  • Allow testing on different browser versions
  • Enable cross platform testing

Interested? Go through the following sections to understand how Grid works, and how to set up your own.

How to Integrate Docker with Selenium Grid

Let’s go through the steps to set up Docker with Selenium Grid. The setup shown below has been done on a Windows machine, but the steps in macOS are also quite similar. 

Step 1: Install Docker

Download Docker in Windows: https://docs.docker.com/docker-for-windows/install/

Docker Desktop does not start automatically after installation. To start Docker Desktop, search for Docker, and select Docker Desktop in the search results.
When the whale icon in the status bar stays steady, Docker Desktop is up-and-running, and is accessible from any terminal window.

If the whale icon is hidden in the Notifications area, click the up arrow on the taskbar to show it. To learn more, see Docker Settings. When the initialization is complete, Docker Desktop launches the onboarding tutorial. The tutorial includes a simple exercise to build an example Docker image, run it as a container, push and save the image to Docker Hub.

Download Docker Desktop for Mac from link here (Mac with intel chip):

Step 2: Verify Installation of Docker

Go to CMD and type below command:”docker –version”==>

C:\Users\sshukla>docker –version

O/P: Docker version 19.03.8, build afacb8b

Step 3: Pull Docker Images

Go to below Docker hub link and search for images to pull into your local machine:

https://hub.docker.com/search?q=&type=image

Docker Integration with Selenium Grid 3

Pull all the below three images using Docker cmd:

 

  1. Pull Selenium-hub image using command ~ docker pull selenium/hub
  2. Pull FireFox Debug image using command ~ docker pull selenium/node-firefox-debug
  3. Pull Chrome Debug image using below command ~ docker pull selenium/node-chrome-debug

Step 4: Verify Images

Check that the images that have been pulled in step 4 are present by using command: docker images

C:\Users\sshukla>docker images

OUTPUT:

REPOSITORY      TAG         IMAGE ID        CREATED         SIZE
selenium/standalone-chrome latest          3adc798b45d8    9 months ago    910MB
selenium/node-firefox-debug   latest          86e57cf1c72c    9 months ago    871MB
selenium/node-chrome-debug latest          845cb7d30dec    9 months ago    941MB
selenium/hub              latest          f476afa5404c    9 months ago    263MB
ubuntu                    latest          ccc6e87d482b    13 months ago   64.2MB
ubuntu                    <none>          775349758637    16 months ago   64.2MB
hello-world    latest        fce289e99eb9    2 years ago     1.84kB

Step 5: Running Docker Container

Run below three commands in sequence:

  1. docker run -d -p 4446:4444 –name selenium-hub -P selenium/hub
  2. docker run -d -P –link selenium-hub:hub selenium/node-chrome-debug
  3. docker run -d -P –link selenium-hub:hub selenium/node-firefox-debug

For ease of execution, expect below results while running above three commands:

  1. Let’s execute the first command as mentioned above in the command line:
docker run -d -p 4446:4444 –name selenium-hub -P selenium/hub

Expected output for the executed command would be similar to below:

O/P:3e4bc48fa50f45526e18f635e085caeb3ad548671e8cbcbd5977931a58075193

To confirm that above command executed and container created, use below:

==>C:\Users\sshukla>docker ps

OUTPUT:

CONTAINER ID   IMAGE   COMMAND     CREATED   STATUS   PORTS     NAMES
3e4bc48fa50f   selenium/hub “/opt/bin/entry_poin…”   14 seconds ago Up 11 seconds   0.0.0.0:4446->4444/tcp   selenium-hub

2. Let’s run the next command from the command line:

docker run -d -P –link selenium-hub:hub selenium/node-chrome-debug

When we enter the above command in the cmd, we expect a reply as shown below:

O/P: bb839e51b74373c0a39477134658e93e42256ac15340625cb92fb95255538e70

To confirm that above command executed and container created, use below:

==> C:\Users\sshukla>docker ps

OUTPUT:

CONTAINER ID  IMAGE  COMMAND     CREATED  STATUS  PORTS     NAMES
bb839e51b743    selenium/node-chrome-debug   “/opt/bin/entry_poin…”   8 seconds ago   Up 6 seconds    0.0.0.0:32768->5900/tcp   bold_booth
3e4bc48fa50f    selenium/hub             “/opt/bin/entry_poin…”   2 minutes ago   Up 2 minutes    0.0.0.0:4446->4444/tcp selenium-hub

3. Now let’s move forward and execute the last command:

docker run -d -P –link selenium-hub:hub selenium/node-firefox-debug

Expected output for the above command would be as below:

O/P: e6997998907ae917d392ee3559501d3e2324c5622a33b2be1d75d3f7dcebfcd4

To confirm that above command executed and container created, use below:

==> C:\Users\sshukla>docker ps

OUTPUT:

CONTAINER ID  IMAGE   COMMAND  CREATED   STATUS    PORTS   NAMES
e6997998907a    selenium/node-firefox-debug   “/opt/bin/entry_poin…”   8 seconds ago   Up 8 seconds    0.0.0.0:32769->5900/tcp   vigorous_galois
bb839e51b743    selenium/node-chrome-debug “/opt/bin/entry_poin…”   2 minutes ago   Up 2 minutes    0.0.0.0:32768->5900/tcp   bold_booth
3e4bc48fa50f    selenium/hub              “/opt/bin/entry_poin…”   4 minutes ago   Up 4 minutes    0.0.0.0:4446->4444/tcp selenium-hub

Step 6: Creating Sample Test

Create two tests – one for Chrome and the other for Firefox as shown below. The reason we are creating two tests is because our main intention is to show a scenario where we can run the same test script for both Chrome and Firefox in parallel on two containers using Grid and Docker.

@Test
    public void chrome() throws MalformedURLException, InterruptedException {
  ChromeOptions dcc = new ChromeOptions();
  dcc.setCapability(“browserName”, “chrome”);
  dcc.setCapability(“OS”, “Windows”);
  WebDriver driver = new RemoteWebDriver(new URL(“https://localhost:4446/wd/hub”),dcc);// pass the url of selenium hub
  driver.get(“https://www.google.com”);
  Thread.sleep(5000);
  driver.quit();  
}
@Test

 public void firefox() throws MalformedURLException, InterruptedException {
  FirefoxOptions dcc = new FirefoxOptions();
  dcc.setCapability(“browserName”, “firefox”);
  dcc.setCapability(“OS”, “Windows”);
  WebDriver driver = new RemoteWebDriver(new URL(“https://localhost:4446/wd/hub”),dcc);// pass the url of selenium hub
  driver.get(“https://www.google.com”);
  Thread.sleep(5000);
  driver.quit();   
}

Step 07: TestNG.xml for Parallel Test

Create a testNG.xml file with parallel tests and thread count=2 as we are trying to execute the test in two browsers in parallel.

<?xml version=“1.0” encoding=“UTF-8”?>
<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>
<suite name=“Automation Test Suite “ parallel=“tests” thread-count=“2”>   
    <test name=“Automation Test CasesChrome”>
  <classes>
<class name=“com.task.automation.testtwo.testone”/>
  </classes>
  </test>
  <test name=“Automation Test Casesfirefox”>
  <classes>
<class name=“com.task.automation.testtwo.testfirefox”/>
  </classes>
  </test>
</suite>

Here are the commands to execute the ‘testNG.xml’ file from the command line:

cd F:\Selenium\SampleTestNG
java -cp F:\Selenium\SampleTestNG\lib\*;F:\Selenium\SampleTestNG\bin org.testng.TestNG testng.xml

After executing the above command, it should execute the tests that we have specified in the testNG.xml file. We will be able to see the test running through two different browsers that we have created using Docker containers.

Benefits of Using Docker with Selenium Grid

Docker helps to achieve scalability and easy deployment on multiple servers. Selenium Grid can be implemented using Docker containers, and it can be done in a single machine, or the infrastructure and parallel execution count can be increased easily. One of the most important benefits of Docker is its capacity to scale. It’s particularly helpful because running a Selenium grid on virtual machines or even separate machines takes up a lot of unnecessary computing overhead.

Docker images are based on shared system resources, so they need fewer resources than a virtual machine. Several nodes can be deployed in a single instance. 

If we need to validate our tests on different browsers like Chrome, Firefox and Safari, then creating multiple docker containers and executing them in parallel is the most convenient and feasible way of execution.

Conclusion

In the present world, supporting multiple browsers or multiple mobile devices is very important for a product. But to test with multiple browsers or devices is not cost effective and very time consuming. Docker container integration for Selenium will help overcome both issues of cost and time; it is practically cost-free and with the power of parallel execution, we are able to execute our test scripts on all the required browsers with time use for execution in one browser.

For automation or SDET job roles, it is quite important to learn about the test execution environment setup with Docker containers. The more we are moving towards DevOps, it seems for QA to learn Docker integration is inevitable. If you are looking for jobs in SDET or Automation Testing roles then do check Talent500. You can find multiple opportunities in the jobs link here.

 

 

0
Sidharth Shukla

Sidharth Shukla

Currently working as a SDET. He is an Automation enabler who provides solutions that mitigates quality risk. Passionate about technical writing and contribution towards QA community.

Add comment