top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

Data-Driven Testing in Selenium with Excel/CSV Files

May 21, 2025
3 min read

Automated testing is powerful, but when you want to validate the same functionality with multiple sets of input data, hardcoding test cases becomes inefficient and difficult to maintain. That’s where data-driven testing (DDT) comes into play.

In this blog, we’ll explore how to implement data-driven testing in Selenium using Excel and CSV files. Whether you’re validating login credentials, form inputs, or product searches, using external data sources like Excel and CSV makes your test scripts more scalable, readable, and flexible.


What is Data-Driven Testing?


Data-driven testing is a framework in which test data is separated from the test logic. Instead of writing separate tests for each data variation, we use a single test that reads input and expected results from an external source like an Excel sheet or a CSV file.

Benefits of data-driven testing include:

  • Easy scalability with more data sets.

  • Enhanced reusability of test scripts.

  • Simplified maintenance and updates.

  • Better coverage of edge cases and negative testing.


Tools and Libraries Required


Depending on the data source, here are the typical tools you'll use:

  • For Excel: Apache POI (Java)

  • For CSV: OpenCSV (Java) or standard libraries in Python

  • Selenium WebDriver for browser automation


Example 1: Data-Driven Testing Using Excel (Java + Selenium)


Let’s consider a login test that takes multiple username-password pairs from an Excel file.


Step 1: Add Apache POI to Your Maven Project


In your pom.xml:

<dependencies>

  <dependency>

    <groupId>org.apache.poi</groupId>

    <artifactId>poi</artifactId>

    <version>5.2.4</version>

  </dependency>

  <dependency>

    <groupId>org.apache.poi</groupId>

    <artifactId>poi-ooxml</artifactId>

    <version>5.2.4</version>

  </dependency>

</dependencies>


Step 2: Create Excel Reader Utility:


public class ExcelUtil {

    public static Object[][] getTestData(String filePath, String sheetName) throws Exception {

        FileInputStream file = new FileInputStream(new File(filePath));

        Workbook workbook = WorkbookFactory.create(file);

        Sheet sheet = workbook.getSheet(sheetName);


        int rowCount = sheet.getPhysicalNumberOfRows();

        int colCount = sheet.getRow(0).getPhysicalNumberOfCells();


        Object[][] data = new Object[rowCount - 1][colCount];


        for (int i = 1; i < rowCount; i++) {

            for (int j = 0; j < colCount; j++) {

                data[i - 1][j] = sheet.getRow(i).getCell(j).toString();

            }

        }


        workbook.close();

        return data;

    }

}


Step 3: Use TestNG with DataProvider


public class LoginTest {


    @DataProvider(name = "loginData")

    public Object[][] loginDataProvider() throws Exception {

        return ExcelUtil.getTestData("testdata/LoginData.xlsx", "Sheet1");

    }


    @Test(dataProvider = "loginData")

    public void testLogin(String username, String password) {

        WebDriver driver = new ChromeDriver();

        driver.get("https://example.com/login");


        driver.findElement(By.id("user")).sendKeys(username);

        driver.findElement(By.id("pass")).sendKeys(password);

        driver.findElement(By.id("loginButton")).click();


        // Add assertions or checks here

        driver.quit();

    }

}


Example 2: Data-Driven Testing Using CSV (Java + OpenCSV)


Step 1: Add OpenCSV Dependency


<dependency>

    <groupId>com.opencsv</groupId>

    <artifactId>opencsv</artifactId>

    <version>5.7.1</version>

</dependency>


Step 2: CSV Reader Utility


java

public class CSVUtil {

    public static Object[][] getTestData(String filePath) throws Exception {

        CSVReader reader = new CSVReader(new FileReader(filePath));

        List<String[]> allData = reader.readAll();

        Object[][] data = new Object[allData.size() - 1][];

        for (int i = 1; i < allData.size(); i++) {

            data[i - 1] = allData.get(i);

        }

        reader.close();

        return data;

    }

}


Step 3: Integrate with TestNG


@DataProvider(name = "csvData")

public Object[][] csvDataProvider() throws Exception {

    return CSVUtil.getTestData("testdata/LoginData.csv");

}


Best Practices for Data-Driven Testing

  • Keep your test data clean, consistent, and well-formatted.

  • Use separate sheets or files for different test scenarios.

  • Validate your data before feeding it into tests.

  • Avoid hardcoding file paths—use configuration files instead.

  • Store test data in version control if it’s part of the project.


Conclusion

Data-driven testing empowers your Selenium tests to run across a wide range of input scenarios without duplicating code. Whether you choose Excel for its structure or CSV for simplicity, separating data from logic leads to more maintainable and scalable automation suites.

By combining Selenium with Apache POI, OpenCSV, you can build flexible, robust test frameworks that adapt quickly as your testing needs grow.

 
 

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page