Skip to content

Commit 4057d1b

Browse files
committed
#19 ExtentSparkReporter
1 parent 4cc95d3 commit 4057d1b

File tree

20 files changed

+1338
-10
lines changed

20 files changed

+1338
-10
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.aventstack</groupId>
66
<artifactId>extentreports</artifactId>
7-
<version>4.0.6</version>
7+
<version>4.0.7-SNAPSHOT</version>
88

99
<name>extentreports</name>
1010
<url>www.extentreports.com</url>

src/main/java/com/aventstack/extentreports/ExtentTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,14 +1104,14 @@ public ExtentTest assignAuthor(String... author) {
11041104
*/
11051105
public ExtentTest assignDevice(String... device) {
11061106
Arrays.stream(device)
1107-
.filter(StringUtil::isNotNullOrEmpty)
1108-
.forEach(x -> {
1109-
Device d = new Device(x.replace(" ", ""));
1110-
test.setDevice(d);
1111-
extent.assignDevice(test, d);
1112-
}
1113-
);
1114-
return this;
1107+
.filter(StringUtil::isNotNullOrEmpty)
1108+
.forEach(x -> {
1109+
Device d = new Device(x.replace(" ", ""));
1110+
test.setDevice(d);
1111+
extent.assignDevice(test, d);
1112+
}
1113+
);
1114+
return this;
11151115
}
11161116

11171117
@Override

src/main/java/com/aventstack/extentreports/model/Media.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import org.bson.types.ObjectId;
66

7+
import com.aventstack.extentreports.utils.FileUtil;
8+
79
public class Media
810
implements Serializable {
911

@@ -18,6 +20,7 @@ public class Media
1820
private String path;
1921
private String base64String;
2022
private int seq;
23+
private long fileSize = 0;
2124

2225
private MediaType mediaType;
2326

@@ -71,12 +74,23 @@ protected String getDescription() {
7174

7275
public void setPath(String path) {
7376
this.path = path;
77+
setFileSize(FileUtil.getFileSize(path));
78+
if (getName() == null || getName().isEmpty())
79+
setName(FileUtil.getFileName(path));
7480
}
7581

7682
public String getPath() {
7783
return path;
7884
}
7985

86+
public long getFileSize() {
87+
return fileSize;
88+
}
89+
90+
public void setFileSize(long fileSize) {
91+
this.fileSize = fileSize;
92+
}
93+
8094
public void setMediaType(MediaType mediaType) {
8195
this.mediaType = mediaType;
8296
}

src/main/java/com/aventstack/extentreports/model/ScreenCapture.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public String getSourceWithIcon() {
1616
return "<a href='#' class='indigo badge' data-featherlight='" + getScreenCapturePath() + "'>img</a>";
1717
}
1818

19-
private String getScreenCapturePath() {
19+
public String getScreenCapturePath() {
2020
return getPath() != null ? getPath() : getBase64String();
2121
}
2222

src/main/java/com/aventstack/extentreports/model/Test.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ public String getHierarchicalName() {
348348
return hierarchicalName;
349349
}
350350

351+
public boolean hasAttributes() {
352+
return hasAuthor() || hasCategory() || hasDevice();
353+
}
354+
351355
public AbstractStructure<TestAttribute> getCategoryContext() {
352356
if (category == null) {
353357
category = new AbstractStructure<>();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.aventstack.extentreports.reporter;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.logging.Level;
6+
import java.util.logging.Logger;
7+
8+
import com.aventstack.extentreports.ReportAggregates;
9+
import com.aventstack.extentreports.reporter.configuration.ExtentSparkReporterConfiguration;
10+
11+
import freemarker.template.Template;
12+
import freemarker.template.TemplateException;
13+
14+
/**
15+
* The ExtentSparkReporter creates a rich standalone spark file. It allows several
16+
* configuration options via the <code>config()</code> method.
17+
*/
18+
public class ExtentSparkReporter extends BasicFileReporter {
19+
20+
private static final Logger logger = Logger.getLogger(ExtentSparkReporter.class.getName());
21+
private static final String REPORTER_NAME = "spark";
22+
private static final String TEST_TEMPLATE_NAME = REPORTER_NAME + "/test.ftl";
23+
private static final String TAG_TEMPLATE_NAME = REPORTER_NAME + "/tag.ftl";
24+
private static final String EXCEPTION_TEMPLATE_NAME = REPORTER_NAME + "/exception.ftl";
25+
private static final String DASHBOARD_TEMPLATE_NAME = REPORTER_NAME + "/dashboard.ftl";
26+
private static final String[] DEFAULT_CONFIG_FILE_PATH = new String[] { "spark.properties",
27+
"src/main/resources/spark.properties" };
28+
29+
private ExtentSparkReporterConfiguration userConfig = new ExtentSparkReporterConfiguration(this);
30+
31+
public ExtentSparkReporter(String path) {
32+
super(path);
33+
init(DEFAULT_CONFIG_FILE_PATH, config());
34+
}
35+
36+
public ExtentSparkReporter(File file) {
37+
super(file);
38+
init(DEFAULT_CONFIG_FILE_PATH, config());
39+
}
40+
41+
public ExtentSparkReporterConfiguration config() {
42+
return userConfig;
43+
}
44+
45+
@Override
46+
public synchronized void flush(ReportAggregates reportAggregates) {
47+
super.flush(reportAggregates);
48+
49+
if (getTestList().isEmpty())
50+
return;
51+
52+
loadUserConfig();
53+
54+
try {
55+
Template template = getFreemarkerConfig().getTemplate(TEST_TEMPLATE_NAME);
56+
processTemplate(template, new File(destination + "index.html"));
57+
if (!getCategoryContextInfo().getTestAttributeTestContextList().isEmpty()) {
58+
template = getFreemarkerConfig().getTemplate(TAG_TEMPLATE_NAME);
59+
processTemplate(template, new File(destination + "tag.html"));
60+
}
61+
if (!getExceptionContextInfo().getExceptionTestContextList().isEmpty()) {
62+
template = getFreemarkerConfig().getTemplate(EXCEPTION_TEMPLATE_NAME);
63+
processTemplate(template, new File(destination + "exception.html"));
64+
}
65+
template = getFreemarkerConfig().getTemplate(DASHBOARD_TEMPLATE_NAME);
66+
processTemplate(template, new File(destination + "dashboard.html"));
67+
} catch (IOException | TemplateException e) {
68+
logger.log(Level.SEVERE, "An exception occurred", e);
69+
}
70+
}
71+
72+
@Override
73+
public String getReporterName() {
74+
return REPORTER_NAME;
75+
}
76+
77+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.aventstack.extentreports.reporter.configuration;
2+
3+
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
4+
5+
/**
6+
* Defines configuration settings for the Spark reporter
7+
*/
8+
public class ExtentSparkReporterConfiguration
9+
extends RichViewReporterConfiguration {
10+
11+
public ExtentSparkReporterConfiguration(ExtentSparkReporter reporter) {
12+
super(reporter);
13+
}
14+
15+
}

src/main/java/com/aventstack/extentreports/utils/FileUtil.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
public class FileUtil {
99

1010
private FileUtil() { }
11+
12+
public static String getFileName(File f) {
13+
if (f == null || !f.exists())
14+
return "";
15+
return f.getName();
16+
}
17+
18+
public static String getFileName(String path) {
19+
return getFileName(new File(path));
20+
}
1121

1222
public static String getFileNameWithoutExtension(File f) {
1323
String name = f.getName();
@@ -56,4 +66,11 @@ public static Boolean fileExists(String path) {
5666
return f.exists() && !f.isDirectory();
5767
}
5868

69+
public static long getFileSize(String path) {
70+
if (path == null)
71+
return 0;
72+
File f = new File(path);
73+
return f.exists() ? f.length() : 0;
74+
}
75+
5976
}

0 commit comments

Comments
 (0)