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

Get Directions

Comments

Popular posts from this blog

How to Install and Set Up Selenium in Python (Step-by-Step)

Tosca for API Testing: A Step-by-Step Tutorial

Feeling Stuck in Manual Testing? Here’s Why You Should Learn Automation Testing