Selenium Grid Tutorial: Setup and Example of Cross Browser Testing

Introduction to Selenium Grid and How to Perform Cross Browser Testing Using Selenium Grid:

We are now close to the end of this comprehensive Selenium tutorials series. Next week, we will conclude this online Selenium Training series with “effort estimation of Selenium Projects” and “Selenium Interview questions and answers” tutorials.

Today, in this tutorial we will introduce you with Selenium Grid –  a distributed test execution environment to speed up the execution of a test pass. Learn how to perform Cross browser testing using Selenium Grid.

Selenium GRID Tutorial

What is the Need of the Selenium Grid?

As you go through entire Selenium WebDriver Tutorials

you will find out WebDriver will execute your test cases on a single machine.

Here are a few problems with such a setup:

  1. What if you want to execute your test cases for different Operating Systems?
  2. How to run your test cases in the different version of the same browser?
  3. How to run your test cases in multiple browsers?
  4. Why should a scenario wait for the execution of other test cases even if it does not depend upon any test cases?

All these problems are addressed in Selenium GRID.

As we proceed with the Selenium course, we will get the idea about how we can overcome these problems. Basically, Grid architecture is based on master-slave architecture. Master machine distributes test cases to different slave machines.

There are 2 versions of Grid available. Selenium Grid 2.0 is the latest from Selenium. Selenium 1.0 was the earlier version. Most of the Selenium experts prefer using Selenium Grid 2.0 as it is packed with new features. Selenium Grid 2.0 supports both Selenium RC and Selenium WebDriver scripts.

Recommended Tools:

#1) CrossBrowserTesting

Easily take your existing Selenium scripts and run them on over 2050 real desktop and mobile browsers. Or check out their Record & Replay feature, allowing you to record a live test and run that recorded test in parallel.

CrossBrowserTesting Logo

#2) LambdaTest

Perform Selenium automation testing on a scalable, secure, and reliable cloud-based Selenium Grid online. Accelerate your Selenium test automation at the same time increase your test coverage by testing on an online infrastructure of 2000+ desktop and mobile browser environments.

Suggested reading =>> Automate Browser Testing with LambdaTest


Benefits Of Selenium Grid

  1. Selenium Grid gives the flexibility to distribute your test cases for execution.
  2. Reduces batch processing time.
  3. Can perform multi-browser testing.
  4. Can perform multi-OS testing.

Basic Terminology Of Selenium Grid:

Hub: Hub is the central point to the entire GRID Architecture which receives all requests. There is only one hub in the selenium grid. Hub distributes the test cases across each node.

Node: There can be multiple nodes in Grid. Tests will run in nodes. Each node communicates with the Hub and performs test assigned to it.

Selenium grid Tutorial 1

Install Selenium GRID

Step 1: Download Selenium Server jar file from Selenium’s official website which is formerly known as Selenium RC Server and save it at any location on the local disk.

URL of selenium HQ:

Step 2: Open the command prompt and navigate to a folder where the server is located. Run the server by using below command

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

The hub will use the port 4444 by default. This port can be changed by passing the different port number in command prompt provided the port is open and has not been assigned a task.

Status can be checked by using the web interface: http://localhost:4444/grid/console

Step 3: Go to the other machine where you intend to setup Nodes. Open the command prompt and run the below line.

java -jar selenium-server-standalone-2.41.0.jar -role node -hub
http://localhost:4444/grid/register -port 5556

Run the selenium server in other machines to start nodes.

Selenium grid Tutorial 2

Browser And Nodes

After starting hub and nodes on each machine when you will navigate to GRID Console

You will find 5 Chrome, 5 Firefox and 1 IE browser under Browser section like below.

Selenium grid Tutorial 3

This indicates that by default you can use 5 Chrome, 5 Firefox and 1 IE browser.

For Instance, if you want to use only IE you can start the node by using below command:

java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
http://localhost:4444/grid/register -port 5556 -browser browserName=iexplore

Verify the browser Type along with other details in GRID Console by clicking on view config.

Selenium grid Tutorial 4

Similarly for Firefox:

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

Selenium grid Tutorial 5

For Chrome:

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

Selenium grid Tutorial 6

