Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Create a Map From Excel Data in Java – Selenium – API Automation

Posted on 01/13/2025 By admin

In this post, we will learn to create a Map from Excel data to use in scripts such as Selenium and Rest Assured tests.

When we write a code, it may require some data to act on. For an example :- If we want to register a user on an application, we require some user data like name, age , address , contact details etc. It is always a good practice to keep logic and data separated.

We use “Excel” extensively to store test data. We are aware that excel stores data as rows and cells and each cell can be uniquely identified using index. I have already covered reading an excel file using Apche POI here.

In this post, we will see a common problem and how can we resolve it.

Consider below excel data :-

I have stored data as a key value pair. If I need value of “FirstName“, I can pass row as 1st and cell as 1st i.e. (1,1). Similarly if I need value of “Company“, I can pass row as 4th and cell as 1st i.e. (4,1). In short, I am hard coding rows and cells. If I add a new field in between , all indexes will be changed. This way is not recommended.

Think if I could get all these key-value pair from excel in to Java data structure – Map, so that I could get the value just by passing field name. You can raise a concern what if I do not know the exact field name? It is genuine and I believe accessing data must be easier. I can use a type preferably Enum to define fields. We should always avoid hard coded things in code which could be passed from outside.

Let’s implement the same.

package ReadExcel; import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import org.apache.poi.EncryptedDocumentException;
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.ss.usermodel.WorkbookFactory; public class ReadExcelDataInMap { public static LinkedHashMap getExcelDataAsMap(String excelFileName, String sheetName) throws EncryptedDocumentException, IOException { // Create a Workbook Workbook wb = WorkbookFactory.create(new File("src\\test\\resources\\excelFiles\\"+excelFileName+".xlsx")); // Get sheet with the given name "Sheet1" Sheet s = wb.getSheet(sheetName); // Initialized an empty LinkedHashMap which retain order LinkedHashMap data = new LinkedHashMap(); // Get total row count int rowCount = s.getPhysicalNumberOfRows(); // Skipping first row as it contains headers for (int i = 1; i < rowCount; i++) { // Get the row Row r = s.getRow(i); // Since every row has two cells, first is field name and another is value. String fieldName = r.getCell(0).getStringCellValue(); String fieldValue = r.getCell(1).getStringCellValue(); data.put(fieldName, fieldValue); } return data; } public static void main(String[] args) throws EncryptedDocumentException, IOException { LinkedHashMap mapData = getExcelDataAsMap("ExcelDataToReadInMap","Sheet1"); for(String s: mapData.keySet()) { System.out.println("Value of "+s +" is : "+mapData.get(s)); } } }

Output :-

Value of FirstName is : Amod
Value of LastName is : Mahajan
Value of Age is : 29
Value of Company is : Sapient
Value of Address is : Bengaluru

This strategy will be helpful in handling dynamic flow. For example :- To register a user you may need multiple fields but do not required all. Some field may ask for some extra details. You can pass all these data setup in excel and let code work on passed data. No need to open excel and get index manually.

You can download/clone above sample project from here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: TestNG Tutorials 54: DataProvider in TestNG – Implementing Object Oriented Concept “Encapsulation” with DataProvider Method
Next Post: Git Tutorial 11 – Git Pull – Download And Merge Changes From Remote Repository

Related Posts

October 2017 – Make Selenium Easy Uncategorized
How to load a URL in browser without using get() or navigate() method in Selenium Uncategorized
October 10, 2017 – Make Selenium Easy Uncategorized
REST Assured Tutorial 25 – How To Create a JSON Object Using Jackson API – ObjectMapper – createObjectNode() Uncategorized
August 17, 2018 – Make Selenium Easy Uncategorized
Make Selenium Easy – Page 25 Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark