我想在Excel中存储用户名和密码,无论我在使用WebDriver的任何应用程序中输入什么。我正在使用TestNG框架和Apache POI将数据写入Excel。但我得到了Null pointer exception
。请告诉我如何使用Excel工作WebDriver。
public class Test {
private static WebDriver driver;
static String username="abc";
static String password="abf123";
public static void main(String args[]) {
try {
driver.get("any url");
driver.findElement(By.xpath("//*[@id='txtUserName']")).sendKeys(username);
driver.findElement(By.xpath("//*[@id='txtPwd']")).sendKeys(password);
FileOutputStream fos = new FileOutputStream("Userpass.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("POI WorkSheet");
HSSFRow row1 = worksheet.createRow((short) 0);
HSSFCell cell1 = row1.createCell((short) 0);
cell1.setCellValue(username);
HSSFCell cell2 = row1.createCell((short) 1);
cell2.setCellValue(password);
workbook.write(fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void f() {
}
}
答案 0 :(得分:1)
替换代码中的以下行
HSSFRow row1 = worksheet.createRow((short) 0);
HSSFCell cell1 = row1.createCell((short) 0);
cell1.setCellValue(username);
HSSFCell cell2 = row1.createCell((short) 1);
cell2.setCellValue(password);
与
HSSFRow row1 = worksheet.createRow(0);
row1.createCell(0).setCellValue(new HSSFRichTextString("username"));
row1.createCell(1).setCellValue(new HSSFRichTextString("password"));
您将获得所需的输出。