从硒中读取空白细胞时无法处理空白细胞?

问题描述 投票:0回答:1

我试图读取excel的值,但当我尝试使用dataprovider读取这些文件的值时,我的excel文件包含一些空白单元格然后我得到nullpointer

请告诉我如何处理excel文件中的这些空白单元格?

请找到我的excel文件如下: -

excel file

请查找附加的我的页面对象文件代码: -

Pageobject file

请查找附件我的例外情况代码如下: -

public class XLUtility {

    public static FileInputStream fi;
    public static XSSFWorkbook wb;
    public static XSSFSheet ws;
    public static XSSFRow row;
    public static XSSFCell cell;

    public XLUtility(String xlfile,String xlsheet) throws IOException{
        try {
            FileInputStream Excelfile=new FileInputStream(xlfile);
            wb=new XSSFWorkbook(Excelfile);
            ws=wb.getSheet(xlsheet);
        } catch (Exception e) {
            e.getMessage();
            e.printStackTrace();
        } 
    }

    public static void setExcelFile(String xlfile,String xlsheet) throws Exception { 

        try {
            FileInputStream ExcelFile = new FileInputStream(xlfile);
        wb = new XSSFWorkbook(ExcelFile);
        ws = wb.getSheet(xlsheet);
        } catch (Exception e){
            System.out.println("Exception occured is"+e);
        }
     }

    public int getRowCount() throws IOException 
    {
        int rowcount=ws.getLastRowNum();
        return rowcount;        
    }

    public int getCellCount(int rownum) {
        row=ws.getRow(rownum);
        int cellcount=row.getLastCellNum();
        return cellcount;
    }

   public String getCelldata(int rownum,int colnum) {
        row=ws.getRow(rownum);
        cell=row.getCell(colnum);
        String data;
        try 
        {
            DataFormatter formatter = new DataFormatter();
           String cellData = formatter.formatCellValue(cell);
           return cellData;
        }
        catch (Exception e) 
        {
            data="";
        }
        return data;
   }
}

请找到我的测试类代码如下: -

Test class two

请找到如下控制台输出: -

[RemoteTestNG] detected TestNG version 6.14.3
FAILED: loginDDT("BlaBla", "")
java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy8.sendKeys(Unknown Source)
    at com.AutomationTest.PageObjects.PageoneObjects.login(PageoneObjects.java:20)
    at com.AutomationTest.Testcases.TC_App_001.loginDDT(TC_App_001.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

    FAILED: loginDDT("", "BlaBla")
    java.lang.NullPointerException
        at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
        at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
        at com.sun.proxy.$Proxy8.sendKeys(Unknown Source)
        at com.AutomationTest.PageObjects.PageoneObjects.login(PageoneObjects.java:20)
        at com.AutomationTest.Testcases.TC_App_001.loginDDT(TC_App_001.java:25)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
        at org.testng.TestRunner.privateRun(TestRunner.java:648)
        at org.testng.TestRunner.run(TestRunner.java:505)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
        at org.testng.SuiteRunner.run(SuiteRunner.java:364)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
        at org.testng.TestNG.runSuites(TestNG.java:1049)
        at org.testng.TestNG.run(TestNG.java:1017)
        at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

    FAILED: loginDDT("BlaBla", "BlaBla")
    java.lang.NullPointerException
        at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
        at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
        at com.sun.proxy.$Proxy8.sendKeys(Unknown Source)
        at com.AutomationTest.PageObjects.PageoneObjects.login(PageoneObjects.java:20)
        at com.AutomationTest.Testcases.TC_App_001.loginDDT(TC_App_001.java:25)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
        at org.testng.TestRunner.privateRun(TestRunner.java:648)
        at org.testng.TestRunner.run(TestRunner.java:505)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
        at org.testng.SuiteRunner.run(SuiteRunner.java:364)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
        at org.testng.TestNG.runSuites(TestNG.java:1049)
        at org.testng.TestNG.run(TestNG.java:1017)
        at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
selenium-webdriver testng testng-dataprovider
1个回答
0
投票

在读取细胞时,需要交叉检查细胞类型。如下

    public String getCellData(String sheetName,int colNum,int rowNum){
    try{
        if(rowNum <=0)
            return "";

    int index = workbook.getSheetIndex(sheetName);

    if(index==-1)
        return "";


    sheet = workbook.getSheetAt(index);
    row = sheet.getRow(rowNum-1);
    if(row==null)
        return "";
    cell = row.getCell(colNum);
    if(cell==null)
        return "";

  if(cell.getCellType()==Cell.CELL_TYPE_STRING)
      return cell.getStringCellValue();
  else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC || cell.getCellType()==Cell.CELL_TYPE_FORMULA ){

      String cellText  = String.valueOf(cell.getNumericCellValue());
      if (HSSFDateUtil.isCellDateFormatted(cell)) {
           // format in form of M/D/YY
          double d = cell.getNumericCellValue();

          Calendar cal =Calendar.getInstance();
          cal.setTime(HSSFDateUtil.getJavaDate(d));
            cellText =
             (String.valueOf(cal.get(Calendar.YEAR))).substring(2);
           cellText = cal.get(Calendar.MONTH)+1 + "/" +
                      cal.get(Calendar.DAY_OF_MONTH) + "/" +
                      cellText;

          // System.out.println(cellText);

         }



      return cellText;
  }else if(cell.getCellType()==Cell.CELL_TYPE_BLANK)
      return "";
  else 
      return String.valueOf(cell.getBooleanCellValue());
    }
    catch(Exception e){

        e.printStackTrace();
        return "row "+rowNum+" or column "+colNum +" does not exist  in xls";
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.