Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Create A List Of Map From Excel Data In Java – Selenium – API Automation

Posted on 03/21/2025 By admin

In this post, we will learn to fetch excel data in a List of Map to achieve Data Driven Testing for Selenium or API automation scripts.

Consider a scenario in which we need to register multiple users with different data. In short need to run a script for different data sets at once. We mostly used excel sheet to pass multiple set of data. Something like below :-

You can see there are three different data sets. Each data set can be considered as a Map and when we add all these maps in to a list then we can get a List of Map and we can iterate over.

Create a Map from excel data I have covered already previously here where excel sheet has only single data set. In above excel we have three data sets.

I have tried to explain the logic of iteration in comments along with codes.

package ReadExcel; import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List; 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 ReadExcelDataInListOfMap { public static List> 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 List which retain order List> dataList = new ArrayList(); // Get data set count which will be equal to cell counts of any row int countOfDataSet = s.getRow(0).getPhysicalNumberOfCells(); // Skipping first column as it is field names for (int i = 1; i < countOfDataSet; i++) { // Creating a map to store each data set individually LinkedHashMap data = new LinkedHashMap(); // Get the row i.e field names count int rowCount = s.getPhysicalNumberOfRows(); // Now we need to iterate all rows but cell should increases once all row is done // i.e. (1,1),(2,1),(3,1),(4,1),(5,1) - First iteration // (1,2),(2,2),(3,2),(4,2),(5,2) - Second iteration // (1,3),(2,3),(3,3),(4,3),(5,3) - Third iteration for(int j = 1; j> mapDataList = getExcelDataAsMap("ExcelDataToReadInListOfMap","Sheet1"); for(int k = 0; k 
Output :-
Data Set 1 : 
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
========================================================
Data Set 2 : 
Value of FirstName is  : Animesh
Value of LastName is  : Prashant
Value of Age is  : 30
Value of Company is  : Robert Bosch
Value of Address is  : Katihar
========================================================
Data Set 3 : 
Value of FirstName is  : Aaditya
Value of LastName is  : Mahajan
Value of Age is  : 26
Value of Company is  : WeInvest
Value of Address is  : Bhopal
========================================================
 
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: DataProvider Concept in TestNG
Next Post: TestNG Tutorials 49: Need of DataProvider Method in TestNG

Related Posts

InspectTooltip – Make Selenium Easy Uncategorized
Git Tutorial 12 – Git Rm – Remove Files From Working And Staging (Index) Directory Uncategorized
Handle Horror Of NullPointerException Using Optional Class Uncategorized
REST Assured Tutorial 19 – Default Host And Port in Rest Assured Uncategorized
Frequently Asked Java Program 19: Java Program to Reverse Position of Words in a String Uncategorized
About Me 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