Learn DataProvider in TestNG

Understand DataProvider in TestNG for efficient Data-Driven Testing. Run Selenium and TestNG tests on real devices and browsers at scale with BrowserStack Automate

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:

Method 1: With the help of Parameters annotation and TestNG XML file

@Parameters({“name”, “searchKey”})

Method 2: With the help of DataProvider annotation

@DataProvider(name= “searchProvider”)

This article will focus on the second method.

DataProvider in TestNG

DataProvider in TestNG allows you to run a test method multiple times with different sets of data. This is useful for data-driven testing, where the same test needs to be executed with different input values to validate various scenarios. It promotes reusability, scalability, and better test coverage, making it an essential tool for automating tests with varying inputs.

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 in TestNG: Example

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.

Talk to an Expert

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 twitter.com. This Selenium test case should run 2 times with different sets of data (the data provided in the 2D array).

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
@Test(dataProvider="testdata")
public void TestChrome(String uname,String password){
System.setProperty("webdriver.chrome.driver", "Path of the driver");
WebDriver driver = new ChromeDriver();
// Maximize browser
driver.manage().window().maximize();
// Load application
driver.get("https://twitter.com/login");
// clear email field
driver.findElement(By.name("session[username_or_email]")).clear();
// Enter username
driver.findElement(By.name("session[username_or_email]")).sendKeys(uname);
// Clear password field
driver.findElement(By.name("session[password]")).clear();
// Enter password
driver.findElement(By.name("session[password]")).sendKeys(password);
}
@DataProvider(name="testdata")
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
twitterdata[0][0]="username1@gmail.com";
// Enter data to row 0 column 1
twitterdata[0][1]="Password1";
// Enter data to row 1 column 0
twitterdata[1][0]="username2@gmail.com";
// Enter data to row 1 column 0
twitterdata[1][1]="Password2";
// 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.

Key Benefits of Using @DataProvider in TestNG

Here are core benefits of using Data Provider in TestNG:

  1. Data-driven testing: Test methods can be run with different data sets, ensuring better test coverage.
  2. Reusability: You can reuse the same @DataProvider across multiple test methods.
  3. Separation of concerns: Keeps the test data separate from the test logic, making tests more maintainable.
  4. Simplified Parameterization: Instead of writing separate test methods for each test case, you can pass different values using a data provider.

Why use BrowserStack Automate for Selenium TestNG Tests?

Here’s why you should use BrowserStack Automate to run Selenium TestNG Tests:

  • Parallel Testing: BrowserStack Automate allows you to run tests simultaneously on various devices and browsers, reducing execution time and providing faster feedback.
  • Real Devices and Browsers: Testing on actual devices and browsers gives a true picture of your app’s performance, unlike emulators. You can access the latest devices without needing to purchase them.
  • Dedicated Dashboard: Automate provides a dashboard to monitor and manage your tests. It displays test results (Pass/Fail/Pending), device and browser details, test duration, screenshots, and more.
  • Custom Reports with Artifacts: Create detailed and customizable reports that include test results, device and browser configurations, video recordings, and screenshots.
  • Seamless CI/CD Integration: Easily integrate with CI/CD tools like Jenkins, TeamCity, and TravisCI, ensuring faster and more reliable application delivery.

BrowserStack Automate Banner

Conclusion

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 3500+ 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

Tags
Automation Testing Selenium Webdriver

Featured Articles

TestNG vs Cucumber: Core Differences

All about TestNG Listeners in Selenium

Automation Tests on Real Devices & Browsers

Seamlessly Run Automation Tests on 3500+ real Devices & Browsers