Selenium – Parameterization using CSV file

by seetaram on June 19, 2011

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.

{ 7 comments… read them below or add one }

hngo March 20, 2012 at 5:28 pm

great post. thanks!

Reply

mrich May 15, 2012 at 2:15 pm

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!

Reply

seetaram May 18, 2012 at 3:58 pm

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.

Reply

Binnu Jesudasan Gudapati November 19, 2012 at 8:07 am

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”);
}
}

Reply

Rupa April 21, 2013 at 6:44 pm

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

Reply

vali May 4, 2015 at 6:31 am

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

Reply

seetaram October 25, 2015 at 5:16 am

I think the code above needs some library. Add the respective Jar file to build path

Reply

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email