Data-driven testing (DDT) is an essential approach in automated testing where test input and output values are read from external data sources. This not only promotes reusability of test scripts but also improves their maintainability and scalability. One common method for implementing DDT in Selenium is using Excel and CSV files as data sources. In this blog, we'll dive into how to set up data-driven tests using Selenium with these popular formats.
The primary benefits of data-driven testing include:
To use Excel files in your Selenium tests, you will need the Apache POI library, which provides capabilities for reading and writing Excel files in Java.
Add Dependencies: Include the Apache POI dependency in your Maven pom.xml
.
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.0.0</version> </dependency>
Create Test Data in Excel: Prepare an Excel sheet (testdata.xlsx
) containing test inputs. For instance, here’s a simple structure for a login test:
Username | Password |
---|---|
user1 | pass1 |
user2 | pass2 |
Read Excel Data in Your Test:
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 ExcelDataReader { public Object[][] getData(String filePath) throws IOException { FileInputStream fileInputStream = new FileInputStream(new File(filePath)); Workbook workbook = new XSSFWorkbook(fileInputStream); Sheet sheet = workbook.getSheetAt(0); int rowCount = sheet.getPhysicalNumberOfRows(); Object[][] data = new Object[rowCount - 1][2]; // Assuming two columns for (int i = 1; i < rowCount; i++) { // start from index 1 to skip header Row row = sheet.getRow(i); data[i - 1][0] = row.getCell(0).getStringCellValue(); // Username data[i - 1][1] = row.getCell(1).getStringCellValue(); // Password } workbook.close(); return data; } }
Creating a Test Class: Use the reading function in your Selenium test class to perform actions based on the retrieved data.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class LoginTest { WebDriver driver = new ChromeDriver(); @DataProvider(name="loginData") public Object[][] fetchData() throws IOException { ExcelDataReader excelReader = new ExcelDataReader(); return excelReader.getData("path/to/testdata.xlsx"); } @Test(dataProvider="loginData") public void testLogin(String username, String password) { driver.get("http://example.com/login"); // Perform login actions with Selenium // e.g., using driver.findElement() to enter username and password // Validate login success/failure } }
CSV files are often easier to handle than Excel, as they are plain text files, which means they also work well across different programming languages. To work with CSV files in Java, the common approach is to use the OpenCSV library.
Add Dependencies: If you wish to use OpenCSV, include it in your pom.xml
.
<dependency> <groupId>com.opencsv</groupId> <artifactId>opencsv</artifactId> <version>5.5.2</version> </dependency>
Create Test Data in CSV: Prepare a CSV file (testdata.csv
) with the same structure:
Username,Password user1,pass1 user2,pass2
Read CSV Data in Your Test:
import com.opencsv.CSVReader; import java.io.FileReader; import java.io.IOException; public class CSVDataReader { public Object[][] getData(String filePath) throws IOException { CSVReader csvReader = new CSVReader(new FileReader(filePath)); List<String[]> records = csvReader.readAll(); Object[][] data = new Object[records.size() - 1][2]; // Excluding header for (int i = 1; i < records.size(); i++) { data[i - 1][0] = records.get(i)[0]; // Username data[i - 1][1] = records.get(i)[1]; // Password } csvReader.close(); return data; } }
Updating the Test Class: Similarly, use the CSV reader in your Selenium test class.
@DataProvider(name="loginDataCSV") public Object[][] fetchCSVData() throws IOException { CSVDataReader csvReader = new CSVDataReader(); return csvReader.getData("path/to/testdata.csv"); } @Test(dataProvider="loginDataCSV") public void testLoginWithCSV(String username, String password) { // Similar implementation as before }
By using the above strategies, you can effectively integrate data-driven testing in your Selenium automation using either Excel or CSV files. You can further enhance your tests by incorporating sophisticated validations and error handling mechanisms to make your automated tests more robust and reliable.
18/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation
18/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation