Skip to content

Commit

Permalink
Add video display in tests (#400)
Browse files Browse the repository at this point in the history
* 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 <grass.hopper.moc@gmail.com>
Co-authored-by: Anshoo Arora <anshooarora@gmail.com>
  • Loading branch information
3 people authored Jun 19, 2024
1 parent 41ae106 commit 182c433
Show file tree
Hide file tree
Showing 26 changed files with 749 additions and 74 deletions.
35 changes: 34 additions & 1 deletion src/main/java/com/aventstack/extentreports/ExtentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.aventstack.extentreports.model.RunResult;
import com.aventstack.extentreports.model.ScreenCapture;
import com.aventstack.extentreports.model.Test;
import com.aventstack.extentreports.model.Video;
import com.aventstack.extentreports.model.service.ExceptionInfoService;
import com.aventstack.extentreports.util.Assert;

Expand Down Expand Up @@ -1110,7 +1111,7 @@ public ExtentTest addScreenCaptureFromPath(String path) {
public ExtentTest addScreenCaptureFromBase64String(String base64, String title) {
Assert.notEmpty(base64, "ScreenCapture's base64 string must not be null or empty");
if (!base64.startsWith("data:"))
base64 = "data:image/png;base64," + base64;
base64 = ScreenCapture.BASE64_ENCODED + base64;
Media m = ScreenCapture.builder().base64(base64).title(title).build();
model.addMedia(m);
extent.onMediaAdded(m, model);
Expand All @@ -1120,4 +1121,36 @@ public ExtentTest addScreenCaptureFromBase64String(String base64, String title)
public ExtentTest addScreenCaptureFromBase64String(String base64) {
return addScreenCaptureFromBase64String(base64, null);
}

public ExtentTest addVideoFromPath(String path, String title) {
Assert.notEmpty(path, "Video path must not be null or empty");
Media m = Video.builder().path(path).title(title).build();
model.addMedia(m);
extent.onMediaAdded(m, model);
return this;
}

public ExtentTest addVideoFromPath(String path) {
return addVideoFromPath(path, null);
}

public ExtentTest addVideoFromBase64String(String base64, String title) {
Assert.notEmpty(base64, "Video's base64 string must not be null or empty");
if (!base64.startsWith("data:"))
base64 = Video.BASE64_ENCODED + base64;
Media m = Video.builder().base64(base64).title(title).build();
model.addMedia(m);
extent.onMediaAdded(m, model);
return this;
}

public ExtentTest addVideoFromBase64String(String base64) {
return addVideoFromBase64String(base64, null);
}

public ExtentTest addMedia(Media m) {
model.addMedia(m);
extent.onMediaAdded(m, model);
return this;
}
}
27 changes: 25 additions & 2 deletions src/main/java/com/aventstack/extentreports/MediaEntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.aventstack.extentreports.model.Media;
import com.aventstack.extentreports.model.ScreenCapture;
import com.aventstack.extentreports.model.Video;
import com.aventstack.extentreports.util.Assert;

/**
* Media builder for base64 and binary screenshots
*
*/
public class MediaEntityBuilder {
private static final String BASE64_ENCODED = "data:image/png;base64,";

private static ThreadLocal<Media> media = new ThreadLocal<>();

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

public static MediaEntityBuilder createScreenCaptureFromBase64String(String base64) {
return createScreenCaptureFromBase64String(base64, null);
}

public static MediaEntityBuilder createVideoFromPath(String path, String title) {
Assert.notEmpty(path, "Video path must not be null or empty");
media.set(Video.builder().path(path).title(title).build());
return getInstance();
}

public static MediaEntityBuilder createVideoFromPath(String path) {
return createVideoFromPath(path, null);
}

public static MediaEntityBuilder createVideoFromBase64String(String base64, String title) {
Assert.notEmpty(base64, "Video's base64 string must not be null or empty");
if (!base64.startsWith("data:"))
base64 = Video.BASE64_ENCODED + base64;
media.set(Video.builder().base64(base64).title(title).build());
return getInstance();
}

public static MediaEntityBuilder createVideoFromBase64String(String base64) {
return createVideoFromBase64String(base64, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@
import com.google.gson.reflect.TypeToken;

public class JsonDeserializer {
private File f;
private final File f;

public JsonDeserializer(final File f) {
this.f = f;
}

public List<Test> deserialize() throws IOException {
Gson gson = GsonExtentTypeAdapterBuilder.builder()
.withMediaTypeAdapter()
.withGsonTypeAdapterFactory()
.withScreenCaptureTypeAdapter()
.build();
String json = new String(Files.readAllBytes(f.toPath()));
Type t = new TypeToken<ArrayList<Test>>(){}.getType();
List<Test> tests = gson.fromJson(json, t);
return tests;
return gson.fromJson(json, t);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.aventstack.extentreports.append;

import java.io.IOException;

import com.aventstack.extentreports.model.Media;
import com.aventstack.extentreports.model.service.MediaService;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

public class MediaTypeAdapter extends TypeAdapter<Media> {

@Override
public void write(JsonWriter out, Media value) throws IOException {
}

@Override
public Media read(JsonReader reader) throws IOException {
reader.beginObject();
String fieldName = null;
int cycle = 0;

String type = null;
String path = null;
String resolvedPath = null;
String title = null;
String base64 = null;

while (reader.hasNext()) {
JsonToken token = reader.peek();
if (token.equals(JsonToken.NAME)) {
fieldName = reader.nextName();
}
if ("string".equalsIgnoreCase(token.name()) && fieldName.equalsIgnoreCase("type")) {
token = reader.peek();
type = reader.nextString();
}
if ("string".equalsIgnoreCase(token.name()) && fieldName.equalsIgnoreCase("path")) {
token = reader.peek();
path = reader.nextString();
}
if ("string".equalsIgnoreCase(token.name()) && fieldName.equalsIgnoreCase("resolvedPath")) {
token = reader.peek();
resolvedPath = reader.nextString();
}
if ("string".equalsIgnoreCase(token.name()) && fieldName.equalsIgnoreCase("title")) {
token = reader.peek();
title = reader.nextString();
}
if ("string".equalsIgnoreCase(token.name()) && fieldName.equalsIgnoreCase("base64")) {
token = reader.peek();
base64 = reader.nextString();
}
if (cycle++ > 10)
return MediaService.createMedia(type, path, resolvedPath, title, base64);
}
reader.endObject();
return MediaService.createMedia(type, path, resolvedPath, title, base64);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,9 @@ public void createDomain(final Test test, final ExtentTest extentTest) throws Cl

private void constructLog(final Log log, final ExtentTest extentTest, final ExceptionInfo ex) {
final Media m = log.getMedia();

if (m != null) {
if (m.getPath() != null) {
extentTest.log(log.getStatus(),
MediaEntityBuilder.createScreenCaptureFromPath(m.getPath()).build());
}
if (((ScreenCapture) m).getBase64() != null) {
extentTest.log(log.getStatus(),
MediaEntityBuilder.createScreenCaptureFromBase64String(((ScreenCapture) m).getBase64()).build());
}
}
extentTest.log(log.getStatus(), log.getDetails(), m);

if (ex != null) {
if (!extentTest.getModel().hasLog()) {
extentTest.log(log.getStatus(), log.getDetails());
}

final List<Log> logs = extentTest.getModel().getLogs();
final Log lastLog = logs.get(logs.size() - 1);
lastLog.setException(ex);
Expand All @@ -109,11 +95,7 @@ private void constructLog(final Log log, final ExtentTest extentTest, final Exce
private void constructTestMedia(final Test test, final ExtentTest extentTest) {
if (test.getMedia() != null) {
for (Media m : test.getMedia()) {
if (m.getPath() != null) {
extentTest.addScreenCaptureFromPath(m.getPath());
} else if (m instanceof ScreenCapture) {
extentTest.addScreenCaptureFromBase64String(((ScreenCapture) m).getBase64());
}
extentTest.addMedia(m);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.aventstack.extentreports.gson;

import com.aventstack.extentreports.append.ScreenCaptureTypeAdapter;
import com.aventstack.extentreports.append.MediaTypeAdapter;
import com.aventstack.extentreports.model.Media;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand All @@ -20,8 +20,9 @@ public Builder withGsonTypeAdapterFactory() {
return this;
}

public Builder withScreenCaptureTypeAdapter() {
builder.registerTypeAdapter(Media.class, new ScreenCaptureTypeAdapter());
public Builder withMediaTypeAdapter() {
//builder.registerTypeAdapter(Media.class, new ScreenCaptureTypeAdapter());
builder.registerTypeAdapter(Media.class, new MediaTypeAdapter());
return this;
}

Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/aventstack/extentreports/model/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,20 @@ public final boolean hasException() {

public final void addMedia(Media media) {
if (media != null && ((media.getPath() != null || media.getResolvedPath() != null)
|| media instanceof ScreenCapture && ((ScreenCapture) media).getBase64() != null))
|| (media instanceof ScreenCapture && ((ScreenCapture) media).getBase64() != null)
|| (media instanceof Video && ((Video) media).getBase64() != null)))
this.media = media;
}

public final boolean hasMedia() {
return media != null;
}

public final boolean hasScreenCapture() {
return media != null && media instanceof ScreenCapture;
}

public final boolean hasVideo() {
return media != null && media instanceof Video;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/aventstack/extentreports/model/Media.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
public class Media implements Serializable, BaseEntity, MetaDataStorable {
private static final long serialVersionUID = 5428859443090457608L;

private MediaType type;
private String path;
private String title;
private String resolvedPath;
private transient Map<String, Object> infoMap;

public enum MediaType {
SCREENCAPTURE, VIDEO
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
@Getter
@Setter
public class ScreenCapture extends Media implements Serializable {
private static final long serialVersionUID = -3047762572007885369L;
private static final long serialVersionUID = -3047762572007885369L;

public static final String BASE64_ENCODED = "data:image/png;base64,";

private String base64;
private String base64;

@Builder
public ScreenCapture(String path, String title, String resolvedPath, String base64) {
super(path, title, resolvedPath, new HashMap<String, Object>());
this.base64 = base64;
}
@Builder
public ScreenCapture(String path, String title, String resolvedPath, String base64) {
super(MediaType.SCREENCAPTURE, path, title, resolvedPath, new HashMap<String, Object>());
this.base64 = base64;
}
}
12 changes: 11 additions & 1 deletion src/main/java/com/aventstack/extentreports/model/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,24 @@ public String getFullName() {

public void addMedia(Media m) {
if (m != null
&& (m.getPath() != null || m.getResolvedPath() != null || ((ScreenCapture) m).getBase64() != null))
&& (m.getPath() != null || m.getResolvedPath() != null ||
(m instanceof ScreenCapture && ((ScreenCapture) m).getBase64() != null) ||
(m instanceof Video && ((Video) m).getBase64() != null)))
media.add(m);
end(status);
}

public boolean hasScreenCapture() {
return !media.isEmpty() && media.stream().anyMatch(x -> x instanceof ScreenCapture);
}

public boolean hasVideo() {
return !media.isEmpty() && media.stream().anyMatch(x -> x instanceof Video);
}

public boolean hasMedia() {
return !media.isEmpty() && media.stream().anyMatch(x -> x instanceof Media);
}

public long timeTaken() {
return endTime.getTime() - startTime.getTime();
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/aventstack/extentreports/model/Video.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.aventstack.extentreports.model;

import java.io.Serializable;
import java.util.HashMap;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Video extends Media implements Serializable {
private static final long serialVersionUID = -6874043323709622353L;

public static final String BASE64_ENCODED = "data:video/mp4;base64,";

private String base64;

@Builder
public Video(String path, String title, String resolvedPath, String base64) {
super(MediaType.VIDEO, path, title, resolvedPath, new HashMap<String, Object>());
this.base64 = base64;
}
}
Loading

0 comments on commit 182c433

Please sign in to comment.