Reputation: 161
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();
}
I tried all the possible solutions given on the internet, but to no use.
Upvotes: 1
Views: 2300
Reputation: 11
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;
}
}
Declared in TestBase class as global.
public ExtentReports repo= ExtentManager.getInstance(); public static ExtentTest test
Call startTest in public void onTestStart(ITestResult result)
test = repo.startTest(result.getName().toUpperCase());
Call endTest in CustomListener Class both in a)public void onTestFailure(ITestResult result); b)public void onTestSuccess(ITestResult result).
repo.endTest(test)
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