1

BaseTest.java:

private static ReportService reportService; // Calling report service interface

@BeforeSuite:
reportService = new ExtentReportService(getConfig()); // New instance of ExtentReportService.

@BeforeMethod:
reportService.startTest(testname); // Starting the test and passing the name and description of the test.

@AfterMethod:
reportService.endTest(); // Ending the test

@AfterSuite:
reportService.close(); // Closing the test

**ExtentReportService.java:** // Contains different extent API methods. (These are designed to be generic.)

protected static ExtentReports extent; // static instance of ExtentReports
protected static ExtentTest test; //static instance of ExtentTTest

@Override // StartTest method
startTest(Method method) {
testMetaData = getTestMetaData(method);
test=extent.startTest(testMetaData.getId(),testMetaData.getSummary());
}

@Override //End test method
endTest() {
extent.endTest(test);
extent.flush();
}
  1. The above is my selenium code.
  2. When I am executing my suite file with parallel="methods" and thread count="3", I am getting the following error: "com.relevantcodes.extentreports.ExtentTestInterruptedException: Close was called before test could end safely using EndTest.".
  3. While debugging, I found that even before all endTest() in AfterMehtod were executed, AfterSuite was being called.
  4. I tried different variations such that the code works, such as, removing static, calling endTest() in the test itself rather than after method, removing close() call from AfterSuite and many other variations. But still getting the same error.
  5. I tried all the possible solutions given on the internet, but to no use.

Sagar Jani
  • 161
  • 2
  • 3
  • 21
  • 1
    You are calling extent.endTest(test); method under endTest method,if so which method is this calling. Try to flush report before it. – Ishita Shah May 31 '18 at 09:06
  • 1
    My endTest() in BaseTest is calling endTest() in ExtentReportsService class. There, first it will end the test, and then flush the report. I tried flushing it before ending the test. It doesn't work. Also another thing is, all my test cases are executing perfectly well. But only the extent report is not generating properly. – Sagar Jani May 31 '18 at 10:36
  • 1
    First, quite the driver and then flush the extent Report. – Ishita Shah May 31 '18 at 11:26
  • 1
    Also tried that, but not working. I am getting successful test runs. Just no proper Extent Report document. – Sagar Jani May 31 '18 at 13:27
  • 1
    You should be doing the `extent.flush()` in your `@AfterSuite` – Bill Hileman May 31 '18 at 14:23
  • Remove the call to close, it's not required. It closes the stream which is why you see this exception. Also, upgrade to version 3 if possible since version 2 is no longer supported. This issue is most probably due to an existing suite being completed even though tests from a different suite are yet to be run in entirety. – Anshoo Jun 01 '18 at 01:25
  • I am executing flush() in @AfterSuite now. Also I upgraded to the lastest version of Extent. But still getting the same problem. – Sagar Jani Jun 01 '18 at 10:44
  • Did you remove the close method call as @Anshoo suggested? – Bill Hileman Jun 01 '18 at 14:45
  • Yes. I removed the close() call. I added the flush() call in AfterSuite. Can this issue be caused by multithreading? – Sagar Jani Jun 04 '18 at 05:14
  • Please refer to the image named "Unsynchronized ouput in the description." – Sagar Jani Jun 04 '18 at 06:49
  • Hi, did You try using synchronized in methods, could You provide xml where You're calling this parallel execution, there is possiblity that You close something before intended – Kovacic Jun 04 '18 at 07:04
  • Yes, I tried that too. I have attached the XML file screenshot for the parallel test I am working on. I am initializing extent report in BeforeSuite, then initializing driver in BeforeMethod and calling driver.quit() in Aftermethod. Then I am calling extent.flush() in AfterSuite. I have also tried with and without alwaysRun=true. – Sagar Jani Jun 04 '18 at 08:01
  • I am still stuck with the issue. But I think it is caused because of multi-threading. When I don't use Extent Reports, the TestNG emailable report shows the steps as they are in their corresponding test case. But in Extent Reports, the steps are getting jumbled. – Sagar Jani Jun 06 '18 at 08:35

1 Answers1

1
  1. ExtentReports Intialized in ExtentManager class using Singleton().

    public class ExtentManager {
    private static ExtentReports extent; public static ExtentReports getInstance() {
    if(extent == null) {
    extent = new ExtentReports(System.getProperty("user.dir")+"\target\surefire-reports\html\extent.html", true, DisplayOrder.OLDEST_FIRST); extent.loadConfig(new File(System.getProperty("user.dir")+"src\test\resources\extentconfig\ReportsConfig.xml"));
    } return extent; } }

  2. Declared in TestBase class as global.

    public ExtentReports repo= ExtentManager.getInstance(); public static ExtentTest test

  3. Call startTest in public void onTestStart(ITestResult result)

    test = repo.startTest(result.getName().toUpperCase());

  4. Call endTest in CustomListener Class both in a)public void onTestFailure(ITestResult result); b)public void onTestSuccess(ITestResult result).

    repo.endTest(test)

  5. Call close() OR flush() in @AfterSuite in TestBase class but NOT both!

    //repo.close(); repo.flush();

Note: I have ExtentReports ver-2.41.2, and TestNg ver-7.1.0.

After the above steps, error 'Getting closed before endTest call in Selenium using Extent Reports' got resolved. Extent report generates each test successfully in the report. Try it out!

Jayanth R
  • 11
  • 2
  • Thanks. But this question was old and I re-wrote the code from scratch as there were many other problems. Your solution, if seen logically, will work out. Thanks for the effort and sharing your answer. – Sagar Jani Dec 30 '19 at 07:15