简体   繁体   English

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

[英]Reading empty cells in excel for selenium webdriver data driven framework

I have around 5 records of data in an excel sheet in which one record has 5 values, one record has only 4 values and one record has only 2 values.我在 excel 工作表中有大约 5 条数据记录,其中一条记录有 5 个值,一条记录只有 4 个值,一条记录只有 2 个值。 My code is not able to handle the empty cells.我的代码无法处理空单元格。 I'm getting dataprovider mismatch exception .我收到数据提供者dataprovider mismatch exception Can someone help me?有人能帮我吗?

Excel sheet Format: Excel 表格格式: 在这张表中,我们在列中有一个驳船,这不是强制性值,所以有时我们将其留空

Here data under barge in column is optional so it will be empty for few records.这里 barge in column 下的数据是可选的,因此对于少数记录它将是空的。

Java Code to Read Excel Sheet: Java 代码读取 Excel 表:

    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 Function to accept the parameters: Java Function 接受参数:

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

So here I'm getting error as: Dataprovider mismatch exception.所以在这里我收到错误: Dataprovider mismatch exception.

Here welcomemsg function expecting 6 parameters but 6th parameter is an optional one.这里welcomemsg function 需要6 个参数,但第6 个参数是可选的。 So I'm getting dataprovider mismatch exception .所以我得到了dataprovider mismatch exception Here I need to handle this optional parameter.这里我需要处理这个可选参数。

Any help is much appreciated.任何帮助深表感谢。

Thanks in advance.提前致谢。

Apparently i am getting NullpointerException in the method getCellData at the line of code Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum) when one of the row from the spreadsheet as defined in the dataProvider is left blank.显然,当 dataProvider 中定义的电子表格中的一行留空时,我在代码Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum)行的方法 getCellData 中得到NullpointerException

Could you try adding a try/catch block around the call Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum) to return CellData as space/blanks.您能否尝试在调用Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum)周围添加一个 try/catch 块,以将 CellData 作为空格/空白返回。 You can handle it appropriately in the Test Class welcomemsg as desired.您可以根据需要在 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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据驱动框架——如何使用Selenium WebDriver和java在excel表中读写 - Data Driven Framework — how to read and write in excel sheet using Selenium WebDriver with java 如何构建数据驱动的 Selenium WebDriver + Java + TestNG 框架 - How to Build a Data Driven Selenium WebDriver + Java + TestNG Framework 从输出Excel读取数据时,它显示Selenium Webdriver中的错误 - While reading the data from output excel,it shows error in selenium webdriver 在Selenium WebDriver中使用JAVA使用Excel工作表对不同网页上的内容进行数据驱动测试 - Data Driven testing using excel sheet for the content on different web pages using JAVA in Selenium WebDriver 如何使用Selenium WebDriver从Excel读取数据来选择多个复选框? - How to select mutiple checkboxes using selenium webdriver reading data from excel? 使用testNG读取硒中的Excel数据 - Reading excel data in selenium with testNG 硒数据驱动测试-从Excel读取为十进制数 - Selenium Data driven test- number read as decimal from excel 如何在没有 excel 文件或数据库的情况下在 Selenium 中进行数据驱动测试 - How to do Data driven testing in Selenium without excel file or Database Java中的关键字驱动框架(Selenium) - Keyword driven framework (Selenium) in Java 从Excel文件中读取空单元格时出现异常 - Exception when reading empty cells from Excel file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM