java - 为 selenium webdriver 数据驱动框架读取 excel 中的空单元格

标签 java selenium-webdriver data-driven exceldatareader

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

Excel表格格式: In this sheet we have a barge in the column which is not a mandatory value so sometimes we leave it blank

这里插入列中的数据是可选的,因此对于少数记录来说它将是空的。

读取 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"));
    }
}

所以我在这里收到错误: 数据提供者不匹配异常。

这里welcomemsg函数需要6个参数,但第6个参数是可选的。所以我收到了dataprovider 不匹配异常。这里我需要处理这个可选参数。

非常感谢任何帮助。

提前致谢。

最佳答案

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

您能否尝试在调用 Cell = ExcelWSSheet.getRow(RowNum).getCell(ColNum) 周围添加一个 try/catch block ,以将 CellData 作为空格/空白返回。您可以根据需要在测试类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;
        }
    }

关于java - 为 selenium webdriver 数据驱动框架读取 excel 中的空单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61273372/

相关文章:

selenium - 如何在运行自动化脚本后杀死驱动程序

javascript - 使用 mocha 和 selenium-webdriver js 获得有意义的堆栈跟踪

c++ - 像 C++ 一样编写高效的实体系统

game-engine - 游戏引擎和数据驱动设计

java - 复杂枚举的联合测试

java - 无法处理简单的 Java.swing 游戏中的移动错误

java - 如何在 Scala 的单元测试中利用不同类的变量?

java - 运行 Selenium 2 测试套件的 Cron 作业

java - 如何在 node.js 中为 IBM DB2 iSeries 选择的 sql 的 where 子句中使用固定长度的 CCSID 37 字符值

java - 启动Mule进行远程调试,无需修改wrapper.conf