Data Driven Test Using CSV File In Selenium WebDriver

Earlier we learnt how to perform data driven software automation testing using Java Excel API to read data from excel file In THIS POST. We have also used Apache POI API to read data from excel file In data driven software automation framework creation. Steps to create data driven framework are given on THIS PAGE. Now let’s

read data from CSV file to perform data driven software testing In selenium WebDriver. Bellow given steps will show you how to read data from from CSV file.

Download required files
We need opencsv-3.2.jar file to read data from CSV file and I have prepared sample CSV data file (Detail.csv) to use It In bellow given example.
  1. DOWNLOAD and extract zip folder to get both above files. 
  2. Add “opencsv-3.2.jar” file In your project’s build path In eclipse. You can learn how to add external jar files In project’s build path In THIS POST.
  3. Copy paste “Detail.csv” file In D: drive.
“Detail.csv” file have some data and we will use that data In out test. Main thing to learn here Is how to read that data from CSV file.
Now run bellow given example In Eclipse. It will read data from CSV file and use It In webdriver software automation test case to fill form fields.

package Testing_Pack;

import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.opencsv.CSVReader;

public class CSVRead {

 //Provide CSV file path. It Is In D: Drive.
 String CSV_PATH="D:\Detail.csv";
 WebDriver driver;

 
 @BeforeTest
 public void setup() throws Exception {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.com/2014/05/form.html");
 }
 
 @Test
 public void csvDataRead() throws IOException{
  
  CSVReader reader = new CSVReader(new FileReader(CSV_PATH));
  String [] csvCell;
  //while loop will be executed till the last line In CSV.
  while ((csvCell = reader.readNext()) != null) {   
   String FName = csvCell[0];
   String LName = csvCell[1];
   String Email = csvCell[2];
   String Mob = csvCell[3];
   String company = csvCell[4];
   driver.findElement(By.xpath("//input[@name='FirstName']")).sendKeys(FName);
   driver.findElement(By.xpath("//input[@name='LastName']")).sendKeys(LName);
   driver.findElement(By.xpath("//input[@name='EmailID']")).sendKeys(Email);
   driver.findElement(By.xpath("//input[@name='MobNo']")).sendKeys(Mob);
   driver.findElement(By.xpath("//input[@name='Company']")).sendKeys(company);
   driver.findElement(By.xpath("//input[@value='Submit']")).click();
   driver.switchTo().alert().accept();
  }  
 }
}

9 thoughts on “Data Driven Test Using CSV File In Selenium WebDriver

  1. HI Aravind G,

    Thanks for the read data from csv tutorial.
    Can you provide example how to write test results to csv as well?
    Also like excel data driven framework you shown in this blog, how to set case to run, test to run in csv?

    Again, thanks for your good works.

  2. Very helpful. I also have the same question as @Christo have. How can we export the report for result of every execution of CSV file. I am printing it in Console but I want it in any output file format. Test output of TestNG are also not helping as they show the summary as a whole execution. Please suggest

  3. Very helpful. I also have the same question as @Christo have. How can we export the report for result of every execution of CSV file. I am printing it in Console but I want it in any output file format. Test output of TestNG are also not helping as they show the summary as a whole execution. Please suggest

  4. Very helpful. I also have the same question as @Christo have. How can we export the report for result of every execution of CSV file. I am printing it in Console but I want it in any output file format. Test output of TestNG are also not helping as they show the summary as a whole execution. Please suggest

  5. Hi, I have few questions, How can we skip the header row when we are reading CSV and I see that for my test its entering user name from all the rows one by one and similarly password from all the rows, how can it be done one by one ? like login once and then again load another url and login etc.

  6. Hi, I have few questions, How can we skip the header row when we are reading CSV and I see that for my test its entering user name from all the rows one by one and similarly password from all the rows, how can it be done one by one ? like login once and then again load another url and login etc.

  7. Your reader variable is an iterable, by looping over it you retrieve the rows.

    To make it skip one item before your loop, simply call next(reader, None) and ignore the return value.

    You can also simplify your code a little; use the opened files as context managers to have them closed automatically:

    with open("tmob_notcleaned.csv", "rb") as infile, open("tmob_cleaned.csv", "wb") as outfile:
    reader = csv.reader(infile)
    next(reader, None) # skip the headers
    writer = csv.writer(outfile)
    for row in reader:
    # process each row
    writer.writerow(row)

    # no need to close, the files are closed automatically when you get to this point.
    If you wanted to write the header to the output file unprocessed, that's easy too, pass the output of next() to writer.writerow():

    headers = next(reader, None) # returns the headers or `None` if the input is empty
    if headers:
    writer.writerow(headers)

Leave a Reply

Your email address will not be published. Required fields are marked *