Data-Driven Testing & Parameterization in Selenium
What is Parameterization?
Parameterization means running the same test logic multiple times with different input values, rather than duplicating the test for each value.
Using TestNG's @DataProvider
@Test(dataProvider = "credentials")
public void login(String user, String pass) {
loginPage.login(user, pass);
}
Using External Data Sources
Data can also be pulled from Excel, CSV, or JSON files using libraries like Apache POI, letting large sets of test data live outside the code entirely.
Why This Matters
Data-driven testing dramatically increases coverage (many input combinations) without multiplying the amount of test code that needs to be maintained.
A Complete DataProvider Example
A data provider is a method that returns rows of test data. Each row becomes one separate test run, and TestNG reports each row individually.
public class LoginTest extends BaseTest {
@DataProvider(name = "credentials")
public Object[][] credentials() {
return new Object[][] {
{"valid_user", "Correct@123", true},
{"valid_user", "wrongPass", false},
{"", "Correct@123", false},
{"locked_user", "Correct@123", false}
};
}
@Test(dataProvider = "credentials")
public void login(String user, String pass, boolean shouldSucceed) {
LoginPage page = new LoginPage(driver);
page.login(user, pass);
Assert.assertEquals(page.isDashboardVisible(), shouldSucceed,
"Unexpected result for user: " + user);
}
}
Adding an expected-result column, like shouldSucceed, lets one test method cover both positive and negative cases. Including the input in the assertion message makes failed rows easy to identify in reports.
Reading Test Data from a CSV File
For larger data sets, keep the values in a file that testers can edit without touching code.
// testdata/login.csv
// user,pass,expected
// valid_user,Correct@123,true
// valid_user,wrongPass,false
@DataProvider(name = "csvLogins")
public Object[][] csvLogins() throws IOException {
List<String> lines = Files.readAllLines(Path.of("testdata/login.csv"));
return lines.stream()
.skip(1) // skip header row
.map(line -> line.split(",", -1))
.map(p -> new Object[] { p[0], p[1], Boolean.parseBoolean(p[2]) })
.toArray(Object[][]::new);
}
For Excel files, Apache POI reads rows the same way. For JSON, a library like Jackson can map each record straight into a small Java class. Data providers can also live in a separate class and be referenced with dataProviderClass, which keeps test classes tidy.
Parameters vs DataProvider
| Feature | @Parameters | @DataProvider |
|---|---|---|
| Source of values | testng.xml | Java code or external files |
| Runs per test | Once, with one value set | Once per data row |
| Best for | Environment settings like browser or base URL | Many input combinations |
Many frameworks use both: @Parameters chooses the browser, while a data provider supplies the inputs.
Common Mistakes and Tips
- Data row column count not matching the method parameters, which makes TestNG fail before the test starts.
- Rows that depend on each other, so one failure breaks the rest. Keep every row independent.
- Storing real passwords in data files; use dedicated test accounts and keep secrets in environment variables.
- Setting
parallel = trueon a data provider while the WebDriver is shared between threads. - Too many near-identical rows; choose data with techniques like boundary value analysis instead of sheer volume.
Designing Good Test Data
The value of data-driven testing depends on the rows you choose. Start with one clearly valid case, then add rows for each boundary, such as the shortest and longest allowed username, and for each type of invalid input: empty values, wrong formats, special characters and leading or trailing spaces. Give every row a short description column so a failed run tells you which scenario broke. Review the data file in code review just like test code, because a wrong expected value in one row can hide a real defect for many releases.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.