Permalink
Browse files

Added emailable report feature

  • Loading branch information...
email2vimalraj committed Dec 24, 2016
1 parent e5bcabc commit ec079118691aac48f473e09cc92445ad8ca3f768
@@ -9,14 +9,16 @@ This listener uses the [ExtentReports v3.0.0](http://extentreports.relevantcodes
## Why another library?
You don't have to spend time in re-inventing on how to define what. Just add this as a listener or [service loader](http://testng.org/doc/documentation-main.html#listeners-service-loader). You will get the nice looking report generated at the end of your test execution.
Also, you will get the **emailable report** out-of-the box.
## Usage
For maven, add the following as dependency:
```
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>testng-extentsreport</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</dependency>
```
@@ -30,11 +32,11 @@ Either in your **testng.xml**, add the listener:
or add as a [service loader](http://testng.org/doc/documentation-main.html#listeners-service-loader) (recommended).
By default, the report will be generated at TestNG's output directory. i.e., `test-output/report.html`.
By default, the report will be generated at TestNG's output directory. i.e., `test-output/report.html` and the emailable report at `test-output/emailable-report.html`.
In case you want to generate the report in a different location, make sure you pass the JVM argument called `extentReportPath` with the absolute file path.
In case you want to generate the report in a different location, make sure you pass the JVM argument called `reportPath` with the absolute directory path.
*For example*: I run my maven test as: `mvn clean test -DreportPath=/Users/vselvam/myproject/output/report.html`
*For example*: I run my maven test as: `mvn clean test -DreportPath=output`. This will generate both the reports in my `${project directory}/output`.
### Adding custom reporter config
You can customize the report using a XML file. The XML file should follow as given here: [ExtentReports Configuration](http://extentreports.relevantcodes.com/java/#configuration).
@@ -6,7 +6,7 @@
<groupId>com.vimalselvam</groupId>
<artifactId>testng-extentsreport</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
<packaging>jar</packaging>
<name>TestNG Extents Report</name>
@@ -0,0 +1,64 @@
package com.vimalselvam.testng;
import com.aventstack.extentreports.model.Test;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.utils.Writer;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This class houses logic to generate the emailable report
*/
public class EmailReporter extends ExtentHtmlReporter {
private class Marker {
}
private static final Logger logger = Logger.getLogger(Marker.class.getEnclosingClass().getName());
private List<Test> parsedTestCollection;
public EmailReporter(String filePath) {
super(filePath);
}
public EmailReporter(File file) {
this(file.getAbsolutePath());
}
@Override
public synchronized void flush() {
if (testList == null || testList.size() == 0) {
return;
}
if (parsedTestCollection != null && parsedTestCollection.size() > 0) {
for (int ix = 0; ix < parsedTestCollection.size(); ix++) {
testList.add(ix, parsedTestCollection.get(ix));
}
}
parsedTestCollection = null;
setEndTime(Calendar.getInstance().getTime());
String extentSource = null;
try {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
cfg.setClassForTemplateLoading(Marker.class.getEnclosingClass(), "view");
cfg.setDefaultEncoding("UTF-8");
Template template = cfg.getTemplate("email.ftl");
StringWriter out = new StringWriter();
template.process(templateMap, out);
extentSource = out.toString();
out.close();
} catch (IOException | TemplateException e) {
logger.log(Level.SEVERE, "Template not found", e);
}
Writer.getInstance().write(new File(filePath), extentSource);
}
}
@@ -5,6 +5,7 @@
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.vimalselvam.testng.EmailReporter;
import com.vimalselvam.testng.NodeName;
import com.vimalselvam.testng.SystemInfo;
import org.testng.IInvokedMethod;
@@ -41,19 +42,28 @@
public ExtentTestNgFormatter() {
setInstance(this);
testRunnerOutput = new ArrayList<>();
String reportPath = System.getProperty("extentReportPath");
if (reportPath == null) {
File file = new File(TestNG.DEFAULT_OUTPUTDIR);
if (!file.exists()) {
if (!file.mkdirs()) {
throw new RuntimeException("Failed to create output run directory");
}
String reportPathStr = System.getProperty("reportPath");
File reportPath;
try {
reportPath = new File(reportPathStr);
} catch (NullPointerException e) {
reportPath = new File(TestNG.DEFAULT_OUTPUTDIR);
}
if (!reportPath.exists()) {
if (!reportPath.mkdirs()) {
throw new RuntimeException("Failed to create output run directory");
}
reportPath = file.getAbsolutePath() + File.separator + "report.html";
}
htmlReporter = new ExtentHtmlReporter(reportPath);
File reportFile = new File(reportPath, "report.html");
File emailReportFile = new File(reportPath, "emailable-report.html");
htmlReporter = new ExtentHtmlReporter(reportFile);
EmailReporter emailReporter = new EmailReporter(emailReportFile);
reporter = new ExtentReports();
reporter.attachReporter(htmlReporter);
reporter.attachReporter(htmlReporter, emailReporter);
}
/**
@@ -0,0 +1,69 @@
<#assign systemAttributeContext=report.getSystemAttributeContext().getSystemAttributeList()>
<#assign pass="#8CA93E">
<#assign fail="#FF7F7F">
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Emailable Report</title>
</head>
<body>
<h1>Emailable Report</h1>
<#list systemAttributeContext>
<p>
<em>System Configuration</em><br />
<#items as sysConfig>
<strong>${sysConfig.name}</strong>: ${sysConfig.value}<#sep>,
</#items>
</p>
</#list>
<#list report.testList>
<table width="100%" border="1" cellspacing="2" cellpadding="10" style="border-collapse: collapse; display: table;">
<tbody>
<tr>
<th>Test Method</th>
<th>Status</th>
<th>Start Time</th>
<th>Duration</th>
</tr>
<#items as parent>
<#list parent.nodeContext.all>
<#items as child>
<tr>
<th colspan="4">Test Name: <em>${child.name}</em> (Suite: <em>${parent.name}</em>)</th>
</tr>
<#list child.nodeContext.all>
<#items as grandchild>
<tr bgcolor=<#if grandchild.status=="pass">${pass}<#else>${fail}</#if>>
<td>
<font color="white"><em>${grandchild.name}</em></font>
</td>
<td>
<font color="white">${grandchild.status?upper_case}</font>
</td>
<td>
<font color="white">${grandchild.startTime?datetime?string}</font>
</td>
<td>
<font color="white">${grandchild.getRunDuration()?string}</font>
</td>
</tr>
</#items>
</#list>
</#items>
</#list>
</#items>
</tbody>
</table>
</#list>
<p style="color: gray; font-size: 10px; text-align: right;">
Reports by <a href="http://vimalselvam.com">vimalselvam.com</a>
</p>
</body>
</html>
@@ -1,10 +1,8 @@
package test;
import com.vimalselvam.testng.listener.ExtentTestNgFormatter;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import java.io.IOException;
@@ -13,10 +11,6 @@
* This is a sample TestNG test.
*/
public class TestNgTest {
// The instance of the ExtentTestNgFormatter can be obtained as follows
private ExtentTestNgFormatter formatter = ExtentTestNgFormatter.getInstance();
@AfterMethod
public void afterMethod(ITestResult iTestResult) throws IOException {
Reporter.log("After Method: " + iTestResult.getMethod().getMethodName());

0 comments on commit ec07911

Please sign in to comment.