Selenium Java – 将hashmap值写入excel

我正在尝试使用hashmap值在excel文件中编写testing用例状态,但不是将数据写入excel。 这是我的代码 –

public void readData() throws Exception { String filePath = System.getProperty("user.dir") + "\\Test Data"; String fileName = "editSubscriptions.xls"; String sheetName = "datapool"; File file = new File(filePath + "\\" + fileName); FileInputStream fis = new FileInputStream(file); Workbook workbook = null; String fileExtName = fileName.substring(fileName.indexOf(".")); if (fileExtName.equals(".xlsx")) { workbook = new XSSFWorkbook(fis); } else if (fileExtName.equals(".xls")) { workbook = new HSSFWorkbook(fis); } Sheet sheet = workbook.getSheet(sheetName); int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum(); HashMap<String, String> hm = new HashMap<String, String>(); for (int i = 1; i < rowCount + 1; i++) { Row row = sheet.getRow(i); for (int j = 1; j < row.getLastCellNum(); j++) { System.out.println(row.getCell(j).getStringCellValue()); driver.findElement(By.linkText("Login")).click(); WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))); driver.findElement(By.id("username")).sendKeys(row.getCell(0).toString()); driver.findElement(By.id("password")).sendKeys(row.getCell(1).toString()); driver.findElement(By.name("submit")).click(); Thread.sleep(10000); try { driver.findElement(By.linkText("Logout")).click(); WebDriverWait wait1 = new WebDriverWait(driver, 10); wait1.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Login"))); hm.put(row.getCell(0).toString(), "Pass"); } catch (Exception ex) { hm.put(row.getCell(0).toString(), "Fail"); driver.get("http://www.openclinica.com"); } } } Set<String> keys = hm.keySet(); for (String key: keys){ System.out.println("Value of "+key+" is: "+hm.get(key)); String filePath1 = System.getProperty("user.dir") + "\\Test Data"; String fileName1 = "editSubscriptions1.xls"; String sheetName1 = "datapool"; File file1 = new File(filePath1 + "\\" + fileName1); FileInputStream fis1 = new FileInputStream(file1); Workbook workbook1 = null; String fileExtName1 = fileName1.substring(fileName1.indexOf(".")); if (fileExtName1.equals(".xlsx")) { workbook1 = new XSSFWorkbook(fis1); } else if (fileExtName1.equals(".xls")) { workbook1 = new HSSFWorkbook(fis1); } Sheet sheet1 = workbook1.getSheet(sheetName1); int rowCount1 = sheet1.getLastRowNum() - sheet1.getFirstRowNum(); for (int i=1; i < rowCount1; i++){ Cell cell = sheet1.getRow(i).createCell(2); cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue(hm.put(key, hm.get(key))); } } } 

我想通过使用hashmap key将状态添加到Excel工作表中。 我能够在控制台中打印状态,但是这不是转换到Excel表格。

HemaSai的价值是:MoulikaNimmala的失败价值是:Keshav的传递价值是:失败

请帮我解决问题

先谢谢你。

 int rowCount1 = sheet1.getLastRowNum() - sheet1.getFirstRowNum(); for (int i=1; i < rowCount1; i++){ Cell cell = sheet1.getRow(i).createCell(2); cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue(hm.put(key, hm.get(key))); } 

根据文档,你的循环应该从索引0开始。

它看起来像你永远不会进入一个循环,因为你从索引1开始,但你的行数是最初的0,如果你从一个空的工作表开始。

编辑:评论后,我会去这样的解决scheme(未经testing!)

  Set<String> keys = hm.keySet(); String filePath1 = System.getProperty("user.dir") + "\\Test Data"; String fileName1 = "editSubscriptions1.xls"; String sheetName1 = "datapool"; File file1 = new File(filePath1 + "\\" + fileName1); FileInputStream fis1 = new FileInputStream(file1); Workbook workbook1 = null; String fileExtName1 = fileName1.substring(fileName1.indexOf(".")); if (fileExtName1.equals(".xlsx")) { workbook1 = new XSSFWorkbook(fis1); } else if (fileExtName1.equals(".xls")) { workbook1 = new HSSFWorkbook(fis1); } Sheet sheet1 = workbook1.getSheet(sheetName1); int rowCount1 = sheet1.getLastRowNum() - sheet1.getFirstRowNum(); //set rowNumber to the last already set one for (String key: keys){ //for every key write the name in column 1 and the result in column 2 rowcount1++; //start at the next free row Cell cell1 = sheet1.createRow(rowCount1).createCell(1); Cell cell2 = sheet1.getRow(rowCount1).createCell(2); cell1.setCellType(Cell.CELL_TYPE_STRING); cell2.setCellType(Cell.CELL_TYPE_STRING); cell1.setCellValue(key)); cell2.setCellValue(hm.get(key))); } }