如何使用java在selenium中inputexcel文件的结果

我已经完成了从Excel中写入读取input数据的代码,现在我想在Excel中写入PASS,FAIL结果到特定的testing用例,但我不知道如何编写正确的代码。 所以你可以举个例子。

请帮帮我。 这是我的代码。

public static void main(String[] args) throws IOException { WebDriver driver=null; Scanner scanner = new Scanner(System.in); // prompt for the URL System.out.print("Enter your URL: "); // get their input as a String String URL = scanner.next(); //System.out.println( URL ); final FirefoxProfile firefoxProfile = new FirefoxProfile(); driver = new FirefoxDriver(firefoxProfile); driver.get(URL); driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS); driver.manage().window().maximize(); //read data from excel try { FileInputStream file = new FileInputStream(new File ("C:\\Documents and Settings\\Deepa\\Desktop\\AMC test cases1.xls")); HSSFWorkbook workbook = new HSSFWorkbook(file); HSSFSheet sheet = workbook.getSheet("login page"); WebElement login=driver.findElement(By.cssSelector("a[data-target='#login-box']")); login.click(); driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS); //For Email ID driver.findElement(By.id(sheet.getRow(1).getCell(1).getStringCellValue())).sendKeys(sheet.getRow(1).getCell(0).getStringCellValue()); //FOR Password driver.findElement(By.id(sheet.getRow(2).getCell(1).getStringCellValue())).sendKeys(sheet.getRow(2).getCell(0).getStringCellValue()); //For Submit the form driver.findElement(By.id(sheet.getRow(3).getCell(1).getStringCellValue())).click(); file.close(); //Else } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } 

使用POI jars使用getXLcellValue函数获取excel中的值使用setXLCellValue函数设置excel中的值

参数: –

  • xlpath – >您的Excel文件的位置
  • sheetName – > Excel文件中的图纸名称
  • rowNum和cellNum – >您的数据存在的行和列号

     public String getXLcellValue(String xlpath, String sheetName, int rowNum, int cellNum) { try{ FileInputStream fis=new FileInputStream(xlpath); Workbook wb=WorkbookFactory.create(fis); Log.info("Get the value from the cell(getXLcellValue)"); return wb.getSheet(sheetName).getRow(rowNum).getCell(cellNum).getStringCellValue(); } catch(Exception ex) { Log.info("Error in getXLcellValue ="+ ex.getMessage()); } return ""; } //set the value of the cell present in specific sheet void setXLCellValue(String xlpath,String sheetName,int rowNum,int cellNum, String input) { try{ FileInputStream fis=new FileInputStream(xlpath); Workbook wb=WorkbookFactory.create(fis); wb.getSheet(sheetName).getRow(rowNum).createCell(cellNum).setCellValue(input); FileOutputStream fos=new FileOutputStream(xlpath); wb.write(fos); fos.close(); Log.info("Set the value in cell(setXLCellValue)"); } catch(Exception ex) { Log.info("Error in setXLCellValue ="+ ex.getMessage()); } }