In real-world automation projects, hardcoding test data inside test scripts makes maintenance difficult. As applications evolve, test data changes frequently, and updating Java code for every change is inefficient.
Data-driven testing solves this problem by separating test logic from test data. Instead of embedding usernames, passwords, and other values directly in your test methods, you store them in external sources such as Excel files, CSV files, or databases.
In this tutorial, you'll learn how to build a data-driven Playwright framework using Java, TestNG, and Excel.
What is Data-Driven Testing?
Data-driven testing is a testing approach where the same test executes multiple times using different sets of input data.
For example, instead of writing separate login tests:
- Login with Admin
- Login with Manager
- Login with Employee
You write one test and supply different credentials from an Excel sheet.
Benefits include:
- Reduced code duplication
- Easier maintenance
- Better test coverage
- Separation of test logic and data
- Improved scalability
Project Structure
A clean folder structure helps keep the framework organized.
playwright-framework
│
├── src
│ ├── main
│ │ ├── pages
│ │ ├── utils
│ │ └── base
│ │
│ └── test
│ ├── tests
│ ├── resources
│ │ └── testdata
│ │ └── LoginData.xlsx
│ └── dataproviders
│
├── pom.xml
└── testng.xmlStep 1: Add Apache POI Dependencies
Apache POI is the most commonly used Java library for reading Excel files.
Add these dependencies to your pom.xml.
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.4.1</version>
</dependency>Refresh your Maven project after saving the file.
Step 2: Create an Excel File
Create an Excel file named LoginData.xlsx.
Example:
| Username | Password |
|---|---|
| admin | Admin@123 |
| manager | Manager@123 |
| employee | Employee@123 |
Store the file under:
src/test/resources/testdataStep 3: Create an Excel Utility Class
Use Apache POI to read Excel data.
public class ExcelUtils {
public static Object[][] getLoginData() throws Exception {
FileInputStream fis =
new FileInputStream(
"src/test/resources/testdata/LoginData.xlsx");
Workbook workbook =
WorkbookFactory.create(fis);
Sheet sheet =
workbook.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
int columns =
sheet.getRow(0).getLastCellNum();
Object[][] data =
new Object[rows - 1][columns];
for (int i = 1; i < rows; i++) {
for (int j = 0; j < columns; j++) {
data[i - 1][j] =
sheet.getRow(i)
.getCell(j)
.toString();
}
}
workbook.close();
fis.close();
return data;
}
}This utility reads the Excel file and returns a two-dimensional array suitable for TestNG.
Step 4: Create a DataProvider
Create a TestNG DataProvider.
@DataProvider(name = "loginData")
public Object[][] loginData()
throws Exception {
return ExcelUtils.getLoginData();
}The DataProvider supplies data to your test methods.
Step 5: Create a Login Page
Example Page Object.
public class LoginPage {
private final Page page;
public LoginPage(Page page) {
this.page = page;
}
public void login(
String username,
String password) {
page.fill("#username", username);
page.fill("#password", password);
page.click("#loginButton");
}
}Step 6: Create the Test Class
Use the DataProvider.
public class LoginTest extends BaseTest {
@Test(dataProvider = "loginData",
dataProviderClass =
LoginDataProvider.class)
public void verifyLogin(
String username,
String password) {
page.navigate(
"https://example.com/login");
LoginPage login =
new LoginPage(page);
login.login(
username,
password);
assertThat(page)
.hasURL(
"https://example.com/dashboard");
}
}TestNG executes the same test once for every row in the Excel sheet.
Execution Flow
Suppose your Excel contains three rows.
| Username | Password |
| admin | Admin@123 |
| manager | Manager@123 |
| employee | Employee@123 |
Execution:
Run 1 → admin
Run 2 → manager
Run 3 → employeeOnly one test method is required.
Handling Different Data Types
Excel may contain:
- Strings
- Numbers
- Dates
- Booleans
Use Apache POI's DataFormatter to safely convert values.
Example:
DataFormatter formatter =
new DataFormatter();
String value =
formatter.formatCellValue(cell);This preserves the displayed cell value and avoids formatting issues.
Parameterizing Different Test Scenarios
Data-driven testing isn't limited to login.
You can store:
- Search keywords
- Product IDs
- Customer details
- Payment information
- API request data
- Form inputs
The same approach can drive a wide variety of tests.
Best Practices
Keep Test Data Separate
Store test data outside Java classes.
Recommended locations:
- Excel
- JSON
- CSV
- YAML
- Database
Avoid Duplicate Data
Use reusable datasets instead of copying values across multiple files.
Name Worksheets Clearly
Good examples:
- LoginData
- SearchData
- PaymentData
Avoid generic names such as:
- Sheet1
- Test
Close Resources
Always close:
- Workbook
- FileInputStream
This prevents memory leaks and file locking issues.
Validate Expected Results
Don't verify only that a test runs.
Validate:
- Successful login
- Error messages
- Dashboard visibility
- Business rules
Common Interview Questions
What is Data-Driven Testing?
A testing approach where one test executes multiple times using different datasets.
Which library is commonly used to read Excel in Java?
Apache POI.
What is the purpose of TestNG's DataProvider?
It supplies multiple sets of data to a single test method, allowing the same test to run repeatedly with different inputs.
Can Playwright execute data-driven tests in parallel?
Yes. TestNG DataProviders can be combined with parallel execution, provided that browser sessions and test data remain thread-safe.
When should you use Excel instead of hardcoded values?
Excel is useful when test data changes frequently, is maintained by non-developers, or must support many test combinations. For API payloads or structured data, JSON may be a better choice.
Conclusion
Data-driven testing is a key technique for building scalable automation frameworks.
By combining Playwright, Java, TestNG, and Apache POI, you can separate test logic from test data, improve maintainability, and increase test coverage without duplicating code.
In this tutorial, you learned how to:
- Read Excel data using Apache POI
- Create a TestNG DataProvider
- Execute Playwright tests with multiple datasets
- Organize a maintainable framework
- Follow best practices for enterprise automation
Once you've mastered data-driven testing, you'll be well prepared to build automation frameworks used in large enterprise applications.