Selenium – Parameterization using CSV file

Parameterization of a Selenium test can be accomplished using various data sources such as XML, Database, Spreadsheet, flat file, CSV file, etc.  Among all of these techniques usage of XML, CSV, and spreadsheet are most widely used.  I have already discussed parameterizing Selenium tests using Microsoft Excel.  Let us discuss fetching test data from a CSV file.

CSV is an acronym for Comma-Separated Values.  The CSV file format is used to store data in tabular format and the data is stored in a plain textual format.  Reading and writing into a CSV file is same as reading / writing into a flat file.  There is an OpenCSV API (a very simple csv parser library for Java) which supports reading / writing into a csv file.  This library provides various classes and methods for accessing data in a csv file.

OpenCSV Jar file can be downloaded from Sourceforge – Download OpenCSV

After downloading the Zip file, unzip the file into a directory.  Go to project properties in Eclipse IDE (Right Click the project to which you want to add this library – > Click properties – > Click Java Build Path – > Libraries – > Add External Jar – > Select the unzipped opencsv.jar file)

An import statement should be added as – > import au.com.bytecode.opencsv.*;

Following this we can start utilizing the open source library for csv.

I have also uploaded a csv file to be used as a test data.  Create a csv file with the same data.  Open a notepad copy and paste the data and save as .csv file.  Then the path of this file should be used in the following code snippet. (view the products.csv file at –  products.csv)

package com.selftechy.readdata;

import java.io.FileReader;
import java.io.IOException;
import au.com.bytecode.opencsv.*;

/**
*
*Author - Seetaram Hegde
*
*/
public class CSVdata {

	private static final String FILE_PATH="F:\\Helios-Workspace\\TestData\\products.csv"; // Replace this path with correct path

	public static void main(String[] args) throws IOException {

		CSVReader reader = new CSVReader(new FileReader(FILE_PATH));
		String [] nextLine;
		while ((nextLine = reader.readNext()) != null) {
			System.out.println("ProductID: [" + nextLine[0] + "]\nProductName: [" + nextLine[1] + "]\nSupplierID: [" + nextLine[2] + "]\nCategoryID: [" + nextLine[3] + "]\nQuantityPerUnit: [" + nextLine[4] + "]");
		}

	}
}

 

Try to execute the above code.  This will fetch the data from the csv file and display on the console.

Above code can be utilized to parameterize the data from a csv file into a Selenium test but needs some tweaking.

Comments 8

  • great post. thanks!

  • Hi,

    I’m brand new to Selenium. The above code gives me the error:
    The declared package “com.selftechy.readdata” does not match the expected package “”
    What do I do to correct this please?

    Thanks!

    • Please remove that line and your package name… for example if you have created the class under package com.example.tests then you need to add this at the top. Eclipse by default adds the package at the top of the class created.

  • Alternative:

    package com.binnu.jesudasan;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;

    public class MyTest {
    String pathname;
    public static void openFile(String pathname) throws IOException{
    File file = new File(pathname);
    BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
    String line =null;
    while((line=bufferedReader.readLine())!=null){
    String[] buffer = line.split(“,”);
    System.out.println(buffer[0]+” “+buffer[1]+” “+buffer[2]);
    }
    try {
    bufferedReader.close();

    } catch (Exception e) {
    System.out.println(e);
    }
    }

    public static void main(String[] args) throws IOException {
    openFile(“C:\\Users\\jbinnu\\Desktop\\TestData.csv”);
    }
    }

  • Hi
    My project requires reading data from tables(data generated dynamically) and writing that data in csv file.Is it possible to automate this in selenium?If yes plz give a hint to this

    Thanks
    Rupa

  • following import showing an error ” import au.com.bytecode.opencsv.*; ” exactly where “au” is there

  • I have 5 tests in Testng and have to run those all on different environments. So basically I have to pick a different url and client, but other pages, elements remain same.

    My Question is, Where do I put while loop so that it reads first line and execute all the tests and then read second line and so on.

    I tried initialize CSV reader at class level, before suite, before method etc. but it throws error, it works for a particular test if I am putting insside @Test but that doesn’t resolve the purpose, I couldn’t find any relevant answer on the internet.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.