How to read test automation script parameters from a CSV file

If your test automation scripts are using parameters, the parameters values can be stored in a CSV file.

A CSV parser is needed for reading the content of the file and providing the data to the test automation script.

One of the parsers that can be used is OPEN CSV

First, download the Open CSV jar file:




Then, a
dd this jar file to the project in Eclipse:
  • right click on the project name
  • select Properties
  • select Java Build Path
  • select the Libraries tab
  • click the Add External Jars button
  • browse to the folder where the Open CSV Jar file is saved
  • select the jar file
  • click OK to save the changes





Display the Package Explorer view in Eclipse by going to:


Menu --> Window --> Show View --> Package Explorer

Expand the project in Package Explorer, expand Referenced Libraries and then expand the open CSV jar:



Expand the com.opencsv package.


The package includes multiple classes such as 
  • CSVParser
  • CSVReader
  • CSVWriter
  • CSVIterator


We will use CSVReader for reading the content of a text file.

Expand the CSVReader class and take a look at the methods:



For the purpose of reading the content of the text file, we need:

CSVReader(Reader) constructor
The constructor will create the csv file object.

readAll() method
This method will read the file content and provide it as a list of String arrays.

close()
This method will close the csv reader object.



The text file includes multiple lines of text.

Each line has multiple values separated by -:

keyword-sortorder-resultnumber
java-author-10
oracle-date-20
microsoft-title-15



How does the code looks like?


import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import com.opencsv.CSVReader;

public class Class1 {

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

  String filePath = "c:\\temp\\parameters.txt";

  CSVReader csvSource;
  List<String[]> allLines;  
  
  FileReader fileReader = new FileReader(filePath);

  csvSource = new CSVReader(fileReader);
  allLines = csvSource.readAll();

  for (int lineNo = 0; lineNo < allLines.size(); lineNo++) {
    String[] currentLineArray = allLines.get(lineNumber);
    String currentLine = currentLineArray[0];
    String[] values = currentLine.split("-");

    for (int index = 0; index < values.length; index++)
      System.out.println(values[index]);

  }
 }
}



Lets see how we can apply this code to a WebDriver script.

The following script is very simple:
  • it opens a site (http://www.vpl.ca)
  • executes a keyword search (for the Java keyword)
  • checks that the url of the results page is correct


The parameter to be added to the script corresponds to the Java keyword.

After adding the parameter, we will be able to run the script for all keyword values stored in the text file.


@Test
public void testPage() throws InterruptedException { 

  WebDriver driver = new FirefoxDriver();
  driver.get("http://www.vpl.ca");

  WebElement searchField;
  searchField = driver.findElement(
           By.xpath("//input[@id='globalQuery']"));

  searchField.click();
  searchField.sendKeys("java");

  WebElement searchButton;
  searchButton = driver.findElement(
          By.xpath("//input[@class='search_button']"));

  searchButton.click();

  Thread.sleep(5000);
  String resultsPageUrl = "t=keyword";
  assertTrue("this is not the results page", 
             driver.getCurrentUrl().indexOf(resultsPageUrl) >= 0);

  driver.quit(); 
} 


The test script updated with parameters is below.



import static org.junit.Assert.*;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.opencsv.CSVReader;

@RunWith(value = Parameterized.class)
public class tests {
 
  private String stringValue;
 
  public tests(String stringValue) {
     this.stringValue = stringValue;
  }
     
    @Parameters
    public static Collection< object > data() throws IOException {
  
      /*
       * if the values of the parameters are not read from a file,
       * the data array is created this way.

            Object[][] data = 
               {
                  {"java-author-10"},
                  {"oracle-date-20"},
                  {"microsoft-title-15"}
               };
     
       * if the values of the parameters are read from a file,
       * the data array needs to be populated from the file
     */
  
     String filePath = "c:\\temp\\parameters.txt";
  
     CSVReader csvSource;
     List< string > allLines; 
             
     csvSource = new CSVReader(new FileReader(filePath));
     allLines = csvSource.readAll();
     csvSource.close();
  
     Object[][] data = new Object[allLines.size()][];
  
     for (int lineNumber = 0; lineNumber < allLines.size(); lineNumber++) 
         data[lineNumber] = allLines.get(lineNumber);             
      
     return Arrays.asList(data);       
  }
  
  @Test
  public void testPage() throws InterruptedException {

     String[] values = stringValue.split("-");
     String keyword = values[0];
  
     WebDriver driver;
     driver = new FirefoxDriver();
   
     driver.get("http://www.vpl.ca");
   
     WebElement searchField;
     searchField = driver.findElement(
                By.xpath("//input[@id='globalQuery']"));
   
     searchField.click();
     searchField.sendKeys(keyword);
   
     WebElement searchButton;
     searchButton = driver.findElement(
                By.xpath("//input[@class='search_button']"));   

     searchButton.click();
   
     Thread.sleep(5000);
   
     String resultsPageUrl = "t=keyword";
     assertTrue("this is not the results page", 
                driver.getCurrentUrl().indexOf(resultsPageUrl) >= 0);
   
     driver.quit();
                 
  }
}

Share this