Saturday, July 9, 2016

Creating array from data in CSV file for Data driven tests in selenium

Two dimensional Array can be created from csv file using the below code to automate workflows in selenium Web-driver using data driven approach. Once data is stored in an array, the code need not to interact with external file again and again but will interact with the array or with data dictionary/hashmap created from the array. 

Code to create an array from CSV file:


package testingtest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

class testclass {

 static String[][] getcsvdatainArray;
    
 public static void main(String[] args) throws IOException {
  // Create an array based on data in a csv file
  testclass tc = new testclass();
  getcsvdatainArray = tc.createarrfromcsvfile("D:/testing.csv");
 }
 
 //get count of rows of data in the csv file
 private int getrowsfromcsv(String strFileName) throws IOException
 {
  InputStream inputfile = new FileInputStream(new File(strFileName));
     BufferedReader bfrReader = new BufferedReader(new InputStreamReader(inputfile));
     int rowscount = 0;
     while ( bfrReader.readLine() != null)
     {
      rowscount++;
     }
     System.out.println(rowscount); 
     bfrReader.close();  
     return rowscount;
 }
 
 //get count of column of data in the csv file
 private int getcolumnsfromcsv(String strFileName) throws IOException
 {
  InputStream inputfile = new FileInputStream(new File(strFileName));
     BufferedReader bfrReader = new BufferedReader(new InputStreamReader(inputfile));
     int columncnt = bfrReader.readLine().split(",").length ;
     bfrReader.close();
     return columncnt;
 }
 
 //create a 2 dimensional string array from the content of csv file
 public String[][] createarrfromcsvfile(String strFileName) throws IOException
 {
  //get rows and column count in the csv file
  int introwcount = getrowsfromcsv(strFileName);
  int intcolcount = getcolumnsfromcsv(strFileName);
  InputStream inputfile = new FileInputStream(new File(strFileName));
     BufferedReader bfrreader = new BufferedReader(new InputStreamReader(inputfile));
     String strrowdta,strrowinfo[];
     int rowcnt = 0;
     //create an array
     String[][]strDta = new String[introwcount][intcolcount];
     //store data in the array
     while ((strrowdta = bfrreader.readLine()) != null)
     {     
      strrowinfo = strrowdta.split(",") ;   
      for (int i = 0;i<strrowinfo.length;i++)
      {
       strDta[rowcnt][i] = strrowinfo[i];
       System.out.println(strDta[rowcnt][i]);
      }
      rowcnt++;     
     }
     bfrreader.close(); 
     return strDta;
 }
}


In another post,code explaining how to read data from a excel file was explained. Click here to understand how to read data from a excel file.

4 comments:

  1. Suppose in each row if the number of entries are different. Like row1 10 entries, row2 12 entries and so on. While storing in 2 dimensional array, we may get arrayindexoutofbound exception. If number of entries are same, then above code works fine. Please let me know whether I am correct are not. Guide me. Thank you..

    ReplyDelete
    Replies
    1. yES it will give the error. The Practical use of this will be in case of a csv file used as the input data, we can have the first row as header row with column names. So it is assumed , the first row will have the max columns. In that case, it will not give error. Once we have data in the array, we can use hashmap to create dictionary with key as the header column value and value as corresponding value from the current row.
      Hope this makes sense.

      Delete
  2. Suppose in each row if the number of entries are different. Like row1 10 entries, row2 12 entries and so on. While storing in 2 dimensional array, we may get arrayindexoutofbound exception. If number of entries are same, then above code works fine. Please let me know whether I am correct are not. Guide me. Thank you..

    ReplyDelete
  3. Existing without the answers to the difficulties you’ve sorted out through this guide is a critical case, as well as the kind which could have badly affected my entire career if I had not discovered your website.
    selenium training in bangalore|

    ReplyDelete