Selenium Grid 2 - Using @DataProvider To Run Parallel WebDriver Tests

In my previous post, We learnt how to execute selenium WebDriver single software automation test in multiple browsers in parallel using Selenium Grid 2. We had used "parameter" tag in testng.xml file to feed browser name in which your software automation test needs to run in parallel. Instead of using @Parameters, We can use @DataProvider annotation method to feed browser names and run selenium WebDriver test in parallel using selenium Grid 2.

If you use @Parameters, You need to write same test multiple times(Depends on number of browsers)  in testng.xml file. Main benefit of using @DataProvider annotation method is you no need to write same software automation test case name multiple times in testng.xml file. You have to write it only once in testng.xml file. @DataProvider annotation method will provide browser details to @Test method. To run test in parallel, you need to set (parallel=true) with @DataProvider annotation. Let's try it with simple example.

My requirement is : I wants to run my selenium software automation test parallel in 3 browsers using selenium grid 2 and testng annotation @DataProvider.

Prerequisites :
  • Selenium Grid 2 hub should be in running mode as described in THIS POST.
  • Selenium Grid 2 node should be registered with hub and in running mode as described in THIS POST.
  • Also IE browser's protected modes should be enabled for all zones as described in THIS POST and zoom level should be set to 100% as described in THIS POST if you are running your test in IE browser too.
I am assuming your grid 2 hub and node are working fine and now this is time to launch software automation test. Write bellow given test in eclipse.

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.
package Grid2;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class fillingForm {

 //Used dataProvider parameter to get data from @DataProvider annotation method.
 //Can accept object array data(browser name, First Name and Last Name) from getNames method.
 @Test(dataProvider = "getNames")
 public void gmailLogin(String browser, String fName, String lName) throws MalformedURLException, InterruptedException {
  //Initialize DesiredCapabilities null.
  DesiredCapabilities cap = null;

  //Initialize browser driver as per data received from getNames().
  if (browser.equals("firefox")) {
   //Set firefox browser capabilities for windows platform.
   cap = DesiredCapabilities.firefox();
  } else if (browser.equals("chrome")) {
   //Set chrome browser capabilities for windows platform.
   cap =;
  } else if (browser.equals("iexplore")) {
   //Set IE browser capabilities for windows platform.
   cap = DesiredCapabilities.internetExplorer();
   cap.setBrowserName("internet explorer");

  //Initialize RemoteWebDriver on grid 2 node with browser capability.
  RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  //Open URL in requested browsers of node and execute test steps.

 //Created @DataProvider annotation method to supply data(browser name, First Name and Last Name) for test
 public Object[][] getNames(){
  Object data[][] = new Object[3][3];
  data[0][0] = "firefox";
  data[0][1] = "FirstName1";
  data[0][2] = "LastName1";
  data[1][0] = "chrome";
  data[1][1] = "FirstName2";
  data[1][2] = "LastName2";
  data[2][0] = "iexplore";
  data[2][1] = "FirstName3";
  data[2][2] = "LastName3";  
  return data;

You can see in above test. We have used @DataProvider annotation method with (parallel=true) to supply data to @Test method.  So no need to set @Parameter in testng.xml file. Use bellow given testng.xml file to run above test.

<suite name="My Test Suite" verbose="2" parallel="tests" thread-count="5">
 <test name="Selenium Grid Test">
   <class name="Grid2.fillingForm" />

Execute above test and verify execution. It will run test in parallel in different three browsers. This way you can use @DataProvider annotation method with (parallel=true) at place of @Parameter to execute test in different browser parallel using selenium grid 2.


  1. Hi Arvind, what is this line of code represents.

    "Object data[][] = new Object[3][3];"

    Is it array? Shall i use String data[][] = new String[3][3]; instead of that line of code?

    1. Yes it's 2 dimensional array it's helpful to write Object so that you can assign any value like String, int , double etc
      Basically it also represents data driven Testing for which we use POI to fetch data from Excel.
      Which reduces number of lines of code.

  2. Is it possible to write @dataprovider in a separate class under one method and that method can be used in multiple classes and can run those classes parallel?