I’m using extent report to generate html report.
I want to print log that i have added after every test step.
This is extent report class.
[package extentreports;
import com.aventstack.extentreports.ExtentReporter;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import utils.HTMLFormat;
import utils.Log;
import utils.Utils;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class ExtentReport {
private static ExtentReport instance;
public static ExtentReport shared() {
if (instance == null) {
instance = new ExtentReport();
}
return instance;
}
private ExtentReports extent;
ExtentHtmlReporter htmlReporter;
public ExtentReports getExtent() {
if (extent == null) {
extent = new ExtentReports();
try {
htmlReporter = new ExtentHtmlReporter(Utils.getReportDir() + "/report.html");
htmlReporter.loadXMLConfig(new File("src/test/java/extentreports/extent-config.xml"));
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.setAppendExisting(false);
extent.attachReporter(htmlReporter);
extent.setSystemInfo("OS Name",System.getProperty("os.name"));
extent.setSystemInfo("OS Version",System.getProperty("os.version"));
extent.setSystemInfo("Java Version",System.getProperty("java.version"));
extent.setSystemInfo("User Name",System.getProperty("user.name"));
} catch (Exception ex) {
Log.addLog(ex.getMessage());
}
}
return extent;
}
public ExtentTest getExtentTest(String name) {
ExtentTest extentTest = getExtent().createTest(name);
extentTest.assignCategory(name);
extentTest.assignAuthor(System.getProperty("user.name"));
return extentTest;
}
public ExtentTest getExtentTest(String name, String description) {
ExtentTest extentTest = getExtent().createTest(name, description);
extentTest.assignCategory(name);
extentTest.assignAuthor(System.getProperty("user.name"));
return extentTest;
}
public ExtentTest getChildExtentTest(ExtentTest extent, String name) {
ExtentTest childExtentTest = extent.createNode(name);
return childExtentTest;
}
public ExtentTest getChildExtentTest(ExtentTest extent, String name, String description) {
ExtentTest childExtentTest = extent.createNode(name, description);
return childExtentTest;
}
// Add log to report file
public static void Log(ExtentTest extentTest, Status status, String description) {
extentTest.log(status, "[" + HTMLFormat.bold(status.toString().toUpperCase()) + "] " + description);
}
public static void Log(ExtentTest extentTest, int isSuccess) {
Status status = Status.FAIL;
if(isSuccess == 1 ) {
status = Status.PASS;
}
if(isSuccess == 3 ) {
status = Status.SKIP;
}
extentTest.log(status, "[" + HTMLFormat.bold(status.toString().toUpperCase()) + "] ");
}
}]
And this is how i’m using with listener.
[package utils;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import extentreports.ExtentReport;
import org.apache.poi.ss.formula.functions.T;
import org.testng.*;
import java.lang.annotation.Annotation;
public class testListener implements ITestListener {
@Override
public void onTestStart(ITestResult iTestResult) {
if (iTestResult != null) {
String testName = iTestResult.getName();
Log.addLog(testName + " is RUNNING...");
}
// Annotation annotation = getAnnotation(iTestResult, Optimum.class);
// if (annotation != null) {
// Optimum optimum = (Optimum) iTestResult;
// String testName = optimum.name();
// if (testName.isEmpty()) {
// testName = iTestResult.getName();
// }
//
// String priority = optimum.priority().toString();
// if (!optimum.description().equals("")) {
// Log.println(optimum.description());
// }
// Log.addLog(“RUNNING…” + testName + " (" + priority + “)”);
// }
}
@Override
public void onTestSuccess(ITestResult iTestResult) {
if (iTestResult != null) {
String testName = iTestResult.getName();
Log.addLog(testName + " is Passed...");
ExtentTest extent = ExtentReport.shared().getExtentTest(testName);
ExtentReport.Log(extent,1);
}
// Annotation annotation = getAnnotation(iTestResult, Optimum.class);
// if (annotation != null) {
// Optimum optimum = (Optimum) annotation;
// String testName = optimum.name();
// if (testName.isEmpty()) {
// testName = iTestResult.getMethod().getMethodName();
// }
//
// Log.addLog(testName + " is Passed");
// }
//
}
@Override
public void onTestFailure(ITestResult iTestResult) {
if (iTestResult != null) {
String testName = iTestResult.getName();
Log.addLog(testName + " is FAILED...");
ExtentTest extent = ExtentReport.shared().getExtentTest(testName);
ExtentReport.Log(extent,2);
}
// Annotation annotation = getAnnotation(iTestResult, Optimum.class);
// Log.addLog(“annotation " + annotation);
// if (annotation != null) {
// Optimum optimum = (Optimum) annotation;
// String testName = optimum.name();
// if (testName.isEmpty()) {
// testName = iTestResult.getMethod().getMethodName();
// }
//
// Log.addLog(testName + " is FAILED”);
// }
}
private Annotation getAnnotation(ITestResult result, Class T) {
return result.getMethod().getConstructorOrMethod().getMethod().getAnnotation(T);
}
@Override
public void onTestSkipped(ITestResult iTestResult) {
if (iTestResult != null) {
String testName = iTestResult.getName();
Log.addLog(testName + " is SKIPPED...");
ExtentTest extent = ExtentReport.shared().getExtentTest(testName);
ExtentReport.Log(extent,3);
}
// Annotation annotation = getAnnotation(iTestResult, Optimum.class);
// if (annotation != null) {
// Optimum optimum = (Optimum) annotation;
// String testName = optimum.name();
// if (testName.isEmpty()) {
// testName = iTestResult.getMethod().getMethodName();
// }
//
// Log.addLog(testName + " is SKIPPED");
// }
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {
Log.addLog(" onTestFailedButWithinSuccessPercentage ");
}
@Override
public void onStart(ITestContext iTestContext) {
Log.addLog("=====START TEST=====");
}
@Override
public void onFinish(ITestContext iTestContext) {
Log.addLog("=====FINISH TEST=====");
}
}]