Selenium with Java: Data-Driven Testing Using MS Excel

In the world of software testing, data-driven testing (DDT) has emerged as a pivotal technique that enhances test coverage, optimizes testing efforts, and ensures robustness. In this blog post, we will delve into data-driven testing using Selenium with Java, specifically focusing on how to utilize MS Excel for managing our test data.

What is Data-Driven Testing?

Data-driven testing is a testing methodology where test scripts are executed using multiple sets of input data. Instead of hardcoding values within the test scripts, we externalize them into data sources like Excel, databases, or CSV files. This allows for efficient management of test cases and makes it easier to validate applications against a variety of data inputs.

Why Use MS Excel?

MS Excel is a popular choice for data-driven testing because:

  • Familiarity: Many testers are comfortable using Excel, making it easy to manage test data.
  • Flexibility: Excel allows for easy manipulation of data, including adding, updating, and deleting rows and columns.
  • Integration: Java libraries such as Apache POI and JExcel can be easily integrated with Selenium to read from and write to Excel files.

Setting Up Your Environment

Before we begin, make sure you have the following set up:

  • Java Development Kit (JDK)
  • Eclipse IDE or any preferred IDE
  • Selenium WebDriver
  • Apache POI library (for working with Excel files)

You can download Apache POI from the Apache POI website.

Sample Excel File

Create an Excel file named TestData.xlsx with the following structure:

Username                        Password

user1                                Pass1

user2                                Pass2

user3                                Pass3


Implementing Data-Driven Testing

  1. Add Required Libraries

    Add the Apache POI and Selenium libraries to your project. You can do this by including the JAR files in your project’s build path.

  2. Create a Java Class

    Create a Java class, say DataDrivenTest, and implement the following code:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.FileInputStream;
import java.io.IOException;

public class DataDrivenTest {
    public static void main(String[] args) throws IOException {
        // Set the path for the ChromeDriver
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Create an instance of WebDriver
        WebDriver driver = new ChromeDriver();

        // Navigate to the login page
        driver.get("http://yourapplicationurl/login");

        // Load the Excel file
        FileInputStream fis = new FileInputStream("TestData.xlsx");
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0);

        // Iterate through each row in the Excel sheet
        for (Row row : sheet) {
            // Get the username and password from the Excel sheet
            String username = row.getCell(0).getStringCellValue();
            String password = row.getCell(1).getStringCellValue();

            // Find the username and password fields and enter data
            driver.findElement(By.id("username")).sendKeys(username);
            driver.findElement(By.id("password")).sendKeys(password);
            driver.findElement(By.id("loginButton")).click();

            // Implement some validation logic here (e.g., checking for success or failure messages)

            // Clear the fields for the next iteration
            driver.findElement(By.id("username")).clear();
            driver.findElement(By.id("password")).clear();
        }

        // Close the workbook and the browser
        workbook.close();
        driver.quit();
    }
}

Explanation of the Code

  1. Setup WebDriver: Set the path for the ChromeDriver and initialize the WebDriver instance.

  2. Load Excel File: Use Apache POI to read the Excel file. The FileInputStream is used to access the file, and the XSSFWorkbook class allows us to work with .xlsx files.

  3. Iterate Through Rows: Loop through each row of the Excel sheet, fetching the username and password from the respective cells.

  4. Web Interactions: Input the retrieved data into the login form fields and click the login button.

  5. Validation Logic: You can add logic to verify if the login was successful based on application-specific conditions.

  6. Cleanup: Close the workbook and quit the WebDriver after the testing is complete.


Followers