In this post, we are going to perform “Data Driven Test Using CSV File In Selenium WebDriver”. well, It’s a major requirement for any organization to use some sort of data driven approach. Reading test data from CSV file is one most common way in hybrid testing frameworks.
So in this example, let’s read data from CSV file to perform data driven software testing In selenium WebDriver.

In this tutorial, we are going to cover below points:

 

Let’s begin:

1. How to Setup OpenCSV library in a Project

In order to start the implementation of Data driven testing using CSV as your data source, we need to set up the environment first. For this reason, this tutorial will explain and help to set up the prerequisite and required dependencies in Eclipse.

Maven based Project:

If you are using Maven as the build tool for you project, use below mentioned dependency and add it into the POM file:

<dependency>
    <groupId>com.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>5.1</version>
</dependency>

OR

If you are using standalone project then download the OpenCSV jar from this location and add it into your project’s class-path.

 

2. Prepare Test Data CSV

Create a CSV format Test data file that you are going to use in your test. Download TestDataCSV  as this we are using in this example.

 

3. How to implement Data Driven testing using CSV

Now, as you’ve added the dependency into the project and Test data CSV file is ready too, now, use the below code to perform data read through CSV.

import java.io.FileReader;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;

import io.github.bonigarcia.wdm.WebDriverManager;
public class DataDrivenTestingUsingCSVInSelenium {

    //Provide test data CSV file path. As below path based on Mac machine. So, lets say you are using windows machine then write the below path accordingly. 
    String CSV_PATH = "/Users/d33p4k/Documents/Techndeck/JavaPractice/TestData.csv";
    WebDriver driver;
    private CSVReader csvReader;
    String[] csvCell;

    @BeforeTest
    public void setup() throws Exception {

        //You can specify the hardcoded value of a chrome driver or driver based on your browser like below line
        //System.setProperty("webdriver.chrome.driver", "/Users/d33p4k/driver/chromedriver");

        //OR

        //Use below line to manage driver by WebdriverManager for chrome browser in our case (you can use any other driver of your choice)
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://amazon.com");
    }

    @Test
    public void dataRead_CSV() throws IOException, CsvValidationException {
        //Create an object of CSVReader
        csvReader = new CSVReader(new FileReader(CSV_PATH));

        driver.findElement(By.id("nav-link-accountList")).click();
        driver.findElement(By.id("createAccountSubmit")).click();

        //You can use while loop like below, It will be executed until the last line in CSV used. 
        while ((csvCell = csvReader.readNext()) != null) {
            String CustomerName = csvCell[0];
            String CustomerEmail = csvCell[1];
            String CustomerPassword = csvCell[2];
            String CustomerConfirmPassword = csvCell[3];
            driver.findElement(By.id("ap_customer_name")).clear();
            driver.findElement(By.id("ap_customer_name")).sendKeys(CustomerName);
            driver.findElement(By.id("ap_email")).clear();
            driver.findElement(By.id("ap_email")).sendKeys(CustomerEmail);
            driver.findElement(By.id("ap_password")).clear();
            driver.findElement(By.id("ap_password")).sendKeys(CustomerPassword);
            driver.findElement(By.id("ap_password_check")).clear();
            driver.findElement(By.id("ap_password_check")).sendKeys(CustomerConfirmPassword);
            driver.findElement(By.id("continue")).click();
        }
    }

    @AfterTest
    public void exit() {
        driver.close();
        driver.quit();
    }
}

 

Check below video to see the execution in progress:

 

That’s it. So easy to implement it.

Happy Learning!!!

4. Resources

Data Driven Approach – Elemental Selenium

Read Data From CSV In Selenium Webdriver...!!! Share on X

Checkout other useful tutorials, take a look:

Rest Assured Tutorial

Apache HttpClient Tutorial

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts