Parameterization of Selenium Tests with CSV file – Part II

In the previous post (view post)I have explained about reading a csv file and displaying its data on console.  Now, let us explore how to store some parameters in the csv file in the Key – Value format and then read the data into a HashMap.  Once the parameters and values are read into a HashMap how to use them in a Selenium test.

HashMap is a Java class (java.util.hashmap), which is fast and easy to use.  Hashmap stores the data in key value pairs.

In the following example, I have automated a scenario which does the following steps:

  1. Open browser
  2. Navigate to http://www.google.co.in
  3. Search for the word “selftechy”

In a csv file I have stored two keys AppURL & SearchVals and their respective values http://www.google.co.in & selftechy

Visit the same file at – SearchData.csv

Copy the data from the above as it is and then save the file as SearchData.csv

Let us have a look at the following code snippet. 

Create two java classes: CsvTestData.java and SearchResults.java

Copy the below code (first one) into CsvTestData.java and next one into the SearchResults.java

package com.selftechy.readdata;

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

/**
*
*Author - Seetaram Hegde
*
*/

public class CsvTestData {
	private static String FILE_PATH;
	
	HashMap<String, String> parametervals = new HashMap<String, String>();
	
	public CsvTestData(String filepath){
		FILE_PATH=filepath;
	}
	
	public String getFilePath(){
		return FILE_PATH;
	}
	
	public HashMap<String, String> readcsvData()throws IOException {
		CSVReader reader = new CSVReader(new FileReader(FILE_PATH));
		String [] nextLine;
		int i=0;
		while ((nextLine = reader.readNext()) != null) {
			parametervals.put(nextLine[i], nextLine[i+1]);
			i++;
		}
		return parametervals;
	}
}

 

package com.selftechy.seleniumtests;

/**
*
*Author - Seetaram Hegde
*
*/

import java.util.HashMap;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
import com.selftechy.readdata.*;


public class SearchResults extends SeleneseTestCase {
	private static SeleniumServer seleniumServer;
	HashMap<String, String> params = new HashMap<String, String>();

	//path to the below constructor should be the path of the "csv" file
	CsvTestData td = new CsvTestData("F:\\Helios-Workspace\\TestData\\SearchData.csv");
	@Before
	public void setUp() throws Exception {
		params.putAll(td.readcsvData());
		selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in/");
        seleniumServer = new SeleniumServer();
        seleniumServer.start();
		selenium.start();
	}

	@Test
	public void testAdvancedSearch() throws Exception {
		selenium.open(params.get("AppURL"));
		selenium.click("link=Advanced search");
		selenium.waitForPageToLoad("30000");
		selenium.type("as_q", params.get("SearchVals"));
		selenium.select("num", "label=20 results");
		selenium.click("//input[@value='Advanced Search']");
		selenium.waitForPageToLoad("30000");
	}

	@After
	public void tearDown() throws Exception {
		selenium.stop();
		seleniumServer.stop();
	}
}

 

Once both the Java classes are created, right click on the second java class and Run As – > JUnit Test

The above example demonstrates how to use csv file for parameterization of Selenium Tests.

Comments 3

  • I have tried to create both classes and I obtained the following exception after running it: java.lang.ArrayIndexOutOfBoundsException: 2

    Any idea will be welcome. Thanks in advance.

    • First we need to understand the Exception – ArrayIndexOutOfBoundsException here. The reason for this exception is in the code we are trying to place something in the array but there is no space allocated. Have a look at the following code:

      package com.selftechy.commons;

      public class Arrays {

      /**
      * Author – Seetaram
      * Copyrights – All rights reserved
      */
      public static void main(String[] args) {
      // TODO Auto-generated method stub

      int[] intArray = new int[5];

      for (int i=0;i<5;i++){
      intArray[i]=i+1;
      }

      for (int i=0;i<5;i++){
      System.out.println(intArray[i]+"\n");
      }
      }

      }
      in the first "for loop" change the value 5 to 10 (i<5 to i<10) and execute, you will get the same "java.lang.ArrayIndexOutOfBoundsException:". This clearly shows that declared array is having space for only so many (might be some number as declared) array elements beyond that JVM will throw the exception saying array is out of bound. In the code you are executing make sure that array does not go beyond what is declared. Try to debug the code step by step using F5 and F6 if the IDE is Eclipse and I am sure that you will be able to find out the problem in the code.

      Thanks,
      Seetaram

  • I had the same problem, java.lang.ArrayIndexOutOfBoundsException: 2. The problem was in the first class, CsvTestData.java. When it reads in the line:

    while ((nextLine = reader.readNext()) != null) {
    parametervals.put(nextLine[i], nextLine[i+1]);
    i++;
    }

    the nextline[i] should be nextLine[0], and nextLine[i+1] should be nextLine[1]. Because nextLine contains only two values, the AppURL and SearchVals. It shouldn’t be iterated 🙂 So the correct code is:

    while ((nextLine = reader.readNext()) != null) {
    parametervals.put(nextLine[0], nextLine[1]);
    i++;
    }

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.