Selenium Grid | Distributed Test Execution

Selenium Grid

We have already seen how to perform cross-browser Testing & parallel test execution in selenium using TestNG. Selenium Grid facilitates in:

  • parallel execution either on the same machine or on different machines.
  • test execution on multiple browsers.
  • segregations of HUB and NODES for distributed test execution.
  • platform independent. It can be implemented on Mac, Windows or any Linux distro.

 

 

Selenium Grid- Overview

 

 

Hub In Selenium Grid

 

There are primarily two components of Selenium Grid; Hub and Node(s). A hub is a primary machine which receives test execution request and is responsible for the distribution of test cases to multiple other machines or itself for execution.

 

We will implement the selenium grid on the same machine.

Same should be Used for multiple machines (with minor change)

 

 

Node(s) in Selenium Grid

 

Any machine where the test execution actually happens is called a node. A node can be any physical machine, remote machine, virtual machine, etc. We can also use the same machine (hub) which can serve as a node as well. It is discussed later in the tutorial.

 

Port numbers are used to define the nodes. We can configure multiple ports on same machine for parallel execution

 

 

Configure The Selenium Grid

 

Perform the following steps to start with the initial configuration:

 

Download Selenium Standalone Server jar

 

Selenium Standalone Server jar file is used to implement RemoteWebDriver which helps in test execution on multiple ports. DesiredCapabilities object is used to define the browser, OS, etc.

Navigate to THIS URL and download the Selenium Standalone Server jar file.

Selenium Grid- configure Selenium Standalone Server jar

Save this jar file to some folder. We will use the jar’s location to configure the selenium grid hub and nodes.

 

Configure The Hub

 

Navigate to the folder where the Selenium Standalone Server jar file is saved. Open the terminal and type following command (I am using Linux, but it should be same for Windows and Mac users).

 

java -jar selenium-server-standalone-3.141.59.jar -role hub

 

Press ENTER key after entering above command. This should register and start the hub. Default port where the hub is registered is 4444.

 

Selenium Grid- start hub

 

 

Verify The Hub Is Registered Successfully

 

Open your browser and navigate to url http://localhost:4444. If the following page is displayed, then the hub is registered successfully.

 

Selenium Grid- hub register

 

Once the nodes are configured, clicking on the console link will open the browser and instance configuration.

 

Configure The Node

 

Open another command prompt (do not close the existing command prompt) in the location where Selenium Standalone Server jar file is saved. Enter the following command to configure a node.

 

java -jar selenium-server-standalone-3.141.59.jar -role webdriver -hub http://localhost:4444/grid/register -port 5555

 

 

Selenium Grid- start node

 

  • to execute the test scripts on a separate machine, replace http://localhost:4444 with the IP address of the desired machine.
  • 5555 is the port# configured for the node. You may change it to any number of your choice. Similarly, a hub’s port number can also be changed.

 

Verify The Node Is Registered Successfully

 

Refresh the http://localhost:4444 page and click on the console link. It should be displayed as follows:

 

Selenium Grid- node register

 

By default, we got 5 instances each of Firefox and Google Chrome browsers. This means we can execute 5 test scripts each at a time on these two browsers.

The number of browser instances can also be configured.

 

Browser Instance Configuration

 

As discussed above, the user can configure the number and type of the browser on a node.

 

Register Node With 1 Firefox Instance

 

To register a node with only 1 Firefox browser instance, use the following command (you may need to terminate the current node registered at port 5555 to reflect the changes) on the folder where selenium standalone server jar is saved.

 

java -jar selenium-server-standalone-3.141.59.jar -role webdriver -hub http://localhost:4444/grid/register -port 5555 -browser browserName=firefox

 

Refresh the console page and only 1 firefox browser instance should be displayed.

 

Selenium Grid- only firefox instance

 

 

Register Node With 1 Firefox and 1 Chrome Instance

 

