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.
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);
@SuppressWarnings("resource")
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();
s.getMapData("EmailID");
}
}
DataProvider
, but in that case you need to prepare different sheets for each test cases.