Data-Driven Testing in Selenium
π Data-Driven Testing in Selenium (Java)
✅ What is Data-Driven Testing?
Data-Driven Testing (DDT) is a software testing method where test data is stored in external files (like Excel, CSV, or databases), and the same test is run multiple times with different input values.
Purpose: To separate test logic from test data and increase test coverage with minimal code.
π Benefits of Data-Driven Testing
Reduces code duplication
Easy to maintain and update data
Increases test coverage
Makes test cases reusable and flexible
π Common Data Sources in Selenium
Excel files (.xls or .xlsx)
CSV files
Databases (MySQL, SQL Server, etc.)
JSON / XML
Properties files
π§° Tools You Need
Selenium WebDriver
TestNG (for test execution and @DataProvider)
Apache POI (for reading Excel files)
π§ͺ Example: Data-Driven Test with TestNG & DataProvider
java
Copy
Edit
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class LoginTest {
@Test(dataProvider = "loginData")
public void testLogin(String username, String password) {
System.out.println("Testing login with: " + username + " / " + password);
// WebDriver code to perform login
}
@DataProvider(name = "loginData")
public Object[][] getLoginData() {
return new Object[][] {
{"user1", "pass1"},
{"user2", "pass2"},
{"user3", "pass3"}
};
}
}
π₯ Reading Data from Excel using Apache POI
Step 1: Add Apache POI to your pom.xml (Maven):
xml
Copy
Edit
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
Step 2: Create Excel Reader Utility:
java
Copy
Edit
import org.apache.poi.ss.usermodel.*;
import java.io.*;
public class ExcelUtils {
public static Object[][] readExcel(String filePath, String sheetName) throws IOException {
FileInputStream fis = new FileInputStream(filePath);
Workbook workbook = WorkbookFactory.create(fis);
Sheet sheet = workbook.getSheet(sheetName);
int rowCount = sheet.getPhysicalNumberOfRows();
int colCount = sheet.getRow(0).getLastCellNum();
Object[][] data = new Object[rowCount - 1][colCount];
for (int i = 1; i < rowCount; i++) {
Row row = sheet.getRow(i);
for (int j = 0; j < colCount; j++) {
data[i - 1][j] = row.getCell(j).toString();
}
}
workbook.close();
fis.close();
return data;
}
}
Step 3: Use it in your Test Class:
java
Copy
Edit
@DataProvider(name = "excelData")
public Object[][] getExcelData() throws IOException {
return ExcelUtils.readExcel("testdata.xlsx", "Sheet1");
}
π§ When to Use Data-Driven Testing?
Testing login forms with many credentials
Testing forms with various input combinations
Repeating the same test with different conditions
π§© Alternative Tools for DDT
JUnit + CSV/Excel
Cucumber with Examples table
Selenium + TestNG + JSON/XML
✅ Summary
Feature Description
Test Framework TestNG, JUnit
Data Sources Excel, CSV, DB, JSON, XML
Main Benefit Reuse test code with multiple inputs
Popular Tool Apache POI (for Excel)
Learn Testing Tools Training in Hyderabad
Read More
π Advanced Usage in Testing
Building Test Suites in TestNG
Introduction to Robot Framework
Setting Up Playwright for the First Time
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment