I am trying to create extent report version v4.0.9 but unable to do so. Below code I have written to setUp class which has all before method and aftermethos and the same class is extented to Utilities class where I am performing tests.
Here is code for setUp class
public class AIG_SetUp {
protected static ExtentLoggerReporter logger;
protected static ExtentReports extent;
protected static ExtentTest log;
@BeforeTest(alwaysRun = true)
public void starttest() {
logger = new ExtentLoggerReporter(System.getProperty("user.dir"));
extent = new ExtentReports();
extent.attachReporter(logger);
System.err.close(); // written to remove JAVA 9 incompatibility.. continued below
System.setErr(System.out); // continue.. and remove the warnings
extent.setSystemInfo("User Name" , "Sobhit");
}
@AfterMethod(alwaysRun = true)
public void endReport(ITestResult result) {
try {
if (result.getStatus() == ITestResult.FAILURE) {
log.log(Status.FAIL , "Test cases Failed" + result.getName());
log.log(Status.FAIL , "Test cases Failed" + result.getThrowable());
} else if (result.getStatus() == ITestResult.SKIP) {
log.log(Status.SKIP , "Test case skipped is" + result.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterTest(alwaysRun = true)
public void endReport() {
extent.flush();
}
}
And here is the utilities class which is extented to above class.
public class UtilitiesOps extends AIG_SetUp {
@Test(groups = {"Core-Smoke"}, description = "List all media types")
public void Verify_List_all_media_types() {
extent.attachReporter(logger);
extent = new ExtentReports();
log = extent.createTest("List all media types");
log.assignCategory("Utilities Operations");
}
Couple of important points to mention Now I am not getting error, before I was getting null pointer exception but now no error. Also Code runs fine but not generating the extent report. If I put everything in one class with no before test and stuff, able to create the report. Not sure why is going wrong.
I really appreciate your help.
I got answer for this after reading about it.
basically I was doing the beforetest without static variables where as it has to be statically intiated because of other global variables.
The below code fixed my issues.
@BeforeTest(alwaysRun = true)
public static void starttest() {
logger = new ExtentLoggerReporter(System.getProperty("user.dir"));
extent = new ExtentReports();
extent.attachReporter(logger);
System.err.close(); // written to remove JAVA 9 incompatibility.. continued below
System.setErr(System.out); // continue.. and remove the warnings
extent.setSystemInfo("User Name" , "Sobhit");
}