To add more than 1 instance of the browser, keep adding the -browser browserName=<browserName> parameter. The following command will instantiate 1 instance each of google chrome and Firefox.

 

java -jar selenium-server-standalone-3.141.59.jar -role webdriver -hub http://localhost:4444/grid/register -port 5555 -browser browserName=firefox -browser browserName=chrome

 

Refreshing the console window in the browser should now display 1 instance each for firefox and chrome.

 

Increase The Browser Instance In Selenium Grid

 

So far we have learned how to configure single instances of browser(s). But in real time we need more than 1 browser instances. To achieve this, we will use maxInstances parameter.

 

java -jar selenium-server-standalone-3.141.59.jar -role webdriver -hub http://localhost:4444/grid/register -port 5555 -browser browserName=firefox,maxInstances=2 -browser browserName=chrome,maxInstances=3

 

Just after the browserName parameter, use a comma and then maxInstances=<number of instances> to configure the number of instances for each browser.

Stop the node, paste above command and press ENTER. This should register 2 and 3 instances for firefox and chrome browser respectively. Refresh the console web page and it should display the number of instances configured.

 

Selenium Grid- configure browser instance

 

 

Session In Selenium Grid

 

There is one more property in Selenium Grid, Session. A session defines how many browsers should be opened at a time during execution on a particular node. The default session count is 5. 

From the above image, we have registered 2 and 3 instances of firefox and chrome browsers respectively, hence 5 different test scripts can be executed concurrently.

However, if we change the value of the session to 2, the first two test scripts should get executed concurrently and third test script should be executed after the completion of any one test script. This is beneficial if tests are executed on a machine with low resources.

In the console window, click on the Configuration tab. It should display the default maxSession value as 5.

 

 

 

Update maxSession Value

 

To update the maxSession value, only 1 parameter value is required. It is applicable to the whole node and hence is used only once, unlike maxInstances parameter.

 

java -jar selenium-server-standalone-3.141.59.jar -role webdriver -hub http://localhost:4444/grid/register -port 5555 -browser browserName=firefox,maxInstances=2 -browser browserName=chrome,maxInstances=3 -maxSession 3

 

Hit Enter and refresh the console web page. Configuration tab now should display maxSessions: 3

 

Configure WebDriver for Selenium Grid

 

The following command will be used to configure ChromeDriver in Node:

 

java -Dwebdriver.chrome.driver=/home/dhawal/Downloads/chromedriver/chromedriver -jar selenium-server-standalone-3.141.59.jar -role webdriver -hub http://localhost:4444/grid/register -port 5555 -browser browserName=firefox,maxInstances=2 -browser browserName=chrome,maxInstances=3 -maxSession 3

 

To configure Firefox, use the following command:

 

java -Dwebdriver.chrome.driver=/home/dhawal/Downloads/chromedriver/chromedriver -Dwebdriver.gecko.driver=/home/dhawal/Downloads/chromedriver/geckodriver -jar selenium-server-standalone-3.141.59.jar -role webdriver -hub http://localhost:4444/grid/register -port 5555 -browser browserName=firefox,maxInstances=2 -browser browserName=chrome,maxInstances=3 -maxSession 3

 

The above command will configure the chromedriver and geckodriver respectively.

Configure another node for the same hub. Change only the port number and everything should remain the same. We will use these configurations in the next tutorials where we will write a test script and execute it using the selenium grid.

Author: Dhawal Joshi

A post-graduate in MCA, ISTQB & ITIL certified QA with more than 8 years of experience in QA working with a CMMI Level 5 organization as System Analyst. I started my automation journey with HP UFT(formerly known as QTP) and for the past few years, I am using Selenium for automation. I also have experience in Android Application Development, Java, HTML, and VBScript. When I am not working, I like to spend time with my family, cooking and learning new developments in IT.

1 thought on “Selenium Grid | Distributed Test Execution

Leave a Reply

Your email address will not be published. Required fields are marked *