[英]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.