Mastering Data-Driven Testing in Selenium with Java explained with structure

 Mastering Data-Driven Testing in Selenium with Java

1. What is Data-Driven Testing?

Data-driven testing (DDT) is a test automation approach where test data is separated from test scripts. Instead of hardcoding values into the test scripts, the test data is stored in external sources such as:

  • Excel files (.xlsx)
  • CSV files
  • Databases (SQL, MongoDB, etc.)
  • JSON or XML files
  • Property files

This approach allows reusability, scalability, and better maintenance.


2. Setting Up Data-Driven Testing in Selenium with Java

To perform DDT in Selenium with Java, you need the following dependencies:

Required Libraries

  1. Selenium WebDriver - For automating web applications.
  2. Apache POI (for Excel) - If using Excel files.
  3. Jackson / Gson (for JSON) - If using JSON.
  4. TestNG or JUnit - For running parameterized tests.

Maven Dependencies

Add these to pom.xml if you're using Maven:

xml

<dependencies> <!-- Selenium WebDriver --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.10.0</version> </dependency> <!-- Apache POI (for Excel files) --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.3</version> </dependency> <!-- Jackson (for JSON handling) --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version> </dependency> <!-- TestNG (for test execution) --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.7.1</version> <scope>test</scope> </dependency> </dependencies>

3. Implementing Data-Driven Testing in Selenium with Java

A. Data-Driven Testing using Excel (Apache POI)

Steps to follow:

  1. Read test data from an Excel file (.xlsx).
  2. Use a utility method to fetch data from Excel.
  3. Pass data dynamically to test cases.

Utility Class for Reading Excel File

java

import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class ExcelUtils { public static String getCellData(String filePath, String sheetName, int row, int col) throws IOException { FileInputStream fis = new FileInputStream(new File(filePath)); Workbook workbook = new XSSFWorkbook(fis); Sheet sheet = workbook.getSheet(sheetName); Cell cell = sheet.getRow(row).getCell(col); String data = cell.toString(); workbook.close(); return data; } }

Selenium Test with Data from Excel

java

import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.io.IOException; public class DataDrivenTest { WebDriver driver; @BeforeClass public void setUp() { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); } @DataProvider(name = "loginData") public Object[][] getData() throws IOException { String filePath = "path/to/excel.xlsx"; return new Object[][] { { ExcelUtils.getCellData(filePath, "Sheet1", 1, 0), ExcelUtils.getCellData(filePath, "Sheet1", 1, 1) }, { ExcelUtils.getCellData(filePath, "Sheet1", 2, 0), ExcelUtils.getCellData(filePath, "Sheet1", 2, 1) } }; } @Test(dataProvider = "loginData") public void loginTest(String username, String password) { driver.get("https://example.com/login"); // Locate username, password fields and login button // driver.findElement(By.id("username")).sendKeys(username); // driver.findElement(By.id("password")).sendKeys(password); // driver.findElement(By.id("loginButton")).click(); } @AfterClass public void tearDown() { driver.quit(); } }

B. Data-Driven Testing using JSON (Jackson Library)

  1. Store test data in a JSON file.
  2. Read data using Jackson.
  3. Pass it to the test.

Sample JSON File (data.json)

json

{ "users": [ { "username": "testuser1", "password": "pass123" }, { "username": "testuser2", "password": "pass456" } ] }

Utility Class for Reading JSON

java

import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; public class JsonUtils { public static JsonNode readJsonFile(String filePath) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.readTree(new File(filePath)); } }

Selenium Test Using JSON Data

java

import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import com.fasterxml.jackson.databind.JsonNode; import java.io.IOException; public class DataDrivenJsonTest { WebDriver driver; @BeforeClass public void setUp() { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); } @DataProvider(name = "jsonData") public Object[][] getData() throws IOException { JsonNode users = JsonUtils.readJsonFile("path/to/data.json").get("users"); Object[][] testData = new Object[users.size()][2]; for (int i = 0; i < users.size(); i++) { testData[i][0] = users.get(i).get("username").asText(); testData[i][1] = users.get(i).get("password").asText(); } return testData; } @Test(dataProvider = "jsonData") public void loginTest(String username, String password) { driver.get("https://example.com/login"); // Locate and interact with elements } @AfterClass public void tearDown() { driver.quit(); } }

4. Best Practices for Data-Driven Testing

Keep test data separate from scripts - Store data in external sources (Excel, JSON, etc.).
Use Data Providers in TestNG - Makes tests reusable.
Implement Utility Classes - Write reusable methods for reading different data sources.
Use Logging - Use Log4j for debugging and logging test execution details.
Handle Exceptions Properly - Always handle file exceptions (IOException, FileNotFoundException).
Avoid Hardcoded File Paths - Use System.getProperty("user.dir") to get dynamic paths.


5. Conclusion

  • Excel + Apache POI: Best for structured tabular test data.
  • JSON + Jackson: Best for API testing and dynamic datasets.
  • TestNG DataProvider: Efficient way to run data-driven tests.
  • Database-Driven: Best for real-time data testing (SQL/MongoDB).

Would you like help integrating a database-driven approach or handling CSV/XML files as well? 🚀

https://otieu.com/4/9433883
https://otieu.com/4/9433883