I have written some test scripts to which i need to send test data to execute the scripts. I have written a code to iterate excel and find the given string along with the row and column number in which the String is present. Here is my testData format which I wish to use:
TestCase_ID || File Format || File Name || File Path || .... n
===============================================================
TC_01 || Document || selenium.pdf || C://selenium.pdf
===============================================================
Here is the excel iteration code I'm using:
public class ExcelFileData {
private String fileFormat;
private String fileName;
String filepath;
public static void getCellData(String testCaseName) {
try {
FileInputStream file = new FileInputStream(new File("C://TestData_01.xlsx"));
@SuppressWarnings("resource")
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(cell.getCellType() == CellType.STRING && cell.getStringCellValue().equalsIgnoreCase(testCaseName)) {
System.out.println(cell.getStringCellValue());
System.out.println("search key at Col: "+cell.getColumnIndex());
System.out.println("search key Found at Row: "+cell.getRowIndex());
}else {
break;
}
}
System.out.println("");
}
}catch (Exception e) {
e.printStackTrace();
}
}
Map<String, ExcelFileData> excelDataMap = new HashMap();
public static void main(String args[]) {
ExcelFileData.getCellData("TC_01");
}
}
Output:
TC_01
search key at Col: 0
search key Found at Row: 1
I wish to find the Data with respect to given test case. As in , I would pass testcase id(i.e TC_01) and then wish to iterate all the columns of this particular row. I'm new to programming, hence would like to know how can I put all the data while iterating through excel in HashMap so that I can use that data as input to my testscripts.