How to use DataProvider in Selenium and TestNG?

Related Product On This Page What is a Data Driven Framework?

February 28, 2026 · 5 min read · Tool Comparison
Related Product

How to use DataProvider in Selenium and TestNG?

has become a fixedness in the. No one need to devote hours of human effort and clip when they can get accurate results with the right examination handwriting, frameworks, and tools.

This is why it is indispensable to understand the different aspects of automated testing. This article will briefly present how to use a website & # 8217; s Data Provider in Selenium TestNG.

Note: TestNG arrive up with DataProvider to automate the procedure of providing test event for executing.

DataProviderhelps with data-driven exam cases that carry the like methods but can be run multiple times with different datum set. It besides helps in provide complex parameters to the test methods.

What is a Data Driven Framework?

A stores the test data in a table or spreadsheet format. It allows automation engineers to use a single test script to fulfill all test data in the table. In this fabric, input values are read from information files and are store in a varying in test book. Data-Driven testing enable the creation of both confident and negative test cases into a single tryout.

In this, input data can be stored in a single or multiple data beginning like XLS, XML, CSV, and database.

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

Read More:

TestNG Parameterization

Parameterization is an execution scheme that runs a test causa automatically multiple clip with various input values. The test pattern render the supplying to read data from a file or database sooner than hard-coded values.

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

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

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

Method 2:With the help of DataProvider note

@ DataProvider (name= “ searchProvider ”)

Read More:

This article will centre on the second method.

SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.

DataProvider in TestNG

DataProvider in TestNGallows you to run a test method multiple times with different sets of data. This is useful fordata-driven testing, where the same test motive to be executed with different input value to validate various scenarios. It promotes reusability, scalability, and best trial coverage, create it an essential creature for automating tests with varying inputs.

To provide the test data, first announce a method that render the data set in the variety of a two-dimensional object arrayObject [] [].

  • The inaugural regalia represents a data set, whereas the second contains argument values.
  • The DataProvider method can be in the like test class or one of its superclasses.
  • It is also potential to furnish a DataProvider in another class but the technique has to be stable.

After bestow this method, annotate it using@ DataProviderto let TestNG know it is a DataProvider method.

Additionally, provide a name utilize the name attribute of the DataProvider annotation. If one hasn ’ t furnish the word, by default, the method & # 8217; s name will be used for mention.

Read More:

Parameterization utilize DataProvider in TestNG: Example

Entering G of web pattern using the examination framework is tedious. To ease this summons, one needs a different operation for executing orotund datasets in single execution flowing. This data-driven construct is accomplish through@ DataProviderannotation in.

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

Talk to an Expert

Let ’ s understand how it act with an instance.

In this example, the tester wants to automate the summons of enteringusername and password,and then log intotwitter.com. This should run 2 times with different sets of data (the data provided in the 2D array).

import org.openqa.selenium.By; signification org.openqa.selenium.WebDriver; import org.openqa.selenium.Chrome.ChromeDriver; import org.testng.annotations.DataProvider; significance org.testng.annotations.Test; public class DataProviderTest {// this will take data from dataprovider which we make @ Test (dataProvider= '' testdata '') public void TestChrome (String uname, String parole) {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 battlefield 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 (countersign);} @ DataProvider (name= '' testdata '') public Object [] [] TestDataFeed () {// Create object raiment with 2 rows and 2 column- initiative 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 information to row 0 column 1 twitterdata [0] [1] = '' Password1 ''; // Enter information to row 1 column 0 twitterdata [1] [0] = '' username2 @ gmail.com ''; // Enter data to row 1 column 0 twitterdata [1] [1] = '' Password2 ''; // regress arrayobject to testscript return twitterdata;}}

The driver will establish Chrome on executing this programme, navigate to the Twitter dwelling page, and inscribe the specified username and parole combinations.

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

Key Benefits of Using @ DataProvider in TestNG

Here are core benefits of habituate Data Provider in TestNG:

  1. Data-driven examination: Test method can be run with different datum set, ensuring better tryout reporting.
  2. Reusability: You can reuse the like@ DataProvideracross multiple examination methods.
  3. Separation of fear: Keeps the test data separate from the test logic, making tests more maintainable.
  4. Simplified Parameterization: Instead of writing separate test method for each trial case, you can pass different values using a datum supplier.

Why use BrowserStack Automate for Selenium TestNG Tests?

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

  • : BrowserStack Automate allows you to run tests simultaneously on diverse device and browser, reducing executing clip and providing faster feedback.
  • Real Devices and Browsers: Testing on actual devices and browser gives a true picture of your app ’ s performance, unlike emulators. You can access the latest devices without needing to buy 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 elaborated and customizable story that include test results, device and browser configurations, picture recordings, and screenshots.
  • Seamless CI/CD Integration: Easily integrate with CI/CD creature like Jenkins, TeamCity, and TravisCI, ensuring quicker and more authentic application delivery.

Conclusion

Running Selenium tests employ DataProvider and TestNG is an fantabulous way to speed up test cycles. Data Provider in Selenium TestNG establishes more thorough, and create phenomenal user experience with minimum time, feat, and resources. It should feature prominently in testing line, as it serve to make testers ’ go infinitely more approachable.

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

Tags
96,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free