Data-Driven Testing Using Excel Files in Selenium + Java
Data-Driven Testing Using Excel Files in Selenium + Java
1. What is Data-Driven Testing?
Data-Driven Testing (DDT) is a testing methodology where test data is separated from test scripts and stored externally — such as in Excel files. The same test case runs multiple times with different sets of input data.
This helps in:
Reducing code duplication
Improving test coverage
Making tests easier to maintain
2. Why Use Excel Files?
Easy to create and manage
Widely used in organizations
Supports large data sets
Compatible with Apache POI (a Java library to read/write Excel files)
3. Tools and Libraries Needed
Selenium WebDriver – For browser automation
Apache POI – To read/write Excel files
TestNG (optional) – To support data providers and test execution
4. Add Apache POI to Your Project
If you're using Maven, add this to your pom.xml:
xml
Copy
Edit
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
5. Sample Excel File
Username Password
user1@test.com pass123
user2@test.com pass456
Save this as TestData.xlsx.
6. Code: Reading Excel Data with Apache POI
java
Copy
Edit
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
public class ExcelUtils {
public static String[][] getExcelData(String filePath, String sheetName) {
String[][] data = null;
try {
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();
data = new String[rowCount - 1][colCount];
for (int i = 1; i < rowCount; i++) {
Row row = sheet.getRow(i);
for (int j = 0; j < colCount; j++) {
Cell cell = row.getCell(j);
data[i - 1][j] = cell.toString();
}
}
workbook.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
}
7. Integrating with TestNG and Selenium
java
Copy
Edit
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
public class LoginTest {
@DataProvider(name = "loginData")
public String[][] getData() {
return ExcelUtils.getExcelData("path/to/TestData.xlsx", "Sheet1");
}
@Test(dataProvider = "loginData")
public void loginTest(String username, String password) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");
driver.findElement(By.name("email")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.id("loginBtn")).click();
// Add assertions here
driver.quit();
}
}
8. Summary
Using Excel with Selenium and Java allows you to build flexible, maintainable, and scalable data-driven tests. By separating the test data, you can:
Reuse test logic
Cover more test scenarios quickly
Manage test input from a familiar interface (Excel)
Learn Selenium JAVA Training in Hyderabad
Read More
How to Handle Web Tables in Selenium WebDriver
Handling Dropdowns and Multiple Windows in Selenium
Using TestNG with Selenium: Why and How
Top 10 Selenium WebDriver Commands Every Beginner Should Know
Visit Our Quality Thought Training in Hyderabad
Comments
Post a Comment