2017-10-16 164 views
0

現在我正在編寫一個用Java編寫數據驅動測試的教程。我的IDE是的IntelliJ社區版和下面是我的代碼Apache POI,CREATE_NULL_AS_BLANK導致錯誤

package utility; 

import config.Constants; 
import executionEngine.DriverScript; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.xssf.usermodel.XSSFCell; 
import org.apache.poi.xssf.usermodel.XSSFRow; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

import java.io.FileInputStream; 
import java.io.FileOutputStream; 

public class ExcelUtils { 
    private static XSSFSheet ExcelWSheet; 
    private static XSSFWorkbook ExcelWBook; 
    private static XSSFCell Cell; 
    private static XSSFRow Row; 

    //This method is to set the File path and to open the Excel File 
    //Pass Excel Path and Sheet Name as Arguments to this method 
    public static void setExcelFile(String Path) throws Exception { 
     try { 
      FileInputStream ExcelFile = new FileInputStream(Path); 
      ExcelWBook = new XSSFWorkbook(ExcelFile); 
     }catch(Exception e){ 
      Log.error("Class Utils | Method setExcelFile | Exception desc: " + e.getMessage()); 
      DriverScript.bResult = false; 
     } 
    } 

    @SuppressWarnings("static-access") 
    //This method is used to write value in the excel sheet 
    //This method accepts four arguments (Result, Row Number, Column Number, Sheet Name) 
    public static void setCellData(String Result, int RowNum, int ColNum, String SheetName) throws Exception{ 
     try{ 
      ExcelWSheet = ExcelWBook.getSheet(SheetName); 
      Row = ExcelWSheet.getRow(RowNum); 

      // CHECK IF ERRORS HAPPEN AFTER THIS!!!!!!!!!!!!!!!! 
      Cell = Row.getCell(ColNum, Row.CREATE_NULL_AS_BLANK); 


      if(Cell == null){ 
       Cell = Row.createCell(ColNum); 
       Cell.setCellValue(Result); 
      }else{ 
       Cell.setCellValue(Result); 
      } 
      //Constant variables Test Data path and Test Data file name 
      FileOutputStream fileOut = new FileOutputStream(Constants.Path_TestData); 
      ExcelWBook.write(fileOut); 
      //fielOut.flush(); 
      fileOut.close(); 
      ExcelWBook = new XSSFWorkbook(new FileInputStream(Constants.Path_TestData)); 
     }catch(Exception e){ 
      DriverScript.bResult = false; 
     } 
    } 

    //This method is to read the test data from the Excel cell 
    //In this we are passing Arguments as Row Num, Col Num & Sheet Name 
    public static String getCellData(int RowNum, int ColNum, String SheetName) throws Exception{ 
     try{ 
      ExcelWSheet = ExcelWBook.getSheet(SheetName); 
      Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum); 
      String CellData = Cell.getStringCellValue(); 
      return CellData; 
     }catch(Exception e){ 
      Log.error("Class Utils | Method getCellData | Exception desc: " + e.getMessage()); 
      DriverScript.bResult = false; 
      return ""; 
     } 
    } 

    //This method id to get the row count used of the excel sheet 
    public static int getRowCount(String SheetName){ 
     int iNumber = 0; 
     try { 
      ExcelWSheet = ExcelWBook.getSheet(SheetName); 
      iNumber = ExcelWSheet.getLastRowNum()+1; 
     }catch(Exception e){ 
      Log.error("Class Utils | Method getRowCount | Exception desc: " + e.getMessage()); 
      DriverScript.bResult = false; 
     } 
     return iNumber; 
    } 

    //This method is to get the Row number of the test case 
    //This method takes three arguments(Test Case Name, Column Number & Sheet name) 
    public static int getRowContains(String sTestCaseName, int colNum, String SheetName) throws Exception{ 
     int iRowNum = 0; 
     try{ 
      //ExcelWSheet = ExcelWBook.getSheet(SheetName); 
      int rowCount = ExcelUtils.getRowCount(SheetName); 
      for(; iRowNum<rowCount; iRowNum++){ 
       if(ExcelUtils.getCellData(iRowNum, colNum, SheetName).equalsIgnoreCase(sTestCaseName)){ 
        break; 
       } 
      } 
     }catch(Exception e){ 
      Log.error("Class Utils | Method getRowContains | Exception desc: " + e.getMessage()); 
      DriverScript.bResult = false; 
     } 
     return iRowNum; 
    } 

    //This method is to get the count of the test steps of test case 
    //This method takes three arguments (Sheet name, Test Case ID & Test case row number) 
    public static int getTestStepsCount(String SheetName, String sTestCaseID, int iTestCaseStart) throws Exception{ 
     try{ 
      for(int i=iTestCaseStart; i<=ExcelUtils.getRowCount(SheetName); i++){ 
       if(!sTestCaseID.equals(ExcelUtils.getCellData(i, Constants.Col_TestCaseID, SheetName))){ 
        int number = i; 
        return number; 
       } 
      } 
      ExcelWSheet = ExcelWBook.getSheet(SheetName); 
      int number = ExcelWSheet.getLastRowNum()+1; 
      return number; 
     }catch(Exception e){ 
      Log.error("Class Utils | Method getTestStepsCount | Exception desc: " + e.getMessage()); 
      DriverScript.bResult = false; 
      return 0; 
     } 
    } 
} 

