How To Use Selenium Grid 2 To Run WebDriver Test Cases In Parallel

Main advantage of selenium grid is we can run webdriver software test cases in parallel using selenium grid 2 environment to reduce software test execution time as described in THIS POST. Also It helps us to perform compatibility testing as it is supporting multiple browsers too. Earlier we have configured selenium grid 2 hub in THIS POST and selenium grid nodes in THIS POST in just single machine. Now lets try to use this environment to execute webdriver software automation test cases in parallel.

Prerequisite : Selenium grid hub and node should be running as described in previous steps.

I have created simple software automation test to execute it parallel in multiple browsers using selenium grid. It will launch three browsers (Mozilla firefox, google chrome and IE) parallel and then execute same software automation test inside each browser.

Note : Please remove internet explorer related code stuff from bellow given script if face any related error as it is not very stable with selenium grid.

Create bellow give test in your eclipse project under Grid package.
package Grid;

import static;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class SeGridTest {
 WebDriver driver = null;
 private StringBuffer verificationErrors = new StringBuffer();

 // Pass plateform, browser and url parameters to launch browser and URL in given plateform.
 @Parameters({ "platform", "browser", "url" })
 @BeforeTest(alwaysRun = true)
 public void setup(String platform, String browser, String url) throws MalformedURLException {
  DesiredCapabilities caps = new DesiredCapabilities();
  // Set Platforms based on parameter received from testng.xml.

  // Set browser capability based on parameter received from testng.xml.
  if (browser.equalsIgnoreCase("Internet Explorer"))
   caps = DesiredCapabilities.internetExplorer();
  if (browser.equalsIgnoreCase("Firefox"))
   caps = DesiredCapabilities.firefox();
  if (browser.equalsIgnoreCase("chrome"))
   caps =;

  // Open browser on grid node.
  driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  // Open URL of Application

 // Simple test method to execute.
 @Test(description = "Wait for button enabled")
 public void waitForButtonEnabled() throws InterruptedException {
  WebDriverWait wait = new WebDriverWait(driver, 15);

 // Simple test method to execute.
 @Test(description = "Wait for button enabled", dependsOnMethods = { "waitForButtonEnabled" })
 public void testCalc() throws InterruptedException {
  String result = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");

  // Get Browser name and version.
  Capabilities caps = ((RemoteWebDriver) driver).getCapabilities();
  String browserName = caps.getBrowserName();
  String browserVersion = caps.getVersion();

  // Get OS name.
  String os = System.getProperty("").toLowerCase();

  // Print test result with browser and OS version detail.
  System.out.println("OS = " + os + ", Browser = " + browserName + " "+ browserVersion + " Test Result = " + result);

 public void afterTest() {
  // Close the browser
  String verificationErrorString = verificationErrors.toString();
  if (!"".equals(verificationErrorString)) {

If you remember, We have used parallel in testng.xml file to execute software automation tests in parallel in selenium webdriver as described in THIS POST. We will use same thing here but using selenium grid.

You can see, We have used parameters (plateform, browser and url) with setup method. All these parameters will be served from testnx.xml file as bellow. We have configured three test in bellow given xml file with different browser parameter to run test in different browsers. Create bellow given testng.xml file under your project and run it.

<!--Set thread-count = 3 to execute test parallel in 3 max browsers at at time. You can increase it-->
<suite name="Parallel Tests" verbose="1" thread-count="3" parallel="tests">
     <!--Set test parameters to execute test in IE browser on windows plateform.-->
  <test name="Windows+IE Test" >
    <parameter name="platform" value="Windows" />
    <parameter name="browser" value="Internet Explorer" />
    <parameter name="url" value="" />
    <class name="Grid.SeGridTest" />
  <!--Set test parameters to execute test in Firefox browser on windows plateform.-->
  <test name="Windows+Firefox Test" >
    <parameter name="platform" value="Windows" />
    <parameter name="browser" value="Firefox" />
    <parameter name="url" value="" />
    <class name="Grid.SeGridTest" />
  <!--Set test parameters to execute test in chrome browser on windows plateform.-->
  <test name="Windows+chrome Test" >
    <parameter name="platform" value="Windows" />
    <parameter name="browser" value="chrome" />
    <parameter name="url" value="" />
    <class name="Grid.SeGridTest" />

Now, execute above testng.xml file and observe test execution process. It will  launch three browsers parallel and then execute tests in all three browsers. At the end of test execution, It will print result in console as bellow.

OS = windows 7, Browser = chrome 43.0.2357.132 Test Result = 7
OS = windows 7, Browser = internet explorer 8 Test Result = 7
OS = windows 7, Browser = firefox 38.0.5 Test Result = 7

Testng result will looks like bellow.

This is the way to execute selenium webdriver software automation test in parallel multiple browsers using selenium grid 2 to perform compatibility testing.


  1. Hi, Aravind
    Thank you very much, I am here with lots of QA automation test knowledge from a known-nothing. you are my only true teacher.
    Here I face a problem: I couldn't run the script. So I change localhost to the address of the hub's IP which I get follow a normal way to get PC IP, but it still not works.
    then I found there is an address on my hub command prompt which is a difference one, so I try using it. The scripts run properly, and I get the testng result same as yours here.
    and there are information such as "input[@id='5']]]" showed in the node command prompt, the end is "Done: [delete seesion:2cb1cc4f-...".
    I don't know whether or not it means that running webdriver in grid is successfully.



  2. what should we use for parallel testing?
    if Selenium Grid then why not TestNG and vice versa.
    what are the pros and cons? what is the basic difference between both of them?

    1. In TestNG,
      - Parallel execution of tests is possible between different browsers of single platform machine.
      - Application can be tested only with current available browser versions.
      - At the same time, you can not run your tests on multiple platforms (windows, mac, linux..etc) on different machines.

      In selenium grid,
      - You can run your tests in parallel on different platforms, different versions of same browser & different browsers.

      Hope now you understand the basic difference between them.

  3. Can we run test cases in grid without using testng.