2016-07-30 39 views
-1

我在一個工作簿中有多個工作表,它也讀取每一行。如何使用java從一個工作簿中讀取多個工作表中的工作表?

但是,每次它開始讀取第一行的表格,並因爲它開始設置/寫入案例的狀態時,它再次開始設置結果從第一行意味着它重疊每個案件的結果同一排。

另外,對於閱讀和書寫工作表,我使用Jxl api。

下面是邏輯的代碼任何人都可以請讓我知道如何解決它:

private void handleScenario(String scenarioName) throws Exception { 
     ExcelHandler testScenarios = new ExcelHandler(TEST_SUITE_PATH); 
     testScenarios.setSheetName(scenarioName); 
     testScenarios.columnData();  

     int rowWorkBook1 = testScenarios.rowCount(); 
     System.out.println("Total Rows1: "+ rowWorkBook1); 

     for (int j = 1; j < rowWorkBook1; j++) { 
      String sno = testScenarios.readCell(testScenarios.getCell("Sno"), j); // SendKey 
      String testCaseDescription = testScenarios.readCell(testScenarios.getCell("TestCaseDescription"), j); 
      String framWork = testScenarios.readCell(testScenarios.getCell("FrameworkName"), j); 
      String operation = testScenarios.readCell(testScenarios.getCell("Operation"), j); // SendKey 
      String value = testScenarios.readCell(testScenarios.getCell("Value"), j); 

      handleObjects(operation, value, framWork); 
      boolean bTestCaseStepStatus = handleObjects(operation,value, framWork); 

      generateReport(bTestCaseStepStatus, scenarioName, sno,testCaseDescription, j); 
     } 
    } 

private boolean generateReport(boolean bTestCaseStepStatus, 
      String testSuiteName, String SNO, String testCaseDescription, int j) 
      throws RowsExceededException, WriteException, IOException { 

     WritableData writableData = new WritableData(TEST_RESULT, testSuiteName, j, 2); 

     // If sheet name is already exist then .... write data at row = row +1 
     // else write directly .. means row = 1 

     if (bTestCaseStepStatus) { 
      //System.out.println("--------------"+bTestCaseStepStatus+"------------"); 
      System.out.println("SNo=" +SNO+ ",Test description=" + testCaseDescription + "Row ID: " + j); 

      writableData.shSheet("Result", 2, j, "Pass"); 
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
     } else { 
     // System.out.println("--------------"+bTestCaseStepStatus+"------------");  
      System.out.println("SNo=" +SNO+ ",Test description=" + testCaseDescription + "Row ID: "+ j);    

      writableData.shSheet("Result", 2, j, "Fail"); 
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
     } 

     return bTestCaseStepStatus; 

     // Finally .... close Report file ... 
    } 

    private boolean handleObjects(String operation, String value, 
      String framWork) throws Exception { 
     // Return true/False 
     ExcelHandler objectRepository = new ExcelHandler(OBJECT_REPOSITORY_PATH, "OR"); 
     objectRepository.columnData(); 
     int rowCount = objectRepository.rowCount(); 
     //System.out.println("Total Rows in hadleObject=" + rowCount); 
     boolean testStepStatus = false; 

     for (int k = 1; k < rowCount; k++) { 
      String frameWorkName = objectRepository.readCell(
        objectRepository.getCell("FrameworkName"), k); 
      String ObjectName = objectRepository.readCell(
        objectRepository.getCell("ObjectName"), k); 
      String Locator = objectRepository.readCell(
        objectRepository.getCell("Locator"), k); // SendKey 

      //System.out.println("FrameWorkNameV=" + frameWorkName 
      //  + ",ObjectName=" + ObjectName + ",Locator=" + Locator); 

      if (framWork.equalsIgnoreCase(frameWorkName)) { 
       testStepStatus = operateWebDriver(operation, Locator, value, 
         ObjectName); 

      } 
     } 

     return testStepStatus; 
    } 
+0

你的方法handleObjects()被調用兩次 - 是故意的嗎? 另外,它看起來像你所做的只是讀取這部分代碼中的值,你可以發佈你的handleObjects()和generateReport()方法嗎? –

+0

@UlliSchmid我已經發布了handleObjects()和generateReport()方法,請檢查。 – SelenyanC2

+0

謝謝你。再次 - handleObjects(操作,值,framWork);被稱爲2次 - 故意?也許嘗試評論第16行? –

回答

0

您還沒有實現什麼generateReport()您的評論是指:

// If sheet name is already exist then .... write data at row = row +1 
// else write directly .. means row = 1 

請更改爲:

ExcelHandler countRowsHandler = new ExcelHandler(TEST_RESULT); 
countRowsHandler.setSheetName(testSuiteName); 
countRowsHandler.columnData(); 
int existingRowCount= countRowsHandler.rowCount(); 
j = j + existingRowCount 

說明:

我推斷這從您在此處提供的文檔:

How to check in workbook sheet exist or not using JXL in selenium webdriver?

WritableData.shSheet(String strSheetName, int iColumnNumber, int iRowNumber, String strData) 

接受一個行號,作爲第三個參數。在

writableData.shSheet("Result", 2, j, "Pass"); 
writableData.shSheet("Result", 2, j, "Fail"); 

你提供j,這是一個「行ID」。這j來自

for (int j = 1; j < rowWorkBook1; j++) { 

所以它將始終從handleScenario()的每個新的調用開始1,覆蓋以前的運行。你需要找出已經有多少行。從你的代碼判斷(這裏沒有提到Internet上記錄的這種方法),你可以用ExcelHandler和.rowCount()來做到這一點。請考慮重構和清理整個代碼庫,這是相當混亂的。使用過時的庫(jxl),奇怪的變量命名和註釋會讓任何局外人理解這裏發生的事情。

+0

我已經使用這一行 ExcelHandler countRowsHandler = new ExcelHandler(TEST_RESULT); countRowsHandler.setSheetName(testSuiteName); countRowsHandler.columnData(); int existingRowCount = countRowsHandler.rowCount(); j = j + existingRowCount handleScenario()方法。 – SelenyanC2

+0

請按照我的指示,它們非常廣泛。 –

+0

請問您可以根據當前的代碼情況分享代碼集成的解決方案嗎? – SelenyanC2

-1

我通過寫3行得到了溶液和下面,他們是:

  1. 聲明公衆詮釋ROWNUMBER = 0;
  2. 見下handleScenario()方法:

    私人無效handleScenario(字符串scenarioName)拋出異常{

    ExcelHandler testScenarios = new ExcelHandler(TEST_SUITE_PATH); 
        testScenarios.setSheetName(scenarioName); 
        testScenarios.columnData(); 
        int rowWorkBook1 = testScenarios.rowCount(); 
        System.out.println("Total Rows1: "+ rowWorkBook1); 
    
        for (int j = 1; j < rowWorkBook1; j++) { 
         String sno = testScenarios 
           .readCell(testScenarios.getCell("Sno"), j); // SendKey 
         String testCaseDescription = testScenarios.readCell(
           testScenarios.getCell("TestCaseDescription"), j); 
    
         String framWork = testScenarios.readCell(
           testScenarios.getCell("FrameworkName"), j); 
         String operation = testScenarios.readCell(
           testScenarios.getCell("Operation"), j); // SendKey 
         String value = testScenarios.readCell(
           testScenarios.getCell("Value"), j); 
            boolean bTestCaseStepStatus = handleObjects(operation, value, 
           framWork); 
         generateReport(bTestCaseStepStatus, scenarioName, sno, 
           testCaseDescription, j+rowNumber); 
    
         // Report 
        } 
        rowNumber = rowWorkBook1; 
    } 
    
+1

這是我的答案的替代方案。它最多隻能用於兩種「情景」。從第三個場景開始,您將重新覆蓋之前的條目。將其更改爲rowNumber = rowNumber + rowWorkBook1。 –

+0

@UlliSchmid無論何時切換到另一個工作表(在同一工作簿中),此解決方案也無法正常工作,因此它會正確設置結果。 但是在測試結果工作簿中,它在兩個不同的工作表案件之間轉義了一行。 – SelenyanC2

相關問題