Skip to content

Commit 182c433

Browse files
grasshopper7grass-hopper-mocanshooarora
authored
Add video display in tests (#400)
* Add video display in tests * Fix tests * Fix template spacing (for minify) * Fix GherkinDialect related tests * Update maven.yml (remove update dependency graph) This was causing several 403 issues: https://github.com/extent-framework/extentreports-java/actions/runs/9579125973/job/26410971673?pr=400 ``` Error: HTTP Status 403 for request POST https://api.github.com/repos/extent-framework/extentreports-java/dependency-graph/snapshots Error: Response body: { "message": "Resource not accessible by integration", "documentation_url": "https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository", "status": "403" } Error: Resource not accessible by integration Error: HttpError: Resource not accessible by integration at /home/runner/work/_actions/advanced-security/maven-dependency-submission-action/571e99aab1055c2e71a1e2309b9691de18d6b7d6/webpack:/maven-dependency-tree-action/node_modules/@github/dependency-submission-toolkit/dist/index.js:5317:1 at processTicksAndRejections (node:internal/process/task_queues:96:5) /home/runner/work/_actions/advanced-security/maven-dependency-submission-action/571e99aab1055c2e71a1e2309b9691de18d6b7d6/webpack:/maven-dependency-tree-action/node_modules/@github/dependency-submission-toolkit/dist/index.js:396 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } ^ Error: Failed to submit snapshot: HttpError: Resource not accessible by integration at /home/runner/work/_actions/advanced-security/maven-dependency-submission-action/571e99aab1055c2e71a1e2309b9691de18d6b7d6/webpack:/maven-dependency-tree-action/node_modules/@github/dependency-submission-toolkit/dist/index.js:499:1 at Generator.throw (<anonymous>) at rejected (/home/runner/work/_actions/advanced-security/maven-dependency-submission-action/571e99aab1055c2e71a1e2309b9691de18d6b7d6/webpack:/maven-dependency-tree-action/node_modules/@github/dependency-submission-toolkit/dist/index.js:396:1) at processTicksAndRejections (node:internal/process/task_queues:96:5) ``` --------- Co-authored-by: grasshopper7 <[email protected]> Co-authored-by: Anshoo Arora <[email protected]>
1 parent 41ae106 commit 182c433

26 files changed

+749
-74
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.aventstack.extentreports.model.RunResult;
1616
import com.aventstack.extentreports.model.ScreenCapture;
1717
import com.aventstack.extentreports.model.Test;
18+
import com.aventstack.extentreports.model.Video;
1819
import com.aventstack.extentreports.model.service.ExceptionInfoService;
1920
import com.aventstack.extentreports.util.Assert;
2021

@@ -1110,7 +1111,7 @@ public ExtentTest addScreenCaptureFromPath(String path) {
11101111
public ExtentTest addScreenCaptureFromBase64String(String base64, String title) {
11111112
Assert.notEmpty(base64, "ScreenCapture's base64 string must not be null or empty");
11121113
if (!base64.startsWith("data:"))
1113-
base64 = "data:image/png;base64," + base64;
1114+
base64 = ScreenCapture.BASE64_ENCODED + base64;
11141115
Media m = ScreenCapture.builder().base64(base64).title(title).build();
11151116
model.addMedia(m);
11161117
extent.onMediaAdded(m, model);
@@ -1120,4 +1121,36 @@ public ExtentTest addScreenCaptureFromBase64String(String base64, String title)
11201121
public ExtentTest addScreenCaptureFromBase64String(String base64) {
11211122
return addScreenCaptureFromBase64String(base64, null);
11221123
}
1124+
1125+
public ExtentTest addVideoFromPath(String path, String title) {
1126+
Assert.notEmpty(path, "Video path must not be null or empty");
1127+
Media m = Video.builder().path(path).title(title).build();
1128+
model.addMedia(m);
1129+
extent.onMediaAdded(m, model);
1130+
return this;
1131+
}
1132+
1133+
public ExtentTest addVideoFromPath(String path) {
1134+
return addVideoFromPath(path, null);
1135+
}
1136+
1137+
public ExtentTest addVideoFromBase64String(String base64, String title) {
1138+
Assert.notEmpty(base64, "Video's base64 string must not be null or empty");
1139+
if (!base64.startsWith("data:"))
1140+
base64 = Video.BASE64_ENCODED + base64;
1141+
Media m = Video.builder().base64(base64).title(title).build();
1142+
model.addMedia(m);
1143+
extent.onMediaAdded(m, model);
1144+
return this;
1145+
}
1146+
1147+
public ExtentTest addVideoFromBase64String(String base64) {
1148+
return addVideoFromBase64String(base64, null);
1149+
}
1150+
1151+
public ExtentTest addMedia(Media m) {
1152+
model.addMedia(m);
1153+
extent.onMediaAdded(m, model);
1154+
return this;
1155+
}
11231156
}

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import com.aventstack.extentreports.model.Media;
44
import com.aventstack.extentreports.model.ScreenCapture;
5+
import com.aventstack.extentreports.model.Video;
56
import com.aventstack.extentreports.util.Assert;
67

78
/**
89
* Media builder for base64 and binary screenshots
910
*
1011
*/
1112
public class MediaEntityBuilder {
12-
private static final String BASE64_ENCODED = "data:image/png;base64,";
13+
1314
private static ThreadLocal<Media> media = new ThreadLocal<>();
1415

1516
private static class MediaBuilderInstance {
@@ -43,12 +44,34 @@ public static MediaEntityBuilder createScreenCaptureFromPath(String path) {
4344
public static MediaEntityBuilder createScreenCaptureFromBase64String(String base64, String title) {
4445
Assert.notEmpty(base64, "ScreenCapture's base64 string must not be null or empty");
4546
if (!base64.startsWith("data:"))
46-
base64 = BASE64_ENCODED + base64;
47+
base64 = ScreenCapture.BASE64_ENCODED + base64;
4748
media.set(ScreenCapture.builder().base64(base64).title(title).build());
4849
return getInstance();
4950
}
5051

5152
public static MediaEntityBuilder createScreenCaptureFromBase64String(String base64) {
5253
return createScreenCaptureFromBase64String(base64, null);
5354
}
55+
56+
public static MediaEntityBuilder createVideoFromPath(String path, String title) {
57+
Assert.notEmpty(path, "Video path must not be null or empty");
58+
media.set(Video.builder().path(path).title(title).build());
59+
return getInstance();
60+
}
61+
62+
public static MediaEntityBuilder createVideoFromPath(String path) {
63+
return createVideoFromPath(path, null);
64+
}
65+
66+
public static MediaEntityBuilder createVideoFromBase64String(String base64, String title) {
67+
Assert.notEmpty(base64, "Video's base64 string must not be null or empty");
68+
if (!base64.startsWith("data:"))
69+
base64 = Video.BASE64_ENCODED + base64;
70+
media.set(Video.builder().base64(base64).title(title).build());
71+
return getInstance();
72+
}
73+
74+
public static MediaEntityBuilder createVideoFromBase64String(String base64) {
75+
return createVideoFromBase64String(base64, null);
76+
}
5477
}

src/main/java/com/aventstack/extentreports/append/JsonDeserializer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@
1313
import com.google.gson.reflect.TypeToken;
1414

1515
public class JsonDeserializer {
16-
private File f;
16+
private final File f;
1717

1818
public JsonDeserializer(final File f) {
1919
this.f = f;
2020
}
2121

2222
public List<Test> deserialize() throws IOException {
2323
Gson gson = GsonExtentTypeAdapterBuilder.builder()
24+
.withMediaTypeAdapter()
2425
.withGsonTypeAdapterFactory()
25-
.withScreenCaptureTypeAdapter()
2626
.build();
2727
String json = new String(Files.readAllBytes(f.toPath()));
2828
Type t = new TypeToken<ArrayList<Test>>(){}.getType();
29-
List<Test> tests = gson.fromJson(json, t);
30-
return tests;
29+
return gson.fromJson(json, t);
3130
}
3231
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.aventstack.extentreports.append;
2+
3+
import java.io.IOException;
4+
5+
import com.aventstack.extentreports.model.Media;
6+
import com.aventstack.extentreports.model.service.MediaService;
7+
import com.google.gson.TypeAdapter;
8+
import com.google.gson.stream.JsonReader;
9+
import com.google.gson.stream.JsonToken;
10+
import com.google.gson.stream.JsonWriter;
11+
12+
public class MediaTypeAdapter extends TypeAdapter<Media> {
13+
14+
@Override
15+
public void write(JsonWriter out, Media value) throws IOException {
16+
}
17+
18+
@Override
19+
public Media read(JsonReader reader) throws IOException {
20+
reader.beginObject();
21+
String fieldName = null;
22+
int cycle = 0;
23+
24+
String type = null;
25+
String path = null;
26+
String resolvedPath = null;
27+
String title = null;
28+
String base64 = null;
29+
30+
while (reader.hasNext()) {
31+
JsonToken token = reader.peek();
32+
if (token.equals(JsonToken.NAME)) {
33+
fieldName = reader.nextName();
34+
}
35+
if ("string".equalsIgnoreCase(token.name()) && fieldName.equalsIgnoreCase("type")) {
36+
token = reader.peek();
37+
type = reader.nextString();
38+
}
39+
if ("string".equalsIgnoreCase(token.name()) && fieldName.equalsIgnoreCase("path")) {
40+
token = reader.peek();
41+
path = reader.nextString();
42+
}
43+
if ("string".equalsIgnoreCase(token.name()) && fieldName.equalsIgnoreCase("resolvedPath")) {
44+
token = reader.peek();
45+
resolvedPath = reader.nextString();
46+
}
47+
if ("string".equalsIgnoreCase(token.name()) && fieldName.equalsIgnoreCase("title")) {
48+
token = reader.peek();
49+
title = reader.nextString();
50+
}
51+
if ("string".equalsIgnoreCase(token.name()) && fieldName.equalsIgnoreCase("base64")) {
52+
token = reader.peek();
53+
base64 = reader.nextString();
54+
}
55+
if (cycle++ > 10)
56+
return MediaService.createMedia(type, path, resolvedPath, title, base64);
57+
}
58+
reader.endObject();
59+
return MediaService.createMedia(type, path, resolvedPath, title, base64);
60+
}
61+
}

src/main/java/com/aventstack/extentreports/append/RawEntityConverter.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,9 @@ public void createDomain(final Test test, final ExtentTest extentTest) throws Cl
8282

8383
private void constructLog(final Log log, final ExtentTest extentTest, final ExceptionInfo ex) {
8484
final Media m = log.getMedia();
85-
86-
if (m != null) {
87-
if (m.getPath() != null) {
88-
extentTest.log(log.getStatus(),
89-
MediaEntityBuilder.createScreenCaptureFromPath(m.getPath()).build());
90-
}
91-
if (((ScreenCapture) m).getBase64() != null) {
92-
extentTest.log(log.getStatus(),
93-
MediaEntityBuilder.createScreenCaptureFromBase64String(((ScreenCapture) m).getBase64()).build());
94-
}
95-
}
85+
extentTest.log(log.getStatus(), log.getDetails(), m);
9686

9787
if (ex != null) {
98-
if (!extentTest.getModel().hasLog()) {
99-
extentTest.log(log.getStatus(), log.getDetails());
100-
}
101-
10288
final List<Log> logs = extentTest.getModel().getLogs();
10389
final Log lastLog = logs.get(logs.size() - 1);
10490
lastLog.setException(ex);
@@ -109,11 +95,7 @@ private void constructLog(final Log log, final ExtentTest extentTest, final Exce
10995
private void constructTestMedia(final Test test, final ExtentTest extentTest) {
11096
if (test.getMedia() != null) {
11197
for (Media m : test.getMedia()) {
112-
if (m.getPath() != null) {
113-
extentTest.addScreenCaptureFromPath(m.getPath());
114-
} else if (m instanceof ScreenCapture) {
115-
extentTest.addScreenCaptureFromBase64String(((ScreenCapture) m).getBase64());
116-
}
98+
extentTest.addMedia(m);
11799
}
118100
}
119101
}

src/main/java/com/aventstack/extentreports/gson/GsonExtentTypeAdapterBuilder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.aventstack.extentreports.gson;
22

3-
import com.aventstack.extentreports.append.ScreenCaptureTypeAdapter;
3+
import com.aventstack.extentreports.append.MediaTypeAdapter;
44
import com.aventstack.extentreports.model.Media;
55
import com.google.gson.Gson;
66
import com.google.gson.GsonBuilder;
@@ -20,8 +20,9 @@ public Builder withGsonTypeAdapterFactory() {
2020
return this;
2121
}
2222

23-
public Builder withScreenCaptureTypeAdapter() {
24-
builder.registerTypeAdapter(Media.class, new ScreenCaptureTypeAdapter());
23+
public Builder withMediaTypeAdapter() {
24+
//builder.registerTypeAdapter(Media.class, new ScreenCaptureTypeAdapter());
25+
builder.registerTypeAdapter(Media.class, new MediaTypeAdapter());
2526
return this;
2627
}
2728

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,20 @@ public final boolean hasException() {
4141

4242
public final void addMedia(Media media) {
4343
if (media != null && ((media.getPath() != null || media.getResolvedPath() != null)
44-
|| media instanceof ScreenCapture && ((ScreenCapture) media).getBase64() != null))
44+
|| (media instanceof ScreenCapture && ((ScreenCapture) media).getBase64() != null)
45+
|| (media instanceof Video && ((Video) media).getBase64() != null)))
4546
this.media = media;
4647
}
4748

4849
public final boolean hasMedia() {
4950
return media != null;
5051
}
52+
53+
public final boolean hasScreenCapture() {
54+
return media != null && media instanceof ScreenCapture;
55+
}
56+
57+
public final boolean hasVideo() {
58+
return media != null && media instanceof Video;
59+
}
5160
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
public class Media implements Serializable, BaseEntity, MetaDataStorable {
1414
private static final long serialVersionUID = 5428859443090457608L;
1515

16+
private MediaType type;
1617
private String path;
1718
private String title;
1819
private String resolvedPath;
1920
private transient Map<String, Object> infoMap;
21+
22+
public enum MediaType {
23+
SCREENCAPTURE, VIDEO
24+
}
2025
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
@Getter
1111
@Setter
1212
public class ScreenCapture extends Media implements Serializable {
13-
private static final long serialVersionUID = -3047762572007885369L;
13+
private static final long serialVersionUID = -3047762572007885369L;
14+
15+
public static final String BASE64_ENCODED = "data:image/png;base64,";
1416

15-
private String base64;
17+
private String base64;
1618

17-
@Builder
18-
public ScreenCapture(String path, String title, String resolvedPath, String base64) {
19-
super(path, title, resolvedPath, new HashMap<String, Object>());
20-
this.base64 = base64;
21-
}
19+
@Builder
20+
public ScreenCapture(String path, String title, String resolvedPath, String base64) {
21+
super(MediaType.SCREENCAPTURE, path, title, resolvedPath, new HashMap<String, Object>());
22+
this.base64 = base64;
23+
}
2224
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,24 @@ public String getFullName() {
159159

160160
public void addMedia(Media m) {
161161
if (m != null
162-
&& (m.getPath() != null || m.getResolvedPath() != null || ((ScreenCapture) m).getBase64() != null))
162+
&& (m.getPath() != null || m.getResolvedPath() != null ||
163+
(m instanceof ScreenCapture && ((ScreenCapture) m).getBase64() != null) ||
164+
(m instanceof Video && ((Video) m).getBase64() != null)))
163165
media.add(m);
164166
end(status);
165167
}
166168

167169
public boolean hasScreenCapture() {
168170
return !media.isEmpty() && media.stream().anyMatch(x -> x instanceof ScreenCapture);
169171
}
172+
173+
public boolean hasVideo() {
174+
return !media.isEmpty() && media.stream().anyMatch(x -> x instanceof Video);
175+
}
176+
177+
public boolean hasMedia() {
178+
return !media.isEmpty() && media.stream().anyMatch(x -> x instanceof Media);
179+
}
170180

171181
public long timeTaken() {
172182
return endTime.getTime() - startTime.getTime();

0 commit comments

Comments
 (0)