|
| 1 | +package javaTest; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | + |
| 8 | +import org.apache.poi.hssf.usermodel.HSSFCell; |
| 9 | +import org.apache.poi.hssf.usermodel.HSSFSheet; |
| 10 | +import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| 11 | +import org.apache.poi.ss.usermodel.Workbook; |
| 12 | + |
| 13 | +import io.restassured.response.Response; |
| 14 | + |
| 15 | +public class ExcelOperation { |
| 16 | + |
| 17 | + public static void readExcel(String filePath, String fileName, String sheetName) throws IOException { |
| 18 | + |
| 19 | + //initializing vars |
| 20 | + File file = new File(filePath + "\\" + fileName); |
| 21 | + FileInputStream inputStream = new FileInputStream(file); |
| 22 | + |
| 23 | + String reqUrl = null; |
| 24 | + String method = null; |
| 25 | + Workbook workbook = null; |
| 26 | + Response resp; |
| 27 | + |
| 28 | + String fileExtensionName = fileName.substring(fileName.indexOf(".")); |
| 29 | + if (fileExtensionName.equals(".xls")) { |
| 30 | + workbook = new HSSFWorkbook(inputStream); |
| 31 | + HSSFSheet sheet = (HSSFSheet) workbook.getSheetAt(0); |
| 32 | + |
| 33 | + System.out.println("Total APIs are : "+ sheet.getLastRowNum()); |
| 34 | + for(int i=1; i<=sheet.getLastRowNum();i++) { |
| 35 | + |
| 36 | + reqUrl = sheet.getRow(i).getCell(1).toString(); |
| 37 | + method = sheet.getRow(i).getCell(2).toString(); |
| 38 | + |
| 39 | + System.out.println("Processing "+ i +reqUrl+" "+method); |
| 40 | + |
| 41 | + //call to method which sends request |
| 42 | + if(sendRequest.testResponseCode(reqUrl,sheet.getRow(i).getCell(2).toString(),Double.parseDouble(sheet.getRow(i).getCell(3).toString()))==0) { |
| 43 | + FileOutputStream f2 = new FileOutputStream(file); |
| 44 | + HSSFCell cell = sheet.getRow(i).createCell(4); |
| 45 | + cell.setCellValue("PASS"); |
| 46 | + System.out.println("writing PASS"+cell.getRowIndex() +" : " + cell.getColumnIndex()); |
| 47 | + workbook.write(f2); |
| 48 | + f2.close(); |
| 49 | + } |
| 50 | + else { |
| 51 | + FileOutputStream f2 = new FileOutputStream(file); |
| 52 | + HSSFCell cell = sheet.getRow(i).createCell(4); |
| 53 | + cell.setCellValue("FAIL"); |
| 54 | + workbook.write(f2); |
| 55 | + f2.close(); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments