Interview #31: Selenium-Java: How to perform data-driven testing with an Excel file?

Data-driven testing is a software testing methodology where test data is driven externally, usually from a file such as an Excel spreadsheet, database, or XML file. Using Selenium WebDriver with Java, we can integrate Excel files for data-driven testing by reading test data from the file and applying it dynamically during test execution. Below is a detailed explanation of how to perform data-driven testing with an Excel file using Selenium Java:

Disclaimer: For QA-Testing Jobs, WhatsApp us @ 91-6232667387

1. Prerequisites

To use Excel files for data-driven testing in Selenium, you need:

  • Apache POI library: This library allows Java programs to interact with Microsoft Excel files (both .xls and .xlsx).
  • Excel file with test data: Prepare an Excel file containing test cases or input data.
  • Java IDE: An IDE like Eclipse or IntelliJ IDEA.
  • Selenium WebDriver library: The core library for browser automation.


2. Steps to Perform Data-Driven Testing with Excel

Step 1: Add Required Dependencies

If you are using Maven, include the following dependencies in your pom.xml file:

<dependencies>
<!-- Selenium WebDriver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.x.x</version>
</dependency>
<!-- Apache POI for Excel handling -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.x.x</version>
</dependency>
</dependencies>

Step 2: Prepare the Excel File

Create an Excel file (TestData.xlsx) with test data. For example:

Article content

Step 3: Write a Utility Class to Read Excel Data

Create a utility class for reading data from the Excel file. Here's an example:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
public class ExcelUtils {
private Workbook workbook;
// Constructor to load the Excel file
public ExcelUtils(String filePath) throws IOException {
FileInputStream fis = new FileInputStream(filePath);
workbook = new XSSFWorkbook(fis);
}
// Method to fetch data from a specific sheet
public String getCellData(int sheetIndex, int rowIndex, int colIndex) {
Sheet sheet = workbook.getSheetAt(sheetIndex);
Row row = sheet.getRow(rowIndex);
Cell cell = row.getCell(colIndex);
if (cell.getCellType() == CellType.STRING) {
return cell.getStringCellValue();
} else if (cell.getCellType() == CellType.NUMERIC) {
return String.valueOf((int) cell.getNumericCellValue());
}
return null;
}
// Method to get total rows in a sheet
public int getRowCount(int sheetIndex) {
return workbook.getSheetAt(sheetIndex).getLastRowNum() + 1;
}
// Close workbook
public void closeWorkbook() throws IOException {
workbook.close();
}
}

Step 4: Integrate Excel Data with Selenium Tests

Now integrate the ExcelUtils utility into your test scripts.

Example Selenium test script:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class DataDrivenTest {
public static void main(String[] args) throws IOException {
// Path to the Excel file
String excelPath = "path/to/TestData.xlsx";
ExcelUtils excelUtils = new ExcelUtils(excelPath);
// Setup WebDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
// Navigate to the login page
driver.get("https://example.com/login");
// Fetch number of rows in Excel
int rowCount = excelUtils.getRowCount(0);
// Iterate through rows of test data
for (int i = 1; i < rowCount; i++) { // Assuming row 0 contains headers
String username = excelUtils.getCellData(0, i, 0);
String password = excelUtils.getCellData(0, i, 1);
// Perform login operation
WebElement usernameField = driver.findElement(By.id("username"));
WebElement passwordField = driver.findElement(By.id("password"));
WebElement loginButton = driver.findElement(By.id("login"));
usernameField.clear();
usernameField.sendKeys(username);
passwordField.clear();
passwordField.sendKeys(password);
loginButton.click();
// Validate login and capture result
// (Add assertions or validations as per requirement)
}
// Close workbook and browser
excelUtils.closeWorkbook();
driver.quit();
}
}

Step 5: Execute the Test

Run the test script. The test will fetch data row by row from the Excel file and execute the test for each set of input values.


3. Advantages of Data-Driven Testing

  • Reusability: Allows a single test script to handle multiple test cases.
  • Scalability: Adding new test data is as simple as updating the Excel file.
  • Separation of Concerns: Test logic is separated from test data, making it easier to manage.


4. Best Practices

  • Use meaningful headers in your Excel files for better readability.
  • Validate data fetched from Excel to avoid runtime errors.
  • Handle file paths dynamically to ensure compatibility across environments.
  • Use assertions or logs to capture the results of each test iteration.

By following these steps, you can efficiently perform data-driven testing using Selenium and Java, leveraging Excel files as the source of test data.

Previous: Interview #30: A critical bug is fixed just before the release date. How would you prioritize your regression testing?

Interview #35: Write a Selenium script that checks for broken links on a webpage.

To check for broken links on a webpage using Selenium, you can follow a systematic approach: Retrieve all anchor (<a>) tags with href ...

Most Popular