How to store data from an Excel Sheet with HashMap in Selenium?

We are working on the marathon of tutorials to maintain parameterization efficiently. There is a very wide scope of data science and data analytics these days. Experts basically look for test data management in any Automation framework, so it’s our responsibility to efficiently maintain the test data. Today we will learn how to store data from excel sheet to the HashMap so that we can retrieve data effectively by its key whenever required.

There could be many techniques but data maintenance and storage through HashMap has its own advantage.

Pre-Requisites

Before we jump to the tutorial, you should go through below links which discuss the application of Apache POI in Excel sheet manipulation:

Exclusive Post: How to install Selenium IDE in Google Chrome?

Functional Specification to Store data in HashMap from Excel sheet

What basically we will do?

We generally define two columns against many rows so here if we consider the 0th cell as the fixed cell which is the real attribute like URL, Search text, etc then the 1st cell will hold its values. Right? Hence, we got key and value pairs.

Now we will store data (value) from 0th Cell as Key in HashMap, whereas data from the 1st cell will be stored as the value in the HashMap. Did you get the logic?

This is the micro level designing. Our goal is to store data map inside another HashMap. This is done to maintain the one to many relationships. Therefore, there will be two HashMap. One HashMap will have String as Key and another HashMap as its corresponding value.

In first HashMap, we can give Excel file/sheet name as Key and data inside this Excel sheet as the corresponding value. Make sense!

If you wish you can use three HashMap. Like one HashMap as Workbook name as Key and Sheet map as values and further Sheet map will have real test data. So, it totally depends on the strategy of Automation framework architect.

A coded representation of the Functional specification

HashMap 1 (Excel file name with data map)

Map<String, Map<String, String>> excelFileMap = new HashMap<String, Map<String, String>>();

HashMap 2 (Test data map)

This map will basically store all the test data. Once all the data are put in this map then the map will be further put into excelFileMap.

Map<String, String> dataMap = new HashMap<String, String>();
excelFileMap.put(“fileName”, dataMap);

Java code to store data in HashMap (Basic uses of Collections framework)

This image contains the Attributes and value pairs in the excel sheet. Our goal is to store all the attributes as key and corresponding values as the value in a HashMap.

Store data from excel sheet

Once we store them then we can retrieve each value by passing the attributes in the method. Here is the code:

package com.inviul.selenium.project;


import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelSheetData {
	
	
	public static Map<String,  Map<String, String>> setMapData() throws IOException {

		  String path = "data/TestDataSheet.xlsx";
		  
		  FileInputStream fis = new FileInputStream(path);
	
		  Workbook workbook = new XSSFWorkbook(fis);
		
		  Sheet sheet = workbook.getSheetAt(0);
		  
		  int lastRow = sheet.getLastRowNum();
		  
		  Map<String, Map<String, String>> excelFileMap = new HashMap<String, Map<String,String>>();
		  
		  Map<String, String> dataMap = new HashMap<String, String>();
		  
		  //Looping over entire row
		  for(int i=0; i<=lastRow; i++){
			  
			  Row row = sheet.getRow(i);
			  
			  //1st Cell as Value
			  Cell valueCell = row.getCell(1);
				  
			  //0th Cell as Key
			  Cell keyCell = row.getCell(0);
				  
			  String value = valueCell.getStringCellValue().trim();
			  String key = keyCell.getStringCellValue().trim();
				  
			  //Putting key & value in dataMap
			  dataMap.put(key, value);
				  
			  //Putting dataMap to excelFileMap
			  excelFileMap.put("DataSheet", dataMap);
		  }
		  
		 //Returning excelFileMap
		return excelFileMap;

	}
	
	//Method to retrieve value
	public static String getMapData(String key) throws IOException{

	Map<String, String> m = setMapData().get("DataSheet");
		String value = m.get(key);
		
		return value;
		
	}

}

Main method which calls getMapData(String arg) by passing the key:

package com.inviul.selenium.project;

import java.io.IOException;

public class ReadSheetData {

	public static void main(String[] args) throws IOException {
		ReadExcelSheetData r = new ReadExcelSheetData();
		
		String val = r.getMapData("search");
		
		System.out.println("Value of the keyword (search) is- "+val);

	}

}

Here is the console output:

Console output of store data to hashmap from excel sheet

If you face any difficulties in understanding the code then feel free to ask your queries in the comment below, even you can share your feedback.

Have you joined our Facebook group?

Join now

Join Inviul fb group

30 Comments

Leave a Reply