Most Selenium test scripts start with hardcoded test data - usernames, passwords, URLs, and input values written directly into the code. This works fine for small projects, but becomes a nightmare when you need to test with multiple data sets or when non-technical team members want to contribute test cases.
Excel files offer a simple solution. Instead of hardcoding values, you can store your test data in spreadsheets and read it programmatically during test execution. Apache POI is the Java library that makes this possible, providing straightforward methods to extract data from Excel files and use it in your Selenium tests.
This article explains how to integrate Excel data reading and writing into your Selenium automation framework using Apache POI.
Running the same test case with multiple input sets is common in automation. Embedding data in scripts makes maintenance difficult and increases the risk of errors. Using Excel to store test data separates data from test logic, reduces maintenance effort, and allows dynamic execution of multiple scenarios.
Below are key reasons why using Excel data enhances Selenium tests:
Reading data from Excel is essential for data-driven testing, where the same workflow must be validated with multiple input sets. Selenium itself does not provide native support for Excel, so Apache POI, a Java library for reading and writing Microsoft Office files, is commonly used.
Using Apache POI allows testers to extract data from both .xls (HSSF) and .xlsx (XSSF) files efficiently. Below is an explanation followed by practical steps that testers can follow:
Apache POI jars need to be added to the project. For Maven users, include the following dependencies in pom.xml:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
Create a FileInputStream to read the Excel file. This stream feeds into an Apache POI Workbook object. For example:
FileInputStream file = new FileInputStream("TestData.xlsx");
Workbook workbook = WorkbookFactory.create(file);
Sheet sheet = workbook.getSheet("Sheet1");
Use a nested loop to access each row and its cells. Testers can read data as strings, numbers, or booleans depending on the cell type.
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case STRING:
System.out.println(cell.getStringCellValue());
break;
case NUMERIC:
System.out.println(cell.getNumericCellValue());
break;
case BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
}
}
}
Always close the Workbook and FileInputStream to prevent memory leaks:
workbook.close();
file.close();
Writing Excel data allows testers to maintain dynamic logs, track test execution, and feed subsequent test steps with updated values. Apache POI provides straightforward methods to create or update Excel files directly from Selenium scripts.
Below is a structured approach to writing Excel data in Selenium:
To write data, you can either open an existing Excel file or create a new one. For an existing file:
FileInputStream file = new FileInputStream("TestData.xlsx");
Workbook workbook = WorkbookFactory.create(file);
Sheet sheet = workbook.getSheet("Sheet1");
To create a new file:
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Results");
Identify the row and cell where data will be written. Use createRow and createCell for new entries.
Row row = sheet.createRow(0); // First row
Cell cell = row.createCell(0); // First column
cell.setCellValue("Test Passed"); // Writing a string value
Apache POI supports strings, numbers, booleans, and formulas. For example:
row.createCell(1).setCellValue(123); // Numeric value
row.createCell(2).setCellValue(true); // Boolean value
row.createCell(3).setCellFormula("SUM(A1:B1)"); // Formula
Writing data requires a FileOutputStream. Always close resources to prevent file corruption:
FileOutputStream outFile = new FileOutputStream("TestData.xlsx");
workbook.write(outFile);
outFile.close();
workbook.close();
Before Selenium can interact with Excel files, Apache POI must be properly configured in the project. A correct setup ensures that reading and writing operations work reliably across different environments and Excel formats. This section explains how to configure Apache POI for both Maven and non-Maven Java projects and highlights common setup considerations.
For Maven users, include the core POI libraries and the OOXML library for .xlsx support in pom.xml:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
These dependencies handle both .xls and .xlsx files, providing full reading and writing capabilities.
For projects not using Maven, download the required JAR files from the Apache POI website. Include the following in the project’s build path:
Proper inclusion ensures all features, including formulas and styles, work without runtime errors.
Apache POI requires Java 8 or higher. Ensure the project’s JDK matches the POI version requirements to avoid compatibility issues.
Store test data files in a dedicated folder, such as src/test/resources, to maintain clarity and support version control. Access the files using relative paths to ensure portability across environments.
When reading or writing Excel files, wrap operations in try-catch blocks and always close FileInputStream, FileOutputStream, and Workbook objects to prevent memory leaks and file corruption.
Excel files can contain multiple data types, including strings, numbers, booleans, dates, and formulas. Selenium scripts must handle each type correctly to avoid errors during test execution. Apache POI provides methods to detect and process these data types efficiently.
Below is an overview of the main data types and how to handle them in Selenium using Apache POI:
Working with Excel files in Selenium can introduce several challenges that may affect test reliability and maintainability. Being aware of these issues and implementing best practices helps prevent errors and ensures the smooth execution of data-driven tests.
Below are the most common challenges testers encounter and how to address them:
Using Excel files in Selenium enables efficient management of large datasets and dynamic, data-driven test execution. Apache POI allows reading, writing, and handling different data types, ensuring scripts process inputs accurately and remain maintainable across multiple scenarios.
To validate these data-driven tests in real-world conditions, run Selenium scripts on BrowserStack and ensure consistent execution across browsers, versions, and devices. Its real device cloud and Selenium Grid integration allow teams to identify environment-specific issues early and maintain reliable, cross-browser automation suites.
Run Selenium Tests on Cloud
Get visual proof, steps to reproduce and technical logs with one click
Continue reading
Try Bird on your next bug - you’ll love it
“Game changer”
Julie, Head of QA
Try Bird later, from your desktop