Sagar Jani
Sagar Jani

Reputation: 161

Getting closed before endTest call in Selenium using Extent Reports

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.

Upvotes: 1

Views: 2300

Answers (1)

Jayanth R
Jayanth R

Reputation: 11

  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!

Upvotes: 1

Related Questions