There are few scenarios where you may need the browser from each type i.e.: IE, Chrome and Firefox.

For instance, you may need to use 1 IE and 1 Firefox and 1 Chrome browser

Selenium grid Tutorial 7

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


maxInstance is used to limit the number of browser initialization in a node.

For example, if you want to work with 2 Firefox and 2 IE then you can start the node using maxInstance.

java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
http://localhost:4444/grid/register -port 5556 -browser browserName=firefox,maxInstance=3

Maximum instance can be verified under configuration tab.

Selenium grid Tutorial 8

Similarly, other browser instances can be configured using maxInstances.


maxSession is used to configure how many numbers of browsers can be used parallel in the remote system.

java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
http://localhost:4444/grid/register -port 5556 -browser browserName=chrome,maxInstance=3
-browser browserName=firefox,maxInstance=3 –maxSession 3

Similarly, you can start multiple nodes and configuration can be verified in the console.


Selenium grid Tutorial 9


Selenium grid Tutorial 10

Sample Grid Code

Here I have used TestNG to run a sample GRID test case.

Prerequisite: Create Hub and nodes as explained earlier and TestNG should be configured in eclipse.

Here I have taken a sample test to login to Gmail and enter username and password

public class GridExample {
	public void mailTest() throws MalformedURLException{
		DesiredCapabilities dr=null;
		RemoteWebDriver driver=new RemoteWebDriver(new    URL("http://localhost:4444/wd/hub"), dr);
		driver.findElement(By.xpath("//input[@id='Email']")) .sendKeys("username");
		driver.findElement(By.xpath("//input[@id='Passwd']")) .sendKeys("password");

As in the example, you have to use RemoteWebDriver if you are using the GRID and you have to provide capabilities to the browser. You have to set the browser and platform as above.

In this example, I have used the platform as WINDOWS. You can use any platform as per your requirement.

A version of the browser can also be set by using dr.setVersion(“version”)

For Instance, you need to run this test serially in multiple browsers you have to configure your testng.xml.Below is the testng.XML suite for above test to run your test serially.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="GRID SAMPLE TEST" thread-count="2">
	<parameter name ="browserType" value="IE"/>
			<class name ="GridExample"/>
	<parameter name ="browserType" value="firefox"/>
			<class name ="GridExample"/>

To run the test parallel, you have to change your testng.xml like below.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="GRID SAMPLE TEST" parllel="tests" thread-count="3">
	<parameter name ="browserType" value="firefox"/>
			<class name ="GridExample"/>
	<parameter name ="browserType" value="IE"/>
			<class name ="GridExample"/>

Here in the testng.XML you have to specify the parameter as parllel=“tests” and thread-count=“3” describes the maximum number of threads to be executed in parallel.

Configuration Using JSON File

The grid can also be launched along with its configuration by using a JSON configuration file.

Create a JSON file for having below configuration. Here I have created a JSON file named as grid_hub.json

  "host": null,
  "port": 4444,
  "newSessionWaitTimeout": -1,
  "servlets" : [],
  "prioritizer": null,
  "capabilityMatcher":   "org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
  "throwOnCapabilityNotPresent": true,
  "nodePolling": 5000,
  "cleanUpCycle": 5000,
  "timeout": 300000,
  "maxSession": 5

Start the hub using below command

java -jar selenium-server-standalone-2.41.0.jar -role hub –hubConfig grid_hub.json

Similarly, create different json file for different nodes as per required configuration.

Here is an example of JSON configuration file for node named as grid_node.json

          "browserName": "chrome",
          "maxInstances": 2
          "browserName": "firefox",
          "maxInstances": 2
          "browserName": "internet explorer",
          "maxInstances": 1

To start the node

java -jar selenium-server-standalone-2.41.0.jar -role rc –nodeConfig grid_node.json

You can change all the configuration of a browser, maxInstances, port, maxSession etc. in the JSON file.

You can provide browser version, the platform in the JSON config file like below:

   “browserName”: “chrome”,”version”:”8”,”platform”:”Windows”


It is advisable to use Selenium Grid when you have to perform multi-browser testing and you have a large number of test cases.

In this module, we covered how to set up Grid hub and nodes along with how to run Grid test cases using testng.XML and JSON file.

Next Tutorial #30: Automation testing with Selenium and Cucumber tool. Cucumber is a BDD testing tool and Framework. We will learn features of the Cucumber tool and its usage in a real-time scenarios including how to integrate Selenium WebDriver with Cucumber.

Please post your queries related to Selenium Grid in the comments below.

Recommended Reading

37 thoughts on “Selenium Grid Tutorial: Setup and Example of Cross Browser Testing”

  1. Just from experience – Make sure you take care of below point while implementing Grid

    1. While running browser instances on node machine – Thumb rule is 1GB for each browser instance .
    Otherwise you will get error like
    Unable to connect to host on port 7055 after 45000 ms
    As Firefox locks this port. Browser need to be started in 45 second otherwise driver will not be able to initiate new browser.

    2.Make sure driver instance always QUIT . If it keeps running on node ,node will go out of memory.

    3.Standalone server used for node & hub should be same .Its common mistake while upgrading to new version.

  2. I have created 3 class in testng 1. first class is for launching browser & in second class I am calling first class like class signin extends Launch
    & in third class defining in which order that test should run.
    when i created hub & node .on the node it only launching the browser signin test is not there is any changes i have to do in xml file??? please help

  3. Hi,

    Can any one lemme know, can we run the multiple nodes at a time in a same machine.

    I have started the hub and i have mapped a node successfully but when i try to map another node it is saying there is already another node is running.. how ever i can run the node after closing the existing one.

    My query is to execute the script in multiple browsers on a same machine.

    Thanks inadvance!!

    • Hi, When you configure the single node it will work fine. When you trying to configure second node then you must assign port number.
      EX:java – C:/Users/user/Downloads/chromedriver_win32/chromedriver.exe -jar selenium-server-standalone-3.9.1.jar -role node –hub -port 5555

  4. hi,
    above mentioned concept is working at my side. but i am doing database verification testing along with UI testing using selenium. my question is while running selenium script with GRID, how to handle database testing part as in case of “Test of Login” same user will get logged in with multiple browser instances i.e. 3 entries will be posted for same user. in this case how to handle the verification of same activities performed by same user multiple time.


  5. Hi. Where do you keep the TestNG XML file? In the project? I have my test scripts, and I have my hub and nodes set up. I just don’t know where to put the code to kick off the parallel testing.

    • Hi,
      in the project itself.
      Select the project–>right click->select New->click on File from the options.
      enter file name with .xml as extention. EX:abc.xml

      enter the below code for three browsers.

      same this pass the parameter in the code as…
      package myPackage;

      import java.util.Iterator;
      import java.util.Set;

      import org.openqa.selenium.By;
      import org.openqa.selenium.Platform;
      import org.openqa.selenium.firefox.FirefoxOptions;
      import org.openqa.selenium.remote.DesiredCapabilities;
      import org.openqa.selenium.remote.RemoteWebDriver;
      import org.testng.annotations.AfterClass;
      import org.testng.annotations.BeforeTest;
      import org.testng.annotations.Parameters;
      import org.testng.annotations.Test;

      public class BrowserGridTest {

      RemoteWebDriver driver = null;
      public void beforeClass(String myBrowser) throws MalformedURLException {

      if(myBrowser.equals(“internet explorer”)){
      DesiredCapabilities capability = new DesiredCapabilities();
      capability.setBrowserName(“internet explorer”);
      InternetExplorerOptions options2 = new InternetExplorerOptions();
      driver = new RemoteWebDriver(new URL(“http://localhost:4444/wd/hub”), capability);
      // driver.manage().window().maximize();
      else if(myBrowser.equals(“chrome”)){
      DesiredCapabilities capability = new DesiredCapabilities();
      ChromeOptions options = new ChromeOptions();
      driver = new RemoteWebDriver(new URL(“http://localhost:4444/wd/hub”), capability);

      else if(myBrowser.equals(“firefox”)){
      DesiredCapabilities capability = new DesiredCapabilities();
      FirefoxOptions options1 = new FirefoxOptions();
      driver = new RemoteWebDriver(new URL(“http://localhost:4444/wd/hub”), capability);

      public void LoginTest() throws InterruptedException{


      public void afterClass(){


      • Have you really tried executing that?
        @Parameters(“myBrowser”) is that a valid annotation?
        capability.setBrowserName(“internet explorer”); –> This is invalid too

  6. HI ,

    when we started implementing selenium grid, in our application when we put bulk of data it is printing one word one word slowly instead of taking complete at once. Can anyone let me know the issue and give any suggestion to solve it.


  7. hai good evening thank you i think this will enhance my testing knowledge on selenium and will make me strong to get job

  8. Hi I have got an error when i was running the statement in Node machine.
    Exception in thread “main” java.lang.Unsupported ————— Could not find the main class: org.openqa.grid.selenium.Gridlauncher.program

  9. Hello Experts,

    I want to setup the Automation environment for which I have two options, 1. Standalone selenium server and other is Selenium Grid to run the test in parallel.
    I seeking experties help here to know:

    What will be the system requirement for both the environment? (Approx test cases will be 500 plus and max 10 testers going to use it)

    Your suggestion will be highly appriciable.

    Thanks in Advance

  10. For a typical grid node, how many instances can you have running at once?

    I was running 10 instances at a time per node and I was getting OOM errors.

    I am having problems determining how much RAM a particular browser uses

  11. why only one internet explorer browser in grid console by default?

    By default firefox and chrome have 5 but IE present only one browser in console, we didn’t mentioned like this by default

  12. For this line line in class:
    RemoteWebDriver driver=new RemoteWebDriver(new URL(“http://localhost:4444/wd/hub”), dr);

    Error is show in Eclipse. I think I added all the jar files in the Build Path. (I used Selenium-java-2.44.0). Please let me know what is wrong with it.



  13. “browserType” error…How can i solve this error?
    should i create a local variable or access it from testng.xml file please suggest me with some example.

    Thank you

  14. How should i increase the default browser count to more than 5 in chrome / firefox?
    Its not working even though I give maxinstances=20, maxseesions=20.

    Can Someone answer this?

  15. Hi I have frame work like all test classes extends base class and webdriver is instantiated in base class so can I run this scenario through selenium hub ??

  16. Hello all-
    Thanks for a great write up.
    I have the GRID up and running with a HUB and 2 nodes.

    My project is broken down as below:
    TestBase class
    Page classes(all page classes extends to TestBase)
    Test classes(all Test classes extends to TestBase as well)

    I was running my tests on different browsers(firefox, chrome) on MAC earlier.

    Now my goal is to run all of the tests in
    Win7(chrome, IE, firefox)
    Win10(Edge, chrome, firefox)
    Mac(Safari, chrome, firefox)

    Can someone please let me know where to write the GRID code(DesiredCapabilities, RemoteWebDriver) so it can scale for all the different OS/browser combination?

    Your help will be greatly appreciated.


  17. The concept of setting up Test Execution Using Selenium grid is quite easy and simple to learn through this article…Thanks for sharing this article, this is seems to be informative

  18. It was not clear how I perform the execution of my test suite, could you explain by kindness?

    Can I run through Eclipse?

  19. Wow! What an article, I like the way of presenting the information. I am very beginner in this field, article feels me very knowledgeable.

  20. hi , I use that same desired capabilities, but I’m facing an error, pls help me out. thank you.

    My code:
    DesiredCapabilities cap =;
    RemoteWebDriver driversel = new RemoteWebDriver(new URL(nodeid),cap);

    Error message :
    org.openqa.selenium.remote.DesiredCapabilities chrome
    INFO: Using `new ChromeOptions()` is preferred to ``
    org.openqa.selenium.SessionNotCreatedException: Unable to create new service: ChromeDriverService

  21. I have 300+ BDD test cases in written in BDD(In Java). Currently I am running all test cases via Jenkins job where I set single browser selection choice option as parameter. But I want to execute my 300+ test cases on different browsers when job start running daily. E.g 100 on IE, 100 on FF, 100 on Chrome etc.. like this.

    Let me know how I can use selenium grid along with jenkin so I can achieve above mention requirements.



Leave a Comment