Data-Driven Testing with TestNG and Selenium Webdriver


Data Driven Testing is a software testing method in which test data is put away in table or spreadsheet sheet design. Data driven testing permits testers to enter a solitary test script that can execute tests for all test information from a table and expect the test output in a similar table. It is likewise called table-driven testing or parameterized testing.

Data-Driven Testing with TestNG and Selenium Webdriver

Data-Driven Testing with TestNG and Selenium Webdriver

Also Read: Introduction to Client-side Performance Testing [Explained with examples]

TestNG is a testing framework developed in the lines of JUnit and NUnit, but it presents a few new functionalities that make it all the more impressive and simpler to use. TestNG is intended to cover all classes of tests: unit, functional, end-to-end, integration, etc., and it requires JDK 5 or higher.

Selenium WebDriver is the most important component of Selenium Tool's Suite. The most recent delivery "Selenium 4.0" is incorporated with WebDriver Programming interface which gives an easier and more succinct programming connection point.

The accompanying picture will provide you with a fair comprehension of Selenium components and the Test Automation Tools.

Data-Driven Testing with TestNG and Selenium Webdriver 2


TestNg Data Provider

Data Provider is a strategy utilized for providing the test information to a test technique. Testing the Application with numerous arrangements of data is utilized. The Data Provider idea is accomplished by @DataProvider comment in TestNG.

Data Provider returns a two-layered object to a test strategy. To supply the data for the test technique it tends to be indicated with a attribute “name" or on the other hand in the event that we don't determine the quality "name" then, at that point, the Data Provider name will be same as the relating strategy name.


a) Test Information Supplier with characteristic name


public String[][] testLogin(){
   Object[][] data = new Object[1][2];
   data[0][0] = "";
   data[0][1] = "password";
   return data;


b) Test Information Supplier without attribute name


public String[][] testLogin(){
    Object[][] data = new Object[1][2];
    data[0][0] = "";
    data[0][1] = "password";
    return data;


What are DataProviders in TestNG 

The DataProviders in TestNG are one more method for finishing the boundaries in the assessment capability, the other one being TestNG boundaries. DataProviders pass various qualities to the TestNG Experiment in a solitary execution and as TestNG Explanations. It is a piece of the inbuilt TestNG information driven testing for which TestNG is very well known. DataProviders help in passing the boundaries in various ways. These will be confounding whenever talked about here. Consequently their division into discrete areas.


The TestNG DataProvider is used in the following manner:-

@DataProvider (name = "name_of_dataprovider")
public Object[][] SampleMethod() {
     return new Object [][] {values}
  • After the presentation of this language structure, there are a couple of things that you should to observe prior to composing an experiment.
  • The TestNG DataProvider (the explanation part) contains just a single trait, which is its name. It is consistently a string type in nature. For instance, "name_of_dataprovider", as referenced previously.
  • DataProviders are not proclaimed on top of the capabilities like TestNG boundaries yet have a technique for their own, which in customary talking terms called a dataprovider strategy.
  • On the off chance that the analyzer has not determined the name of the dataprovider, then the technique name turns into the dataprovider name of course.
  • TestNG dataprovider returns a 2d rundown of items.
  • The strategy then, at that point, plays out an information driven test for each worth that you have determined.
  • The DataProvider name calls the DataProvider technique, and in the event that there is no name determined by the analyzer, the DataProvider strategy is the default name utilized in the getting @Test case.



How to Use DataProvider In TestNG 

We will start with a straightforward and basic DataProvider test first. Observe the following code, which contains the @DataProvider

import org.testng.annotations.DataProvider;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Samplepart
@DataProvider (name = "data-provider")
public Object[][] Smaplepart(){
   return new Object[][] {{"First-Value"}, {"Second-Value"}};

@Test (dataProvider = "data-provider")
public void myTest (String val) {
         System.out.println("Passed Parameter Is : " + val);


Please note that you can import DataProvider in TestNG by adding the line import org.testng.annotations.DataProvider as shown in the above example code.

Run the code with TestNG Test and see the result:-

Data-Driven Testing with TestNG and Selenium Webdriver 3



How to Provide Multiple Sets of Data using DataProvider

The beneath code makes sense of an experiment where we test the "Gmail" login Page with numerous arrangements of information utilizing Data provider.

We have two capabilities in this model:-

  • getData()
  • testgmailLogin()

getData() is the DataProvider capability that is conveying test data as a variety of exhibit of objects (object[ ][ ]). The test strategy testgmailLogin() snares onto the DataProvider by pronouncing that its data ought to be provided by the DataProvider named "getData".

testgmailLogin() is the capability, which contains the boundaries for getting data from DataProvider. It can contain conditions, circles, selenium orders, and so on. Here we are having the boundaries that coordinate with an exhibit in DataProvider and furthermore the necessary selenium orders that should be performed on the application.

package DataProvider;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class GmailLogin {
public WebDriver driver;

public void launchApp(){
driver = new ChromeDriver();

@Test(dataProvider = "getData")
public void testGmailLogin(String Username, String Password) throws InterruptedException{
Assert.assertTrue(driver.findElement(By.xpath("//font[text()='SenchaExtJSOverview']")).isDisplayed(),"Login Failed");
System.out.println("Login successful");

public Object[][] getData(){
Object[][] data = new Object[3][2];
data[0][0] = "";
data[0][1] = "password1";
data[1][0] = "";
data[1][1] = "password3";
data[2][0] = "";
data[2][1] = "Testing";
return data;

public void closeBrowser(){


DataProviders With Method As A Parameter

we have utilized one method for giving the DataProvider to another test class, i.e., by making a DataProvider technique for every strategy that will call it. It is okay, however we will superfluously expand the lines of code in the java record, which is viewed as a terrible coding practice. On the off chance that I can do similar occupation for seven lines rather than 10, I ought to take the plunge.

It is the explanation that DataProviders likewise acknowledge a technique as a boundary, and afterward we can simply check the strategy name and give the boundaries as indicated by it. Notice the accompanying code:-

import org.testng.Assert;
import java.lang.reflect.Method;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Samplepart {
           @DataProvider (name = "data-provider")
            public Object[][] dpMethod (Method m){
                      switch (m.getName()) {
                      case "Sum": 
                               return new Object[][] {{3, 5 , 8}, {4, 6, 10}};
                      case "Diff": 
                               return new Object[][] {{3, 5, -2}, {4, 6, -2}};
                      return null;


           @Test (dataProvider = "data-provider")
           public void Sum (int a, int b, int result) {
             int sum = a + b;
             Assert.assertEquals(result, sum);

           @Test (dataProvider = "data-provider")
           public void Diff (int a, int b, int result) {
             int diff = a - b;
             Assert.assertEquals(result, diff);

Run the above code and perceive how the result analyzes:-

Data-Driven Testing with TestNG and Selenium Webdriver 4


Advantages of using Data Driven Framework

  • Further develops test inclusion.
  • Re-ease of use of code.
  • Less upkeep.
  • Quicker Execution.
  • Allows better error handling



In this article we learnt about the Data-Driven framework of Selenium. Also, we learned about how we make use of TestNG’s Data Provider to read data  and pass them to your test methods so that we can test your application with different data without changing the test script.

Leave a Comment