代码执行成功,但数据只在最后一次迭代时才添加到Excel中

我需要帮助的人,我卡在一个点。

这是我的代码之后的方法

@AfterMethod public void writeResult(ITestResult result) throws IOException, Exception { Workbook workbk = Workbook.getWorkbook(new File("D:\\Selenium Automation\\Selenium\\Framework\\Prototype\\Output\\eLogistics_Result_Latest.xls")); WritableWorkbook bookk = Workbook.createWorkbook(new File("D:\\Selenium Automation\\Selenium\\Framework\\Prototype\\Output\\eLogistics_Result_Latest_Written.xls"),workbk); int methodCellColumn =findCellLocationColumn("D:\\Selenium Automation\\Selenium\\Framework\\Prototype\\Output\\eLogistics_Result_Latest.xls", methodName); int methodCellRow =findCellLocationRow("D:\\Selenium Automation\\Selenium\\Framework\\Prototype\\Output\\eLogistics_Result_Latest.xls", methodName); if(result.getStatus() == ITestResult.SUCCESS) { System.out.println("Method-"+methodName+"- has Passed"); writeDataInCell(methodCellColumn+3, methodCellRow,"Pass",bookk); System.out.println("Method Column number is-> "+(methodCellColumn+3)); System.out.println("Method row number is-> "+methodCellRow); } else if(result.getStatus() == ITestResult.FAILURE) { System.out.println("Method-"+methodName+"- has Failed"); writeDataInCell(methodCellColumn+3, methodCellRow,"Fail",bookk); System.out.println("Method Column number is-> "+(methodCellColumn+3)); System.out.println("Method row number is-> "+methodCellRow+"\n"); } else if(result.getStatus() == ITestResult.SKIP) { System.out.println("Method-"+methodName+"- has Skipped"); writeDataInCell(methodCellColumn+3, methodCellRow,"Skip",bookk); System.out.println("Method Column number is-> "+(methodCellColumn+3)); System.out.println("Method row number is-> "+methodCellRow); } bookk.write(); bookk.close(); workbk.close(); } 

用于获取列号和单元格号的方法返回正确的结果。 其方法是 –

 public int findCellLocationColumn(String xlPath,String runningMethod) throws Exception, IOException { Workbook workbk = Workbook.getWorkbook(new File(xlPath)); Sheet sht = workbk.getSheet(0); Cell requiredCell = sht.findCell(runningMethod); int cellColumnNumber = requiredCell.getColumn(); workbk.close(); return cellColumnNumber; } 

 public int findCellLocationRow(String xlPath,String runningMethod) throws Exception, IOException { Workbook workbk = Workbook.getWorkbook(new File(xlPath)); Sheet sht = workbk.getSheet(0); Cell requiredCell = sht.findCell(runningMethod); int cellRowNumber = requiredCell.getRow(); workbk.close(); return cellRowNumber; } 

writeDataInCell的方法是 –

  public void writeDataInCell(int col,int row,String result,WritableWorkbook book1) throws RowsExceededException, WriteException, BiffException, IOException { WritableSheet firstSheet = book1.getSheet(0); Label label= new Label(col, row, result); firstSheet.addCell(label); System.out.println("writeDataInCell method executed for row-> "+row); } 

我不知道什么是错误的,但数据不是如预期的那样写在输出excel文件中输出excel看起来像这样 – 在这里输入图像说明

虽然预期的输出文件应该看起来像这样 – 在这里输入图像说明

