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.


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)

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.

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:

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

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


Leave a Reply