如何使用java在selenium中将结果输入excel文件
我已经完成了从excel读取输入数据的代码编写,现在我想将通过、失败的结果写入excel中的特定测试用例,但我不知道如何编写正确的代码。你能给我举个例子吗 请帮帮我。 这是我的密码如何使用java在selenium中将结果输入excel文件,java,excel,selenium,selenium-webdriver,Java,Excel,Selenium,Selenium Webdriver,我已经完成了从excel读取输入数据的代码编写,现在我想将通过、失败的结果写入excel中的特定测试用例,但我不知道如何编写正确的代码。你能给我举个例子吗 请帮帮我。 这是我的密码 public static void main(String[] args) throws IOException { WebDriver driver=null; Scanner scanner = new Scanner(System.in
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 JAR使用
getXLcellValue
函数在excel中获取值使用setXLCellValue
函数在excel中设置值
参数:-
- xlpath->Excel文件的位置
- Excel文件中的图纸名称->图纸名称
- rowNum和cellNum->数据所在的行和列编号 出席
getXLcellValue
函数在excel中获取值使用setXLCellValue
函数在excel中设置值
参数:-
- xlpath->Excel文件的位置
- 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());
}
}