I need your help, I want to read test data based on the Test script. And after reading data it should enter in the application.

I am using selenium Java + Apache POI

Below is the code please help.

enter image description here

  package mysrc;

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 ReadExcel {

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

        String path = "G:\\Selenium_TestNG_RnD\\MyFW\\testData\\demo.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);
            Cell cell = row.getCell(i);
            //0th Cell as Key
            /* Cell keyCell = row.getCell(0);

           //1st Cell as Value
           Cell valueCell = row.getCell(1);

           String value = valueCell.getStringCellValue().trim();
           String key = keyCell.getStringCellValue().trim();*/

            String value = cell.getStringCellValue().trim();
            String key = cell.getStringCellValue().trim();

            //Putting key & value in dataMap
            dataMap.put(key, value);

            System.out.println(dataMap.put(key, value));

            //Putting dataMap to excelFileMap
            excelFileMap.put("TestData", dataMap);

        //Returning excelFileMap
        return excelFileMap;


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

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

        return value;


    public static void main(String[] args) throws IOException {

        ReadExcel s = new ReadExcel();

  • 1
    Good question..
    – Selenium
    Commented Sep 22, 2019 at 10:31
  • Where are you stuck? Commented Sep 22, 2019 at 11:25
  • @NielsvanReijmersdal I am unable to read the Data based on the test case and column name (Key value pair). Above is the logic what I wrote. I just want to read data based on the Test case number.
    – forumqaa
    Commented Sep 22, 2019 at 11:33
  • Hi @forumqaa i can suggest you answer by using DataProvider, but in that case you need to prepare different sheets for each test cases. Commented Sep 23, 2019 at 5:41

1 Answer 1


Hi @Forumqaa Below code may help for you.

Please change the logic as per you.

public class Sac {

public static void main(String[] args) throws IOException {

    Sac s = new Sac ();

public ArrayList<String> getData(String testCaseName) throws IOException{

    ArrayList<String> a = new ArrayList<String>();      
    FileInputStream fis = new FileInputStream("G:\\Selenium_TestNG_RnD\\MyFW\\testData\\demo.xlsx");
    XSSFWorkbook workbook =new XSSFWorkbook(fis);

    int sheets =workbook.getNumberOfSheets();

    for (int i = 0; i < sheets; i++) {


            XSSFSheet sheet = workbook.getSheetAt(i);

            Iterator<Row> row = sheet.iterator();
            Row firstRow =row.next();
            Iterator<Cell> cell =firstRow.cellIterator();
            int k=0;
            int coloumn =0;


                Cell value =cell.next();

                    coloumn =k;

        //  System.out.println(coloumn);


                Row r =row.next();

                    Iterator<Cell> cy =r.cellIterator();

                        Cell c =cy.next();








    return a;


  • 1
    it was helped me and near to what I was expected. I am not sure who is down voting, if they can not help then do not down vote.
    – forumqaa
    Commented Sep 24, 2019 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.