Data driven testing code for selenium with JAVA

September 25, 2023
data driven testing code for selenium java

To perform data-driven testing code for Selenium with Java and read input from an Excel sheet or CSV file, we’ll use Apache POI for Excel or OpenCSV for CSV.

Below is an example of how to perform data-driven testing for product feedback functionality in an e-commerce application using multiple input values from an Excel sheet or a CSV file.

Data-Driven Testing using Excel (Apache POI):
org.apache.poi poi 5.0.0

Example:
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.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ProductFeedbackTest {

public static void main(String[] args) throws IOException {
    WebDriver driver = new ChromeDriver();
    driver.get("your_ecommerce_application_url");

    FileInputStream file = new FileInputStream(new File("path/to/your/excel_file.xlsx"));
    Workbook workbook = new XSSFWorkbook(file);
    Sheet sheet = workbook.getSheetAt(0);

    for (int i = 1; i <= sheet.getLastRowNum(); i++) {
        Row row = sheet.getRow(i);
        String productName = row.getCell(0).getStringCellValue();
        String feedback = row.getCell(1).getStringCellValue();

        // Perform the necessary steps to navigate to the product and submit feedback
        driver.findElement(By.id("product-search")).sendKeys(productName);
        driver.findElement(By.id("submit-search")).click();

        // Fill the feedback form and submit
        driver.findElement(By.id("feedback-text")).sendKeys(feedback);
        driver.findElement(By.id("submit-feedback")).click();

        // Add verification steps based on your application's behavior
    }

    driver.quit();
}

}

Finally, after processing all rows in the Excel sheet, we clean up by quitting the WebDriver, which closes the browser window.

This code demonstrates how to perform data-driven testing in Selenium with Java by reading input from an Excel sheet. You can adapt it to your specific e-commerce application and feedback functionality by adjusting the element locators and verification steps accordingly.

You can customize this script to match the structure of your application and the specifics of your test data. This is just a basic example to demonstrate the concept of data-driven testing code for Selenium with Java.

In the ever-evolving landscape of software testing and quality assurance, staying ahead of the curve is imperative and very necessary. QA Training Hub in Hyderabad offers an exceptional opportunity for the individuals looking to excel in the field of automation testing with Selenium and Java. With its comprehensive Selenium with Java course, students are equipped with the necessary skills and knowledge needed to become proficient automation testers.

If you are interested in learning Selenium Java course, then look no further than QA Training Hub. The course is instructed by an experienced trainer and real time industry expert Mr.Subba Raju. Contact QA Training Hub and become an expert in Selenium and Java.

Leave a Comment