Open In App

How to Use DataProvider in TestNG Selenium?

Last Updated : 01 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Selenium-based automation testing, there are scenarios where you need to run the same test multiple times with different sets of input data, where TestNG's DataProvider feature is used. This improves test efficiency, reduces redundancy in your code, and makes tests more comprehensive.

Here are the Steps to use DataProvider in TestNG Selenium:

Step 1: Set Up Your Project

Make sure you have the necessary dependencies for TestNG and Selenium in your project. If you are using Maven, your pom.xml should include:

XML
<dependencies>
    <!-- TestNG dependency -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>
    <!-- Selenium dependency -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.0.0</version>
    </dependency>
</dependencies>

Step 2: Create the DataProvider Class.

After Installing dependency we need to create a class where the DataProvider is defined. This class will provide the test data to the test methods.

DataProviderExample.java

Java
package io.learn.datadriven;

import org.testng.annotations.DataProvider;

public class DataProviderExample {

    @DataProvider(name = "loginData")
    public Object[][] provideLoginData() {
        return new Object[][] {
            { "standard_user", "secret_sauce" },
            { "locked_out_user", "secret_sauce" },
            { "problem_user", "secret_sauce" }
        };
    }
}

In this:

  • The @DataProvider annotation is used to define a method (provideLoginData) that provides the test data.
  • This method returns a 2D Object array where each row represents a different set of input data (username and password).

Step 3: Create the Selenium Test Class.

Create a test class where the actual login test will be performed. The test will use the loginData provided by the DataProvider.

LoginTest.java

Java
package io.learn.datadriven;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class LoginTest {

    WebDriver driver;

    @BeforeMethod
    public void setUp() {
        // Initialize the WebDriver (make sure the path to chromedriver is correct)
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Change the path of ChromeWebDriver\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.saucedemo.com/"); // URL of the login page
    }

    @Test(dataProvider = "loginData", dataProviderClass = DataProviderExample.class)
    public void testLogin(String username, String password) {
        // Perform login test
        WebElement usernameField = driver.findElement(By.id("user-name"));
        WebElement passwordField = driver.findElement(By.id("password"));
        WebElement loginButton = driver.findElement(By.id("login-button"));

        usernameField.clear(); // Clear any existing text
        passwordField.clear(); // Clear any existing text

        usernameField.sendKeys(username);
        passwordField.sendKeys(password);
        loginButton.click();

        System.out.println("The Datadriven test executed successfully");
    }

    @AfterMethod
    public void tearDown() {
        driver.quit();
    }
}

Here we did:

  • @BeforeMethod: Sets up the WebDriver and navigates to the SauceDemo login page before each test.
  • @Test(dataProvider = "loginData", dataProviderClass = DataProviderExample.class): Runs the testLogin method multiple times using data sets from the loginData DataProvider.
  • testLogin(String username, String password): Performs login by entering the provided username and password into the login page fields.
  • @AfterMethod: Closes the browser after each test to clean up resources.

Step 3: Running the Test

To run the tests, use the following command in the terminal or through your IDE:

mvn test

If you're running the tests from an IDE, just click Run. You will see that the test executes three times with different credentials

Output:

Output-of-Selenium-DataProvider-Example
Output of Selenium DataProvider Example

DataProvider in TestNG with Selenium simplifies parameterized testing by running tests with multiple data sets, like different login credentials. It reduces code duplication, enhances test coverage, and makes tests more maintainable and scalable.


TestNG Data Provider
Visit Course explore course icon

Similar Reads