Comments (3)
package CustomTestListener;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
/**
-
Created by ID850378 on 17/07/2017.
*/
public class CustomTestListener implements ITestListener {private static String dateTimeFormat = "yyyy-MM-dd_kk-mm";
private static String executionReportsHome = "target";
private static String FILE_NAME = "Report_%s.html";
private static ExtentReports extent;
private static ExtentHtmlReporter htmlReporter;
private static ExtentTest currentTest;private String getDateString(){
Date now = Calendar.getInstance().getTime();
SimpleDateFormat format = new SimpleDateFormat(dateTimeFormat);
return format.format(now);
}public void onStart(ITestContext iTestContext) {
// making sure paths are in place FILE_NAME = String.format(FILE_NAME, "Dipika"); // configuring the extent reporter htmlReporter = new ExtentHtmlReporter(executionReportsHome+"/"+FILE_NAME); htmlReporter.config().setDocumentTitle("ExtentReports - Created by TestNG Listener"); htmlReporter.config().setReportName("ExtentReports - Created by TestNG Listener"); htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP); htmlReporter.config().setChartVisibilityOnOpen(true); htmlReporter.config().setTheme(Theme.STANDARD); extent = new ExtentReports(); extent.attachReporter(htmlReporter); extent.setReportUsesManualConfiguration(true);
}
private void assignCategories(ITestResult iTestResult){
ArrayList<String> labels = new ArrayList<String>(); String className = iTestResult.getTestClass().getRealClass().getName(); //String methodName = iTestResult.getMethod().getMethodName(); String[] groupName = iTestResult.getMethod().getGroups(); //labels.addAll(Arrays.asList(className.split("\\."))); labels.addAll(Arrays.asList(className)); labels.addAll(Arrays.asList(groupName));
//labels.addAll(Arrays.asList(methodName.split("\.")));
for (String label : labels) { currentTest.assignCategory(label.toLowerCase()); //currentTest.assignAuthor(label.toLowerCase()); }
}
@BeforeSuite
public void onTestStart(ITestResult iTestResult) {
try {
String testName = iTestResult.getMethod().getMethodName();
String[] test = iTestResult.getMethod().getGroups();
currentTest = extent.createTest(testName);
assignCategories(iTestResult);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onTestSuccess(ITestResult iTestResult) {
currentTest.pass("<strong>TEST PASSED!!!!!!!</strong>");
currentTest.getClass().getPackage();
currentTest.getModel().getDescription();
}
public void onTestFailure(ITestResult iTestResult) {
try {
String testName = iTestResult.getMethod().getMethodName();
String screenshotName = testName+".jpg";
if(null != iTestResult.getThrowable()){
currentTest.fail( iTestResult.getThrowable().getMessage() );
//currentTest.fail(iTestResult.getThrowable().getStackTrace().toString());
currentTest.fail( iTestResult.toString());
Throwable exception = iTestResult.getThrowable();
if(exception!=null)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
currentTest.fail(sw.toString());
}
}else {
currentTest.fail("Could not get cause!!!");
}
currentTest.addScreenCaptureFromPath(screenshotName);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onTestSkipped(ITestResult iTestResult) {
try {
if(null == iTestResult.getThrowable()){
currentTest.skip("<strong>TEST SKIPPED!!!!!!!</strong>");
}else {
currentTest.skip(iTestResult.getThrowable().getMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {
try {
onTestFailure(iTestResult);
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterSuite
public void onFinish(ITestContext iTestContext) {
try {
htmlReporter.flush();
htmlReporter.stop();
extent.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
from extentreports-java.
from extentreports-java.
Did you see the examples section already for ITestListener?
http://extentreports.com/docs/versions/3/java/
It should work as-is and you can customize it to output the info you have in the code
from extentreports-java.
Related Issues (20)
- HTML Report showing some french characters as question mark (?) HOT 1
- Unable to generate report using version 3.1.5 HOT 1
- removeTest(extentTest) doesn't remove the test from the extent report HOT 2
- why extentTest.log(LogStatus ls, String testStep, String testDescription) method removed in version 3.1.5?? HOT 4
- Issue in extent.flush()
- Extent Report shows me all the TestStep Info of Second Test Case in the First Test Case HOT 3
- How to Customize image size in extentreport HOT 1
- Need the Extent Report 4.0 Jar HOT 1
- hi anshoo, kindly help me, c# latest extentreports dll doesnt have loadconfig and display order constructor pls help me what are the latest replacements for these
- Not able to change date/time format by customizing config file HOT 3
- How to display time between each step in extent report.
- Extent Report does not show test as fail if it fails before logging pass status of test to extent report OR if extent end test is not executed in Selenium C#
- How to add custom react js component
- Executing Cusom javascript/ adding html file
- Extent Report 3.1.5 - Null Pointer Exception with ITestListener HOT 1
- My screenshot is not adding to the extent report. HOT 1
- I am using this Extentreport But i am not able to Put log for different tests, Can you please help me out to put Log using this Extentreports HOT 2
- Can we overwrite any test result in an existing extent report xml after rerun of the failures
- Incorrect Total Time Taken (Sum of Time of All Test Scripts Executed) in Dashboard HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from extentreports-java.