所需的控制台输出看起来像这样 –

 Started InternetExplorerDriver server (64-bit) 2.44.0.0 Listening on port 32921 Static method completed [TestNG] Running: C:\Users\sha.RCOTIS\AppData\Local\Temp\testng-eclipse--424717979\testng- customsuite.xml Execution mode is Y Login method completed Method-Login- has Passed writeDataInCell method executed for row-> 1 Method Column number is-> 7 Method row number is-> 1 Execution mode is N Method-quickProject- has Skipped writeDataInCell method executed for row-> 2 Method Column number is-> 7 Method row number is-> 2 Execution mode is Y Close Browser method completed Method-closeBrowser- has Passed writeDataInCell method executed for row-> 3 Method Column number is-> 7 Method row number is-> 3 Execution mode is N Method-closeBrowser1- has Skipped writeDataInCell method executed for row-> 4 Method Column number is-> 7 Method row number is-> 4 PASSED: Test("1", "Project Creation", "1.00", "Login", "Robert.Lezwijn@otis.com1", "Y") PASSED: Test("2", "Complete Execution", "Final 1", "closeBrowser", "NA", "Y") SKIPPED: Test("", "", "1.01", "quickProject", "NA", "N") org.testng.SkipException: Execution mode is N at eLogistics_Execution.Execute_Test.Test(Execute_Test.java:55) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:767) at org.testng.TestRunner.run(TestRunner.java:617) at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at org.testng.SuiteRunner.run(SuiteRunner.java:240) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) at org.testng.TestNG.run(TestNG.java:1057) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) SKIPPED: Test("2", "Complete Execution", "Final 1", "closeBrowser1", "NA", "N") org.testng.SkipException: Execution mode is N at eLogistics_Execution.Execute_Test.Test(Execute_Test.java:55) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:767) at org.testng.TestRunner.run(TestRunner.java:617) at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at org.testng.SuiteRunner.run(SuiteRunner.java:240) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) at org.testng.TestNG.run(TestNG.java:1057) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) =============================================== Default test Tests run: 4, Failures: 0, Skips: 2 =============================================== =============================================== Default suite Total tests run: 4, Failures: 0, Skips: 2 =============================================== [TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms [TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@450b9338: 47 ms [TestNG] Time taken by org.testng.reporters.JUnitReportReporter@4f013415: 0 ms [TestNG] Time taken by org.testng.reporters.jq.Main@666e2862: 47 ms [TestNG] Time taken by org.testng.reporters.XMLReporter@4ff561a7: 0 ms [TestNG] Time taken by org.testng.reporters.EmailableReporter2@11def881: 0 ms 

帮我find解决scheme的人。 。 。!

由于每次从book1对象获取表格,除了上次更改外,所做的所有更改都会被跳过。 参考:

 WritableSheet firstSheet = book1.getSheet(0); 

你可以做的是,将writeDataInCell()的返回types改为WritableWorkBook,并改变调用区域的逻辑,如下所示:

 public WritableWorkbook writeDataInCell(int col,int row,String result,WritableWorkbook book1) throws RowsExceededException, WriteException, BiffException, IOException { WritableSheet firstSheet = book1.getSheet(0); Label label= new Label(col, row, result); firstSheet.addCell(label); System.out.println("writeDataInCell method executed for row-> "+row); return book1; } 

 if(result.getStatus() == ITestResult.SUCCESS) { System.out.println("Method-"+methodName+"- has Passed"); this.bookk = writeDataInCell(methodCellColumn+3, methodCellRow,"Pass",bookk); System.out.println("Method Column number is-> "+(methodCellColumn+3)); System.out.println("Method row number is-> "+methodCellRow); } else if(result.getStatus() == ITestResult.FAILURE) { System.out.println("Method-"+methodName+"- has Failed"); this.bookk = writeDataInCell(methodCellColumn+3, methodCellRow,"Fail",bookk); System.out.println("Method Column number is-> "+(methodCellColumn+3)); System.out.println("Method row number is-> "+methodCellRow+"\n"); } else if(result.getStatus() == ITestResult.SKIP) { System.out.println("Method-"+methodName+"- has Skipped"); this.bookk = writeDataInCell(methodCellColumn+3, methodCellRow,"Skip",bookk); System.out.println("Method Column number is-> "+(methodCellColumn+3)); System.out.println("Method row number is-> "+methodCellRow); }