0

我想将用户名和密码存储在 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() {
    }
}
4

1 回答 1

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"));

您将获得所需的输出。

于 2013-03-19T13:54:07.403 回答