【问题标题】:Reading empty cells in excel for selenium webdriver data driven framework为 selenium webdriver 数据驱动框架读取 excel 中的空单元格
【发布时间】:2020-04-17 13:55:46
【问题描述】:

我在 Excel 表中有大约 5 条数据记录,其中一条记录有 5 个值,一条记录只有 4 个值,一条记录只有 2 个值。我的代码无法处理空单元格。我收到dataprovider mismatch exception。有人可以帮我吗?

Excel工作表格式:

此处插入列中的数据是可选的,因此对于少数记录它将为空。

读取 Excel 表的 Java 代码:

    public static Object[][] getTableArray(String FilePath, String SheetName) throws Exception 
{
    String[][] tabArray = null;
    try
    {
        FileInputStream ExcelFile = new FileInputStream(FilePath);
        ExcelWBook = new XSSFWorkbook(ExcelFile);
        ExcelWSheet = ExcelWBook.getSheet(SheetName);
        int startRow = 1;
        int startCol = 0;
        int ci, cj;
        row = ExcelWSheet.getRow(startRow);
        int totalRows = ExcelWSheet.getLastRowNum();
        int totalCols = row.getLastCellNum();
        tabArray = new String[totalRows][totalCols];
        ci = 0;
        for (int i = startRow; i <= totalRows; i++, ci++)
        {
            cj = 0;
            for (int j = startCol; j < totalCols; j++, cj++) 
            {
                tabArray[ci][cj] = getCellData(i, j);
            }
        }
    }catch (FileNotFoundException e) {
        System.out.println("Could not find the Excel sheet");
        e.printStackTrace();
    }catch (IOException e) {
        System.out.println("Could not read the Excel sheet");
        e.printStackTrace();
    }
    return tabArray;
}
public static String getCellData(int RowNum, int ColNum) throws Exception {
    try {
        Cell = row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK);
        Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
        String CellData = Cell.getStringCellValue();
        return CellData;
    } catch (Exception e) {
        System.out.println(e.getMessage());
        throw e;
    }
}

接受参数的Java函数:

@Test(dataProvider = "WelcomeMessage", priority = 2)
public void welcomemsg(String survey,String kw, String name,  String verbiage,  String audiofile, String barge) throws Exception {
    try {
        SurveyBuilderPage sp = PageFactory.initElements(MainConfig.getDriver(), SurveyBuilderPage.class);
        sp.setWelcomeMessage(survey, kw, name, verbiage, audiofile, barge);
    }
    catch (Exception e) {
        e.printStackTrace();
        String stackTrace = new Object(){}.getClass().getEnclosingMethod().getName();
        File screenshotFile = ((TakesScreenshot)MainConfig.getDriver()).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshotFile, new File(userdir+"\\Screenshot"+stackTrace+".png"));
    }
}

所以在这里我收到错误: Dataprovider mismatch exception.

这里 welcomemsg 函数需要 6 个参数,但第 6 个参数是可选的。所以我收到dataprovider mismatch exception。这里我需要处理这个可选参数。

非常感谢任何帮助。

提前致谢。

【问题讨论】:

  • 你用的是什么java库?您能否分享代码并完成错误消息。
  • @VN'sCorner 嘿,谢谢您的回复。我已经更新了问题并添加了必要的信息。请检查并做必要的事情。
  • @S N V Siva Kumar - 请检查解决方案并发表评论。

标签: java selenium-webdriver data-driven exceldatareader


【解决方案1】:

显然,当 dataProvider 中定义的电子表格中的一行留空时,我在代码行 Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum) 的方法 getCellData 中得到 NullpointerException

您能否尝试在调用 Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum) 周围添加一个 try/catch 块,以将 CellData 作为空格/空白返回。您可以根据需要在 Test Class welcomemsg 中适当地处理它。

public static String getCellData(int RowNum, int ColNum) throws Exception {
        String CellData;
        try {
            Cell = row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);

            try {
                Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
            } catch (NullPointerException npe) {
                CellData = " ";
            }
            if (Cell == null) {
                CellData = " ";
            } else {
                CellData = Cell.getStringCellValue();
            }
            return CellData;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw e;
        }
    }

【讨论】:

  • 感谢您的帮助。 DataFormatter 使我的任务变得简单并解决了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
相关资源
最近更新 更多