我現在的問題是以下部分:

public static void setCellData(String Result, int RowNum, int ColNum, String SheetName) throws Exception{ 
    try{ 
     ExcelWSheet = ExcelWBook.getSheet(SheetName); 
     Row = ExcelWSheet.getRow(RowNum); 

     // CHECK IF ERRORS HAPPEN AFTER THIS!!!!!!!!!!!!!!!! 
     Cell = Row.getCell(ColNum, Row.CREATE_NULL_AS_BLANK); 

我不知道現在該做什麼...... CREATE_NULL_AS_BLANK被標記爲紅色,說「無法解析符號」,我的數據驅動框架的每一步都失敗了。

我試圖用

org.apache.poi.ss.usermodel.Row.MissingCellPolicy.CREATE_NULL_AS_BLANK; 

使用它有了這個代碼,我可以執行我的代碼,但我的框架不只是第一步破碎

這裏之前是我的日誌

2017-10-16 13:19:50,093 INFO [Log] **************************************************************************************** 
2017-10-16 13:19:50,093 INFO [Log] **************************************************************************************** 
2017-10-16 13:19:50,093 INFO [Log] $$$$$$$$$$$$$$$$$$$$$     Wikipedia_01  $$$$$$$$$$$$$$$$$$$$$$$$$ 
2017-10-16 13:19:50,093 INFO [Log] **************************************************************************************** 
2017-10-16 13:19:50,093 INFO [Log] **************************************************************************************** 
2017-10-16 13:19:50,094 ERROR [Log] Class Utils | Method getCellData | Exception desc: null 
2017-10-16 13:19:50,094 INFO [Log] Opening Browser 
2017-10-16 13:19:53,388 INFO [Log] Closing the browser 
2017-10-16 13:19:54,069 INFO [Log] XXXXXXXXXXXXXXXXXXXXXXX    -E---N---D-    XXXXXXXXXXXXXXXXXXXXXX 
2017-10-16 13:19:54,069 INFO [Log] X 
2017-10-16 13:19:54,069 INFO [Log] X 
2017-10-16 13:19:54,069 INFO [Log] X 
2017-10-16 13:19:54,069 INFO [Log] X 

我希望有人能幫我解決這個問題,因爲這會讓我瘋狂。

編輯:在我添加「setCellData」部分之前,框架完全正常工作,每一步都完美無瑕地執行。在此之後發生的錯誤

+0

您使用的是什麼版本的Apache POI?如果不是最新的,升級時會發生什麼? – Gagravarr

+0

我用3.17 ...那是錯誤發生的地方...然後我也試着降級到3.10.1,但是錯誤仍然存​​在 – Pawana

回答

1

Row.MissingCellPolicy.CREATE_NULL_AS_BLANK替換您的Row.CREATE_NULL_AS_BLANK

和使用您的「常規」 進口(例如:import org.apache.poi.ss.usermodel.Row;

我是用v3.14,其更新爲v3.17,並得到了同樣的錯誤。在這些版本之間的某處,CREATE_NULL_AS_BLANK被包裝在MissingCellPolicy枚舉中,因此出現了錯誤。