Reputation: 53
I'm integrating html-reports to our test framework using Extent Report library (version 4).
For that purpose I wrote a wrapper, that encapsulates our default TestNG logger (for the console output) and ExtentReport logger (ExtentTest.log, to collect data for html-report). We use TestNg as our testing framework.
Having an issue capturing log messages from failed assertions (both soft and hard Assertions) to display it in html-report, they go only to the console log. What would be the possible solution to capture those Assert log messages to show them in html-report?
I can't extend Assert (or SoftAssert) classes and add my own implementation (by adding an instance of ExtentTest.log there) because I'd have to replace all assertions in dozens of tests.
public class Loggers {
private static final Loggers instance = new Loggers();
private static ExtentTest test;
private static ExtentReports extent;
private static Logger LOG;
private static final String REPORT_LOCATION = "test-output/reports.extent.html";
/**
* Returns an instance of {@link ExtentReports} object. If it doesn't exist creates a new instance and returns it
*/
public static ExtentReports getLogger() {
if ( extent == null ) {
createInstance();
}
return extent;
}
/**
* Create ExtentReport and attaches htmlReporter to it
*/
public static void createInstance() {
ExtentHtmlReporter htmlReporter = getHTMLReporter();
extent = new ExtentReports();
extent.attachReporter( htmlReporter );
}
/**
* This method creates, configures and returns an instance of ExtentHtmlReporter
*
*/
public static ExtentHtmlReporter getHTMLReporter() {
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter( REPORT_LOCATION );
return htmlReporter;
}
/**
* This method logs a message with the INFO level for both instances of TestNG Logger and ExtentTest
*/
public void info( String message ) {
LOG.info( message );
test.log( Status.INFO, message );
}
Upvotes: 3
Views: 6357
Reputation: 96
The Below Code Works for me for Extent Report Try it!
1) Initialize Extent report and Logger
public static Logger log = Logger.getLogger("devpinoyLogger");
public ExtentReports rep = ExtentManager.getInstance();
public static ExtentTest test;
2) Use Configuration File ReportsConfig.xml for Extent Report that you can get from extentreports official site.
3) Create class ExtentManager that load config file and set output of Extent report file.
public class ExtentManager {
private static ExtentReports extent;
public static ExtentReports getInstance(){
if(extent==null){
System.out.println("Path of user DIR"+System.getProperty("user.dir"));
extent = new ExtentReports(System.getProperty("user.dir")+"\\target\\surefire-reports\\ExecutionReport.html",true,DisplayOrder.OLDEST_FIRST);
extent.loadConfig(new File(System.getProperty("user.dir")+"\\src\\main\\java\\extentconfig\\ReportsConfig.xml"));
}
return extent;
}
}
4) Use INFO and ERROR log in method you are creating to display logs in Extent report.
public void click(String xpath) {
try {
driver.findElement(By.xpath(Variables.OR.getProperty(xpath))).click();
System.out.println(xpath + "Button clicked");
test.log(LogStatus.INFO, xpath + " Button clicked");
Thread.sleep(1000);
} catch (Exception e) {
System.err.println("Cannot Click " + e.getMessage());
test.log(LogStatus.ERROR,"Unable to click on :: " + xpath + " Button");
throw new AssertionError("Unable to click on :: " + xpath + " Button", e);
}
}
5) Use Custom listener class CustomListeners
public class CustomListeners extends TestBase implements ITestListener, ISuiteListener {
public boolean flag;
..implement all methods of CustomListeners class and use logs in onTestSuccess and onTestFailure Methods.
public void onTestSuccess(ITestResult arg0) {
test.log(LogStatus.PASS, arg0.getName().toUpperCase() + " PASS");
rep.endTest(test);
rep.flush();
}
public void onTestFailure(ITestResult arg0) {
System.out.println(arg0 + " =================Test Case Failed===========================");
flag = true;
System.out.println("Flag is inside onTestFailure " + flag);
System.setProperty("org.uncommons.reportng.escape-output", "false");
try {
test.log(LogStatus.FAIL, arg0.getName().toUpperCase() + " Failed with exception : " + arg0.getThrowable());
rep.endTest(test);
rep.flush();
} catch (IOException e) {
System.err.println("IOException occurs " + e.getMessage());
e.printStackTrace();
}
}
}
Upvotes: 0
Reputation: 53
Found the solution. Will post here if somebody will have similar issue.
If you locate your loggers in a listener class, you can put your logger over there and use ITestResult as an argument, specifically its method getThrowable (It returns the throwable that was thrown while running the method)
/**
* Triggered upon the failure of a test
*/
public void onTestFailure( ITestResult testResult ) {
LOG.fail( testResult.getThrowable() );
}
It will print in the report a failed assertion or thrown exception.
Upvotes: 2