使用selenium webdriver将状态写入java中的excel文件

rks48beu  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(363)

我可以通过我的程序将状态写入excel文件,但仍然存在一个问题,状态没有正确写入excel工作表。控制台输出正常-
moulikanimmala的值为:fail键为:moulikanimmala fail和1
的价值lakshman_nimmala@yahoo.com is:传递键is:lakshman_nimmala@yahoo.com 通过和2
赛拉克什曼的价值。nimmala@gmail.com 是:失败键是:sailakshman。nimmala@gmail.com 失败和3
keshav的值为:fail键为:keshav fail和4
但excel表中的状态填写不正确。lakshman_nimmala@yahoo 应填写通过和凯沙夫应填写失败。

@Test(priority = 2)
    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);

            WebDriverWait wait = new WebDriverWait(driver, 10);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[text()='Sign In']")));
            driver.findElement(By.xpath("//*[text()='Sign In']")).click();

            WebDriverWait wait1 = new WebDriverWait(driver, 10);
            wait1.until(ExpectedConditions.visibilityOfElementLocated(By.name("username")));
            driver.findElement(By.name("username")).clear();

            driver.findElement(By.name("username")).sendKeys(row.getCell(0).toString());
            driver.findElement(By.name("passwd")).clear();

            driver.findElement(By.name("passwd")).sendKeys(row.getCell(1).toString());
            driver.findElement(By.name("signin")).click();
            Thread.sleep(10000);
            try {
                new Actions(driver).moveToElement(driver.findElement(By.xpath("//*[text()='nimmala']"))).perform();
                driver.findElement(By.xpath("//*[text()='Sign Out']")).click();

                WebDriverWait wait2 = new WebDriverWait(driver, 10);
                wait2.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[text()='Sign In']")));
                driver.findElement(By.xpath("//*[text()='Sign In']")).click();

                hm.put(row.getCell(0).toString(), "Pass");
            } catch (Exception ex) {
                //ex.printStackTrace();
                hm.put(row.getCell(0).toString(), "Fail");
                driver.get("http://in.yahoo.com/?p=us");
            }
    }  

    Set<String> keys = hm.keySet();

    int i=1;
    for (String key: keys){
        System.out.println("Value of "+key+" is: "+hm.get(key));

        String filePath1 = System.getProperty("user.dir") + "\\Test Data";
        String fileName1 = "editSubscriptions.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();

            Cell cell1 = sheet1.getRow(i).createCell(2);
            cell1.setCellType(Cell.CELL_TYPE_STRING);
            cell1.setCellValue(hm.get(key));
            System.out.println(" key is:" + key+" "+hm.get(key) + " and " + i);
            i = i+1;

      //Close input stream
        fis1.close();

      //Create an object of FileOutputStream class to create write data in excel file
        FileOutputStream outputStream = new FileOutputStream(file1);

        //write data in the excel file
        workbook1.write(outputStream);

        //close output stream
        outputStream.close();
    }
  }

问题在于识别行索引。
我们有没有办法根据excel文件中的单元格值来识别行索引,以便替换 i 在下面的语句中使用正确的行索引?: Cell cell1 = sheet1.getRow(i).createCell(2);

plicqrtu

plicqrtu1#

要返回给定单元格值的行号,以下代码将起作用-

public int findRow(Sheet sheet, String content) {
int rowNum = 0;
for (Row row : sheet) {
for (Cell cell: row) {
    if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
        if (cell.getRichStringCellValue().getString().trim().equals(content)) {
                    return row.getRowNum();
                }
            }
        }
    }
    return 0;}

相关问题