App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
Home Guide How to use DataProvider in Selenium and TestNG?

How to use DataProvider in Selenium and TestNG?

By Neha Vaidya, Community Contributor -

Automation testing has become a fixture in the software development process. No one wants to devote hours of human effort and time when they can get accurate results with the right test scripts, frameworks, and tools.

This is why it is essential to understand the different aspects of automated testing. This article will briefly deliver how to use a website’s Data Provider in Selenium TestNG.

Note: TestNG comes up with DataProvider to automate the process of providing test cases for execution.

DataProvider helps with data-driven test cases that carry the same methods but can be run multiple times with different data sets. It also helps in providing complex parameters to the test methods.

What is a Data Driven Framework?

A data-driven framework stores the test data in a table or spreadsheet format. It allows automation engineers to use a single test script to execute all test data in the table. In this framework, input values are read from data files and are stored in a variable in test scripts. Data-Driven testing enables the creation of both positive and negative test cases into a single test.

In this test automation framework, input data can be stored in a single or multiple data sources like XLS, XML, CSV, and databases.

Before understanding what Data Provider in Selenium is, let’s first look at parameterization and its two types.

TestNG Parameterization

Parameterization is an execution strategy that runs a test case automatically multiple times with various input values. The test design provides the provision to read data from a file or database rather than hard-coded values.

In TestNG, there are two ways by which to achieve parameterization:

  • With the help of Parameters annotation and TestNG XML file
    @Parameters({“name”, “searchKey”})
  • With the help of DataProvider annotation
    @DataProvider(name= “searchProvider”)

This article will focus on the second method.

DataProvider in TestNG

To provide the test data, first declare a method that returns the data set in the form of a two-dimensional object array Object[][].

  • The first array represents a data set, whereas the second contains parameter values.
  • The DataProvider method can be in the same test class or one of its superclasses.
  • It is also possible to provide a DataProvider in another class but the technique has to be static.

After adding this method, annotate it using @DataProvider to let TestNG know it is a DataProvider method. Additionally, provide a name using the name attribute of the DataProvider annotation. If one hasn’t provided the word, by default, the method’s name will be used for reference.

Parameterization using DataProvider

Entering thousands of web forms using the testing framework is tedious. To ease this process, one needs a different process for executing large datasets in single execution flow. This data-driven concept is achieved through @DataProvider annotation in TestNG framework.

It has only one attribute ‘name’. If one does not specify the name attribute, then the DataProvider’s name will be the same as the corresponding method name. This is how DataProvider eases the task of testing multiple sets of data.

Let’s understand how it works with an example.

In this example, the tester wants to automate the process of entering username and password, and then logging into This Selenium test case should run 2 times with different sets of data (the data provided in the 2D array).

Let’s implement the same and see how it works.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Chrome.ChromeDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderTest {
// this will take data from dataprovider which we created
public void TestChrome(String uname,String password){
System.setProperty("", "Path of the driver");
WebDriver driver = new ChromeDriver();
// Maximize browser
// Load application
// clear email field
// Enter username
// Clear password field
// Enter password
public Object[][] TestDataFeed(){

// Create object array with 2 rows and 2 column- first parameter is row and second is //column
Object [][] twitterdata=new Object[2][2];

// Enter data to row 0 column 0
// Enter data to row 0 column 1
// Enter data to row 1 column 0
// Enter data to row 1 column 0
// return arrayobject to testscript
return twitterdata;

The driver will launch Chrome on executing this program, navigate to the Twitter home page, and enter the specified username and password combinations.

Note: Data Provider in Selenium can also be used to simultaneously enter the combinations of data across multiple websites.

Closing Notes

  • Running Selenium tests using DataProvider and TestNG is an excellent way to speed up test cycles
  • Data Provider in Selenium TestNG establishes more thorough automated testing of websites, and create phenomenal user experiences with minimal time, effort, and resources.
  • It should feature prominently in testing pipelines, as it serves to make testers’ lives infinitely more accessible.

These tests are always best to run on real browsers and devices. With BrowserStack cloud Selenium Grid of 3000+ real browsers and devices, testers can access to run Selenium tests. Sign up, choose the browser-device-OS combination required, and start testing for free.

Run Selenium Tests

Automation Testing Selenium Webdriver

Featured Articles

TestNG vs Cucumber: Core Differences

All about TestNG Listeners in Selenium

App & Browser Testing Made Easy

Seamlessly test across 20,000+ real devices with BrowserStack