How to read test data from CSV file in Selenium?

After this post, we will achieve extra miles in parameterization in the Selenium project. We have discussed test data read operation from excel sheet, write operation to excel sheet, reading test data from JSON file, writing data to JSON file, reading test data from properties file, writing test data to properties file, parameterization using TestNG @Parameters annotation and @DataProvider annotation and last, we have seen reading data from the database. Today we will discuss reading test data from the CSV file.

We rarely maintain test data inside CSV based file, but, in circumstances like when we need to download data file from some source and it came as CSV file then in such situation we should be aware of the technique to read such data from the CSV.

What is the CSV file?

CSV stands for Comma Separated File. It is a tabular representation of data in a plain text format. Each data is separated by comma sign. Here each line depicts a new row and column is depicted by comma separated text. See image below as the sample data of the CSV.

CSV File Sample

The default CSV is viewable in any spreadsheet application like Microsoft Excel. After creating a CSF file in Notepad, it looks like below image:

CSV File

 

Why do we use a CSV file?

Here are some of the important reasons to use CSV in software lifecycle:

  • It is a plain text file so easy for web developers to create and integrate
  • CSV file is easy to import and export and convertible to any spreadsheet and database as well
  • It is used in most of the leading domains like Banking, Finance, Insurance, etc
  • Easy to understand and implement
  • It is considerably faster, human readable, compact and smaller in size
  • Most of the application of this era use CSV for data exchange
  • We use a CSV as an alternative to Excel sheet

Don’t Miss: How to connect GitHub with Jenkins?

Pre-Requisites

Like Excel sheet reading, we also need to add Jar file in our project to read CSV based test data. So, guys below I have provided maven dependencies as well as direct link to download the jar file.

How to read CSV file in Selenium WebDriver?

Once you have downloaded the jar file then add the jar file as external jar file via build path. You don’t need to add jar file if you have defined maven dependency.

Here is the sample code which reads data of the CSV formatted file.

Sample Code 1

Sample Code 2

Code Explanation

At first, we define the path of the CSV file and using FileReader class we capture all the contents of the CSV format. Next, we create the instance of the CSVReader class and pass an object of the FileReader as an argument inside CSVReader. Further, we create List of the String array in which all the test data are stored. We print the values of the data through iteration. Here is the console output.

Console Output

 CSV File Console Data

 It was all about reading test data from CSV file in Selenium WebDriver. Feel free to ask doubts and don’t forget to join our Facebook group to participate in the discussion.

Join Inviul fb group

Leave a Reply