diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index bd90d8f..a508169 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -29,7 +29,3 @@ jobs: cache: maven - name: Build with Maven run: mvn -B package --file pom.xml - - # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive - - name: Update dependency graph - uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6 diff --git a/src/main/java/com/aventstack/extentreports/ExtentTest.java b/src/main/java/com/aventstack/extentreports/ExtentTest.java index 61a225e..2fb6485 100644 --- a/src/main/java/com/aventstack/extentreports/ExtentTest.java +++ b/src/main/java/com/aventstack/extentreports/ExtentTest.java @@ -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; @@ -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); @@ -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; + } } \ No newline at end of file diff --git a/src/main/java/com/aventstack/extentreports/MediaEntityBuilder.java b/src/main/java/com/aventstack/extentreports/MediaEntityBuilder.java index 9676170..ed45e88 100644 --- a/src/main/java/com/aventstack/extentreports/MediaEntityBuilder.java +++ b/src/main/java/com/aventstack/extentreports/MediaEntityBuilder.java @@ -2,6 +2,7 @@ import com.aventstack.extentreports.model.Media; import com.aventstack.extentreports.model.ScreenCapture; +import com.aventstack.extentreports.model.Video; import com.aventstack.extentreports.util.Assert; /** @@ -9,7 +10,7 @@ * */ public class MediaEntityBuilder { - private static final String BASE64_ENCODED = "data:image/png;base64,"; + private static ThreadLocal media = new ThreadLocal<>(); private static class MediaBuilderInstance { @@ -43,7 +44,7 @@ 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(); } @@ -51,4 +52,26 @@ public static MediaEntityBuilder createScreenCaptureFromBase64String(String base 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); + } } \ No newline at end of file diff --git a/src/main/java/com/aventstack/extentreports/append/JsonDeserializer.java b/src/main/java/com/aventstack/extentreports/append/JsonDeserializer.java index 76598bb..dfe8d1e 100644 --- a/src/main/java/com/aventstack/extentreports/append/JsonDeserializer.java +++ b/src/main/java/com/aventstack/extentreports/append/JsonDeserializer.java @@ -13,7 +13,7 @@ import com.google.gson.reflect.TypeToken; public class JsonDeserializer { - private File f; + private final File f; public JsonDeserializer(final File f) { this.f = f; @@ -21,12 +21,11 @@ public JsonDeserializer(final File f) { public List deserialize() throws IOException { Gson gson = GsonExtentTypeAdapterBuilder.builder() + .withMediaTypeAdapter() .withGsonTypeAdapterFactory() - .withScreenCaptureTypeAdapter() .build(); String json = new String(Files.readAllBytes(f.toPath())); Type t = new TypeToken>(){}.getType(); - List tests = gson.fromJson(json, t); - return tests; + return gson.fromJson(json, t); } } \ No newline at end of file diff --git a/src/main/java/com/aventstack/extentreports/append/MediaTypeAdapter.java b/src/main/java/com/aventstack/extentreports/append/MediaTypeAdapter.java new file mode 100644 index 0000000..8cad5bf --- /dev/null +++ b/src/main/java/com/aventstack/extentreports/append/MediaTypeAdapter.java @@ -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 { + + @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); + } +} diff --git a/src/main/java/com/aventstack/extentreports/append/RawEntityConverter.java b/src/main/java/com/aventstack/extentreports/append/RawEntityConverter.java index efc83d6..d28ac44 100644 --- a/src/main/java/com/aventstack/extentreports/append/RawEntityConverter.java +++ b/src/main/java/com/aventstack/extentreports/append/RawEntityConverter.java @@ -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 logs = extentTest.getModel().getLogs(); final Log lastLog = logs.get(logs.size() - 1); lastLog.setException(ex); @@ -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); } } } diff --git a/src/main/java/com/aventstack/extentreports/gson/GsonExtentTypeAdapterBuilder.java b/src/main/java/com/aventstack/extentreports/gson/GsonExtentTypeAdapterBuilder.java index ec74290..c80181a 100644 --- a/src/main/java/com/aventstack/extentreports/gson/GsonExtentTypeAdapterBuilder.java +++ b/src/main/java/com/aventstack/extentreports/gson/GsonExtentTypeAdapterBuilder.java @@ -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; @@ -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; } diff --git a/src/main/java/com/aventstack/extentreports/model/Log.java b/src/main/java/com/aventstack/extentreports/model/Log.java index 04e80d0..01fe623 100644 --- a/src/main/java/com/aventstack/extentreports/model/Log.java +++ b/src/main/java/com/aventstack/extentreports/model/Log.java @@ -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; + } } diff --git a/src/main/java/com/aventstack/extentreports/model/Media.java b/src/main/java/com/aventstack/extentreports/model/Media.java index 1bdfeb8..9ef6d15 100644 --- a/src/main/java/com/aventstack/extentreports/model/Media.java +++ b/src/main/java/com/aventstack/extentreports/model/Media.java @@ -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 infoMap; + + public enum MediaType { + SCREENCAPTURE, VIDEO + } } diff --git a/src/main/java/com/aventstack/extentreports/model/ScreenCapture.java b/src/main/java/com/aventstack/extentreports/model/ScreenCapture.java index 5f441f0..f5d495f 100644 --- a/src/main/java/com/aventstack/extentreports/model/ScreenCapture.java +++ b/src/main/java/com/aventstack/extentreports/model/ScreenCapture.java @@ -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()); - this.base64 = base64; - } + @Builder + public ScreenCapture(String path, String title, String resolvedPath, String base64) { + super(MediaType.SCREENCAPTURE, path, title, resolvedPath, new HashMap()); + this.base64 = base64; + } } diff --git a/src/main/java/com/aventstack/extentreports/model/Test.java b/src/main/java/com/aventstack/extentreports/model/Test.java index c0329f4..39d93b7 100644 --- a/src/main/java/com/aventstack/extentreports/model/Test.java +++ b/src/main/java/com/aventstack/extentreports/model/Test.java @@ -159,7 +159,9 @@ 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); } @@ -167,6 +169,14 @@ public void addMedia(Media m) { 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(); diff --git a/src/main/java/com/aventstack/extentreports/model/Video.java b/src/main/java/com/aventstack/extentreports/model/Video.java new file mode 100644 index 0000000..8645a98 --- /dev/null +++ b/src/main/java/com/aventstack/extentreports/model/Video.java @@ -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()); + this.base64 = base64; + } +} diff --git a/src/main/java/com/aventstack/extentreports/model/service/MediaService.java b/src/main/java/com/aventstack/extentreports/model/service/MediaService.java index 155a8b3..dd93853 100644 --- a/src/main/java/com/aventstack/extentreports/model/service/MediaService.java +++ b/src/main/java/com/aventstack/extentreports/model/service/MediaService.java @@ -5,11 +5,14 @@ import java.nio.file.Paths; import com.aventstack.extentreports.model.Media; +import com.aventstack.extentreports.model.Media.MediaType; import com.aventstack.extentreports.model.ScreenCapture; +import com.aventstack.extentreports.model.Video; public class MediaService { public static void tryResolveMediaPath(Media media, String[] knownPath) { - if (knownPath == null || (media instanceof ScreenCapture && ((ScreenCapture) media).getBase64() != null)) + if (knownPath == null || (media instanceof ScreenCapture && ((ScreenCapture) media).getBase64() != null) + || (media instanceof Video && ((Video) media).getBase64() != null)) return; String loc = media.getPath(); File f = new File(loc); @@ -38,4 +41,41 @@ public static String getBase64(Media m) { return ((ScreenCapture) m).getBase64(); return null; } + + public static boolean isVideoBase64(Media m) { + return m instanceof Video && ((Video) m).getBase64() != null; + } + + public static String getVideoBase64(Media m) { + if (isVideoBase64(m)) + return ((Video) m).getBase64(); + return null; + } + + public static Media createMedia(String type, String path, String resolvedPath, String title, String base64) { + if (MediaType.valueOf(type) == MediaType.SCREENCAPTURE) { + ScreenCapture sc = ScreenCapture.builder().build(); + if (base64 != null) + sc.setBase64(base64); + updatePaths(sc, path, resolvedPath, title); + return sc; + } + if (MediaType.valueOf(type) == MediaType.VIDEO) { + Video vid = Video.builder().build(); + if (base64 != null) + vid.setBase64(base64); + updatePaths(vid, path, resolvedPath, title); + return vid; + } + return null; + } + + private static void updatePaths(Media media, String path, String resolvedPath, String title) { + if (path != null) + media.setPath(path); + if (resolvedPath != null) + media.setResolvedPath(resolvedPath); + if (title != null) + media.setTitle(title); + } } diff --git a/src/main/java/com/aventstack/extentreports/model/service/TestService.java b/src/main/java/com/aventstack/extentreports/model/service/TestService.java index d0fd61f..1b994e4 100644 --- a/src/main/java/com/aventstack/extentreports/model/service/TestService.java +++ b/src/main/java/com/aventstack/extentreports/model/service/TestService.java @@ -5,20 +5,57 @@ import com.aventstack.extentreports.gherkin.model.IGherkinFormatterModel; import com.aventstack.extentreports.model.Log; +import com.aventstack.extentreports.model.Media; +import com.aventstack.extentreports.model.ScreenCapture; import com.aventstack.extentreports.model.Test; +import com.aventstack.extentreports.model.Video; import com.aventstack.extentreports.util.Assert; public class TestService { public static Boolean testHasScreenCapture(Test test, Boolean deep) { if (deep) { - Boolean hasScreenCapture = !test.getMedia().isEmpty() - || test.getLogs().stream().anyMatch(Log::hasMedia); + Boolean hasScreenCapture = (!test.getMedia().isEmpty() && test.hasScreenCapture()) + || test.getLogs().stream().anyMatch(Log::hasScreenCapture); if (!hasScreenCapture) hasScreenCapture = test.getChildren().stream().anyMatch(x -> testHasScreenCapture(x, deep)); return hasScreenCapture; } return test.hasScreenCapture(); } + + public static Boolean testHasVideo(Test test, Boolean deep) { + if (deep) { + Boolean hasVideo = (!test.getMedia().isEmpty() && test.hasVideo()) + || test.getLogs().stream().anyMatch(Log::hasVideo); + if (!hasVideo) + hasVideo = test.getChildren().stream().anyMatch(x -> testHasVideo(x, deep)); + return hasVideo; + } + return test.hasVideo(); + } + + public static Boolean testHasMedia(Test test, Boolean deep) { + return testHasScreenCapture(test, deep) || testHasVideo(test, deep); + } + + + public static boolean isScreenCapture(Media m) { + return m instanceof ScreenCapture; + } + + public static boolean isVideo(Media m) { + return m instanceof Video; + } + + public static String videoSrc(Media m) { + if (MediaService.isVideoBase64(m)) + return MediaService.getVideoBase64(m); + else if (m.getResolvedPath() != null && !m.getResolvedPath().isEmpty()) + return m.getResolvedPath(); + else if (m.getPath() != null && !m.getPath().isEmpty()) + return m.getPath(); + return ""; + } public static Test createTest(Class type, String name, String description) { Assert.notEmpty(name, "Test name must not be null or empty"); diff --git a/src/main/java/com/aventstack/extentreports/reporter/ExtentSparkReporter.java b/src/main/java/com/aventstack/extentreports/reporter/ExtentSparkReporter.java index 8f60dba..62a9b24 100644 --- a/src/main/java/com/aventstack/extentreports/reporter/ExtentSparkReporter.java +++ b/src/main/java/com/aventstack/extentreports/reporter/ExtentSparkReporter.java @@ -147,7 +147,6 @@ private void flush(ReportEntity value) { final String filePath = getFileNameAsExt(FILE_NAME, new String[]{".html", ".htm"}); final Template template = getFreemarkerConfig().getTemplate(SPA_TEMPLATE_NAME); processTemplate(template, new File(filePath)); - return; } catch (IOException | TemplateException e) { disposable.dispose(); logger.log(Level.SEVERE, "An exception occurred", e); diff --git a/src/main/java/com/aventstack/extentreports/templating/FreemarkerTemplate.java b/src/main/java/com/aventstack/extentreports/templating/FreemarkerTemplate.java index 218fb83..1e49e9e 100644 --- a/src/main/java/com/aventstack/extentreports/templating/FreemarkerTemplate.java +++ b/src/main/java/com/aventstack/extentreports/templating/FreemarkerTemplate.java @@ -36,8 +36,7 @@ public Template createTemplate(String templatePath) } public String getSource(Template template, Map templateMap) throws TemplateException, IOException { - String source = processTemplate(template, templateMap); - return source; + return processTemplate(template, templateMap); } public void writeTemplate(Template template, Map templateMap, File outputFile) diff --git a/src/main/resources/com/aventstack/extentreports/templates/commons/commons-macros.ftl b/src/main/resources/com/aventstack/extentreports/templates/commons/commons-macros.ftl index a7c89de..06ec6ce 100644 --- a/src/main/resources/com/aventstack/extentreports/templates/commons/commons-macros.ftl +++ b/src/main/resources/com/aventstack/extentreports/templates/commons/commons-macros.ftl @@ -29,18 +29,25 @@ <#macro mediaSingle m> - <#if m??>
- <#if m.base64??> - <#if config.thumbnailForBase64()?? && config.thumbnailForBase64()> - - <#else> - base64 img - - <#elseif m.resolvedPath??> - <#elseif m.path??> + + <#if TestService.isVideo(m)> +
- diff --git a/src/main/resources/com/aventstack/extentreports/templates/spark/macros/recurse_nodes.ftl b/src/main/resources/com/aventstack/extentreports/templates/spark/macros/recurse_nodes.ftl index 174f40b..0e55d63 100644 --- a/src/main/resources/com/aventstack/extentreports/templates/spark/macros/recurse_nodes.ftl +++ b/src/main/resources/com/aventstack/extentreports/templates/spark/macros/recurse_nodes.ftl @@ -9,7 +9,7 @@
  • ${node.status}
  • ${node.timeTaken()?number_to_time?string("mm:ss:SSS")}
  • -
  • <#if TestService.testHasScreenCapture(node, true)>
  • +
  • <#if TestService.testHasMedia(node, true)>
diff --git a/src/test/java/com/aventstack/extentreports/AppenderTest.java b/src/test/java/com/aventstack/extentreports/AppenderTest.java index 55a4989..c4c8d2b 100644 --- a/src/test/java/com/aventstack/extentreports/AppenderTest.java +++ b/src/test/java/com/aventstack/extentreports/AppenderTest.java @@ -7,6 +7,7 @@ import org.testng.annotations.Test; import com.aventstack.extentreports.model.ScreenCapture; +import com.aventstack.extentreports.model.Video; import com.aventstack.extentreports.reporter.JsonFormatter; public class AppenderTest { @@ -132,7 +133,9 @@ public void testWithMedia() throws IOException { // parent checks Assert.assertEquals(list.size(), 1); Assert.assertEquals(list.get(0).getMedia().size(), 1); + Assert.assertTrue(list.get(0).getMedia().get(0) instanceof ScreenCapture); Assert.assertNotNull(list.get(0).getLogs().get(0).getMedia()); + Assert.assertTrue(list.get(0).getLogs().get(0).getMedia() instanceof ScreenCapture); Assert.assertEquals(list.get(0).getMedia().get(0).getPath(), test1.getModel().getMedia().get(0).getPath()); Assert.assertEquals(list.get(0).getLogs().get(0).getMedia().getPath(), test1.getModel().getLogs().get(0).getMedia().getPath()); @@ -163,4 +166,59 @@ public void testWithMediaBase64() throws IOException { Assert.assertEquals(((ScreenCapture) list.get(0).getLogs().get(0).getMedia()).getBase64(), ((ScreenCapture) test1.getModel().getLogs().get(0).getMedia()).getBase64()); } + + @Test + public void testWithVideoMedia() throws IOException { + // initial, create archive: + ExtentReports extent = new ExtentReports(); + JsonFormatter json = new JsonFormatter(JSON_ARCHIVE); + extent.attachReporter(json); + ExtentTest test1 = extent.createTest("Testname1") + .addVideoFromPath("vid.mp4") + .fail("Fail", MediaEntityBuilder.createVideoFromPath("vid.mp4").build()); + extent.flush(); + + // post, check archive + extent = new ExtentReports(); + extent.createDomainFromJsonArchive(JSON_ARCHIVE); + List list = extent.getReport().getTestList(); + + // parent checks + Assert.assertEquals(list.size(), 1); + Assert.assertEquals(list.get(0).getMedia().size(), 1); + Assert.assertTrue(list.get(0).getMedia().get(0) instanceof Video); + Assert.assertNotNull(list.get(0).getLogs().get(0).getMedia()); + Assert.assertTrue(list.get(0).getLogs().get(0).getMedia() instanceof Video); + Assert.assertEquals(list.get(0).getMedia().get(0).getPath(), test1.getModel().getMedia().get(0).getPath()); + Assert.assertEquals(list.get(0).getLogs().get(0).getMedia().getPath(), + test1.getModel().getLogs().get(0).getMedia().getPath()); + } + + @Test + public void testWithVideoBase64() throws IOException { + // initial, create archive: + ExtentReports extent = new ExtentReports(); + JsonFormatter json = new JsonFormatter(JSON_ARCHIVE); + extent.attachReporter(json); + ExtentTest test1 = extent.createTest("Testname1") + .addVideoFromBase64String("base64string") + .fail("Fail", MediaEntityBuilder.createVideoFromBase64String("base64string").build()); + extent.flush(); + + // post, check archive + extent = new ExtentReports(); + extent.createDomainFromJsonArchive(JSON_ARCHIVE); + List list = extent.getReport().getTestList(); + + // parent checks + Assert.assertEquals(list.size(), 1); + Assert.assertEquals(list.get(0).getMedia().size(), 1); + Assert.assertTrue(list.get(0).getMedia().get(0) instanceof Video); + Assert.assertNotNull(list.get(0).getLogs().get(0).getMedia()); + Assert.assertTrue(list.get(0).getLogs().get(0).getMedia() instanceof Video); + Assert.assertEquals(((Video) list.get(0).getMedia().get(0)).getBase64(), + ((Video) test1.getModel().getMedia().get(0)).getBase64()); + Assert.assertEquals(((Video) list.get(0).getLogs().get(0).getMedia()).getBase64(), + ((Video) test1.getModel().getLogs().get(0).getMedia()).getBase64()); + } } diff --git a/src/test/java/com/aventstack/extentreports/ExtentReportsTest.java b/src/test/java/com/aventstack/extentreports/ExtentReportsTest.java index e8cfbdc..42e231f 100644 --- a/src/test/java/com/aventstack/extentreports/ExtentReportsTest.java +++ b/src/test/java/com/aventstack/extentreports/ExtentReportsTest.java @@ -8,17 +8,22 @@ import com.aventstack.extentreports.gherkin.GherkinDialectManager; import com.aventstack.extentreports.gherkin.model.Feature; import com.aventstack.extentreports.model.Test; +import org.testng.annotations.BeforeMethod; public class ExtentReportsTest { + private static final String TEST_NAME = "TEST"; + private ExtentReports extent; - private ExtentReports extent() { - return new ExtentReports(); + @BeforeMethod + public void beforeMethod() throws UnsupportedEncodingException { + extent = new ExtentReports(); + extent.setGherkinDialect("en"); } @org.testng.annotations.Test public void createTestOverloadTypeNameDesc() { - ExtentTest test = extent().createTest(Feature.class, TEST_NAME, "Description"); + ExtentTest test = extent.createTest(Feature.class, TEST_NAME, "Description"); Test model = test.getModel(); Assert.assertTrue(model.isBDD()); Assert.assertEquals(model.getName(), TEST_NAME); @@ -29,7 +34,7 @@ public void createTestOverloadTypeNameDesc() { @org.testng.annotations.Test public void createTestOverloadTypeName() { - ExtentTest test = extent().createTest(Feature.class, TEST_NAME); + ExtentTest test = extent.createTest(Feature.class, TEST_NAME); Test model = test.getModel(); Assert.assertTrue(model.isBDD()); Assert.assertEquals(model.getName(), TEST_NAME); @@ -40,7 +45,7 @@ public void createTestOverloadTypeName() { @org.testng.annotations.Test public void createTestOverloadKeywordNameDesc() throws ClassNotFoundException { - ExtentTest test = extent().createTest(new GherkinKeyword("Feature"), TEST_NAME, "Description"); + ExtentTest test = extent.createTest(new GherkinKeyword("Feature"), TEST_NAME, "Description"); Test model = test.getModel(); Assert.assertTrue(model.isBDD()); Assert.assertEquals(model.getName(), TEST_NAME); @@ -51,7 +56,7 @@ public void createTestOverloadKeywordNameDesc() throws ClassNotFoundException { @org.testng.annotations.Test public void createTestOverloadKeywordName() throws ClassNotFoundException { - ExtentTest test = extent().createTest(new GherkinKeyword("Feature"), TEST_NAME); + ExtentTest test = extent.createTest(new GherkinKeyword("Feature"), TEST_NAME); Test model = test.getModel(); Assert.assertTrue(model.isBDD()); Assert.assertEquals(model.getName(), TEST_NAME); @@ -62,7 +67,7 @@ public void createTestOverloadKeywordName() throws ClassNotFoundException { @org.testng.annotations.Test public void createTestOverloadNameDesc() { - ExtentTest test = extent().createTest(TEST_NAME, "Description"); + ExtentTest test = extent.createTest(TEST_NAME, "Description"); Test model = test.getModel(); Assert.assertFalse(model.isBDD()); Assert.assertEquals(model.getName(), TEST_NAME); @@ -73,7 +78,7 @@ public void createTestOverloadNameDesc() { @org.testng.annotations.Test public void createTestOverloadName() { - ExtentTest test = extent().createTest(TEST_NAME); + ExtentTest test = extent.createTest(TEST_NAME); Test model = test.getModel(); Assert.assertFalse(model.isBDD()); Assert.assertEquals(model.getName(), TEST_NAME); @@ -84,7 +89,6 @@ public void createTestOverloadName() { @org.testng.annotations.Test public void gherkinDialect() throws UnsupportedEncodingException { - ExtentReports extent = extent(); extent.setGherkinDialect("de"); Assert.assertEquals(GherkinDialectManager.getLanguage(), "de"); } @@ -92,7 +96,6 @@ public void gherkinDialect() throws UnsupportedEncodingException { @org.testng.annotations.Test public void addTestRunnerOutputSingle() { String[] logs = new String[]{"Log1", "Log2"}; - ExtentReports extent = extent(); Arrays.stream(logs).forEach(extent::addTestRunnerOutput); Assert.assertEquals(extent.getReport().getLogs().size(), 2); Arrays.stream(logs).forEach(x -> Assert.assertTrue(extent.getReport().getLogs().contains(x))); @@ -101,7 +104,6 @@ public void addTestRunnerOutputSingle() { @org.testng.annotations.Test public void addTestRunnerOutputList() { String[] logs = new String[]{"Log1", "Log2"}; - ExtentReports extent = extent(); extent.addTestRunnerOutput(Arrays.asList(logs)); Assert.assertEquals(extent.getReport().getLogs().size(), 2); Arrays.stream(logs).forEach(x -> Assert.assertTrue(extent.getReport().getLogs().contains(x))); diff --git a/src/test/java/com/aventstack/extentreports/ExtentTestLogTest.java b/src/test/java/com/aventstack/extentreports/ExtentTestLogTest.java index d08215d..82aedec 100644 --- a/src/test/java/com/aventstack/extentreports/ExtentTestLogTest.java +++ b/src/test/java/com/aventstack/extentreports/ExtentTestLogTest.java @@ -11,6 +11,7 @@ public class ExtentTestLogTest { private static final String DETAILS = "details"; private static final String ATTACHMENT = "img.png"; + private static final String VIDEO_ATTACHMENT = "vid.mp4"; private ExtentTest test() { return new ExtentReports().createTest("Test"); @@ -48,10 +49,14 @@ public void logDetailsMedia() throws IOException { Assert.assertEquals(test.getModel().getLogs().get(1).getDetails(), DETAILS); Assert.assertEquals(test.getModel().getLogs().get(1).getStatus(), Status.FAIL); Assert.assertEquals(test.getModel().getLogs().get(1).getMedia().getPath(), ATTACHMENT); + test.log(Status.FAIL, MediaEntityBuilder.createVideoFromPath(VIDEO_ATTACHMENT).build()); + Assert.assertEquals(test.getModel().getLogs().get(2).getDetails(), ""); + Assert.assertEquals(test.getModel().getLogs().get(2).getStatus(), Status.FAIL); + Assert.assertEquals(test.getModel().getLogs().get(2).getMedia().getPath(), VIDEO_ATTACHMENT); } @Test - public void logMedia() throws IOException { + public void logMedia() { ExtentTest test = test().log(Status.SKIP, MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); Assert.assertEquals(test.getModel().getStatus(), Status.SKIP); @@ -62,10 +67,14 @@ public void logMedia() throws IOException { Assert.assertEquals(test.getModel().getLogs().get(1).getDetails(), ""); Assert.assertEquals(test.getModel().getLogs().get(1).getStatus(), Status.FAIL); Assert.assertEquals(test.getModel().getLogs().get(1).getMedia().getPath(), ATTACHMENT); + test.log(Status.FAIL, MediaEntityBuilder.createVideoFromPath(VIDEO_ATTACHMENT).build()); + Assert.assertEquals(test.getModel().getLogs().get(2).getDetails(), ""); + Assert.assertEquals(test.getModel().getLogs().get(2).getStatus(), Status.FAIL); + Assert.assertEquals(test.getModel().getLogs().get(2).getMedia().getPath(), VIDEO_ATTACHMENT); } @Test - public void logMarkup() throws IOException { + public void logMarkup() { Markup m = MarkupHelper.createCodeBlock("code"); ExtentTest test = test().log(Status.SKIP, m); Assert.assertEquals(test.getModel().getStatus(), Status.SKIP); @@ -86,7 +95,7 @@ public void logThrowable() { } @Test - public void logThrowableMedia() throws IOException { + public void logThrowableMedia() { Exception ex = ex(); ExtentTest test = test().log(Status.SKIP, ex, MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); @@ -98,6 +107,10 @@ public void logThrowableMedia() throws IOException { Assert.assertEquals(test.getModel().getLogs().get(1).getException().getException(), ex); Assert.assertEquals(test.getModel().getLogs().get(1).getStatus(), Status.FAIL); Assert.assertEquals(test.getModel().getLogs().get(1).getMedia().getPath(), ATTACHMENT); + test.log(Status.FAIL, MediaEntityBuilder.createVideoFromPath(VIDEO_ATTACHMENT).build()); + Assert.assertEquals(test.getModel().getLogs().get(2).getDetails(), ""); + Assert.assertEquals(test.getModel().getLogs().get(2).getStatus(), Status.FAIL); + Assert.assertEquals(test.getModel().getLogs().get(2).getMedia().getPath(), VIDEO_ATTACHMENT); } @Test @@ -108,7 +121,7 @@ public void failDetails() { } @Test - public void failMedia() throws IOException { + public void failMedia() { ExtentTest test = test().fail( MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); test.log(Status.FAIL, DETAILS, MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); @@ -125,7 +138,7 @@ public void failThrowable() { } @Test - public void failThrowableMedia() throws IOException { + public void failThrowableMedia() { Exception ex = ex(); ExtentTest test = test().fail(ex, MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); @@ -135,7 +148,7 @@ public void failThrowableMedia() throws IOException { } @Test - public void failMarkup() throws IOException { + public void failMarkup() { Markup m = MarkupHelper.createCodeBlock("code"); ExtentTest test = test().fail(m); Assert.assertEquals(test.getModel().getStatus(), Status.FAIL); @@ -151,7 +164,7 @@ public void skipDetails() { } @Test - public void skipMedia() throws IOException { + public void skipMedia() { ExtentTest test = test().skip( MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); test.log(Status.FAIL, DETAILS, MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); @@ -168,7 +181,7 @@ public void skipThrowable() { } @Test - public void skipThrowableMedia() throws IOException { + public void skipThrowableMedia() { Exception ex = ex(); ExtentTest test = test().skip(ex, MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); @@ -178,7 +191,7 @@ public void skipThrowableMedia() throws IOException { } @Test - public void skipMarkup() throws IOException { + public void skipMarkup() { Markup m = MarkupHelper.createCodeBlock("code"); ExtentTest test = test().log(Status.SKIP, m); Assert.assertEquals(test.getModel().getStatus(), Status.SKIP); @@ -194,7 +207,7 @@ public void warnDetails() { } @Test - public void warnMedia() throws IOException { + public void warnMedia() { ExtentTest test = test().warning( MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); test.log(Status.WARNING, DETAILS, MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); @@ -211,7 +224,7 @@ public void warnThrowable() { } @Test - public void warnThrowableMedia() throws IOException { + public void warnThrowableMedia() { Exception ex = ex(); ExtentTest test = test().log(Status.WARNING, ex, MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); @@ -221,7 +234,7 @@ public void warnThrowableMedia() throws IOException { } @Test - public void warnMarkup() throws IOException { + public void warnMarkup() { Markup m = MarkupHelper.createCodeBlock("code"); ExtentTest test = test().log(Status.WARNING, m); Assert.assertEquals(test.getModel().getStatus(), Status.WARNING); @@ -237,7 +250,7 @@ public void passDetails() { } @Test - public void passMedia() throws IOException { + public void passMedia() { ExtentTest test = test().pass( MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); test.log(Status.PASS, DETAILS, MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); @@ -254,7 +267,7 @@ public void passThrowable() { } @Test - public void passThrowableMedia() throws IOException { + public void passThrowableMedia() { Exception ex = ex(); ExtentTest test = test().pass(ex, MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); @@ -264,7 +277,7 @@ public void passThrowableMedia() throws IOException { } @Test - public void passMarkup() throws IOException { + public void passMarkup() { Markup m = MarkupHelper.createCodeBlock("code"); ExtentTest test = test().log(Status.PASS, m); Assert.assertEquals(test.getModel().getStatus(), Status.PASS); @@ -280,7 +293,7 @@ public void infoDetails() { } @Test - public void infoMedia() throws IOException { + public void infoMedia() { ExtentTest test = test().info( MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); test.log(Status.INFO, DETAILS, MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); @@ -297,7 +310,7 @@ public void infoThrowable() { } @Test - public void infoThrowableMedia() throws IOException { + public void infoThrowableMedia() { Exception ex = ex(); ExtentTest test = test().info(ex, MediaEntityBuilder.createScreenCaptureFromPath(ATTACHMENT).build()); @@ -307,7 +320,7 @@ public void infoThrowableMedia() throws IOException { } @Test - public void infoMarkup() throws IOException { + public void infoMarkup() { Markup m = MarkupHelper.createCodeBlock("code"); ExtentTest test = test().log(Status.INFO, m); Assert.assertEquals(test.getModel().getStatus(), Status.PASS); diff --git a/src/test/java/com/aventstack/extentreports/ExtentTestMediaTest.java b/src/test/java/com/aventstack/extentreports/ExtentTestMediaTest.java index 3d15515..e3ea378 100644 --- a/src/test/java/com/aventstack/extentreports/ExtentTestMediaTest.java +++ b/src/test/java/com/aventstack/extentreports/ExtentTestMediaTest.java @@ -4,6 +4,7 @@ import org.testng.annotations.Test; import com.aventstack.extentreports.model.ScreenCapture; +import com.aventstack.extentreports.model.Video; public class ExtentTestMediaTest { private static final String BASE64_ENCODED = "data:image/png;base64,"; @@ -15,6 +16,12 @@ public class ExtentTestMediaTest { private static final String PATH = "src/test/resources/img.png"; private static final String TITLE = "MediaTitle"; + private static final String VIDEO_BASE64_ENCODED = "data:video/mp4;base64,"; + private static final String VIDEO_BASE64 = "video base64 string"; + + private static final String VIDEO_PATH = "src/test/resources/vid.png"; + private static final String VIDEO_TITLE = "VideoTitle"; + private ExtentReports extent() { return new ExtentReports(); } @@ -189,4 +196,151 @@ public void addScreenCaptureFromBase64NodeLogOverloads() { Assert.assertEquals(((ScreenCapture) node.getModel().getLogs().get(0).getMedia()).getBase64(), BASE64_ENCODED + BASE64); } + + + @Test(expectedExceptions = IllegalArgumentException.class) + public void addVideoFromEmptyPathTest() { + extent().createTest("Test") + .addVideoFromPath(""); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void addVideoFromNullPathTest() { + extent().createTest("Test") + .addVideoFromPath(null); + } + + @Test + public void addVideoFromPathTest() { + ExtentTest test = extent().createTest("Test") + .addVideoFromPath(VIDEO_PATH, VIDEO_TITLE) + .pass("Pass"); + Assert.assertEquals(test.getModel().getMedia().size(), 1); + Assert.assertEquals(test.getModel().getMedia().get(0).getPath(), VIDEO_PATH); + Assert.assertEquals(test.getModel().getMedia().get(0).getTitle(), VIDEO_TITLE); + } + + @Test + public void addVideoFromPathTestOverloads() { + ExtentTest test = extent().createTest("Test") + .addVideoFromPath(VIDEO_PATH) + .pass("Pass"); + Assert.assertEquals(test.getModel().getMedia().size(), 1); + Assert.assertEquals(test.getModel().getMedia().get(0).getPath(), VIDEO_PATH); + } + + @Test + public void addVideoFromPathTestLog() { + ExtentTest test = extent().createTest("Test") + .pass("Pass", MediaEntityBuilder.createVideoFromPath(VIDEO_PATH, VIDEO_TITLE).build()); + Assert.assertEquals(test.getModel().getMedia().size(), 0); + Assert.assertNotNull(test.getModel().getLogs().get(0).getMedia()); + Assert.assertEquals(test.getModel().getLogs().get(0).getMedia().getPath(), VIDEO_PATH); + Assert.assertEquals(test.getModel().getLogs().get(0).getMedia().getTitle(), VIDEO_TITLE); + } + + @Test + public void addVideoFromPathTestLogOverloads() { + ExtentTest test = extent().createTest("Test") + .pass("Pass", MediaEntityBuilder.createVideoFromPath(VIDEO_PATH).build()); + Assert.assertEquals(test.getModel().getMedia().size(), 0); + Assert.assertNotNull(test.getModel().getLogs().get(0).getMedia()); + Assert.assertEquals(test.getModel().getLogs().get(0).getMedia().getPath(), VIDEO_PATH); + } + + @Test + public void addVideoFromPathNodeLog() { + ExtentTest test = extent().createTest("Test"); + ExtentTest node = test + .createNode("Node") + .pass("Pass", MediaEntityBuilder.createVideoFromPath(VIDEO_PATH, VIDEO_TITLE).build()); + Assert.assertEquals(node.getModel().getMedia().size(), 0); + Assert.assertNotNull(node.getModel().getLogs().get(0).getMedia()); + Assert.assertEquals(node.getModel().getLogs().get(0).getMedia().getPath(), VIDEO_PATH); + Assert.assertEquals(node.getModel().getLogs().get(0).getMedia().getTitle(), VIDEO_TITLE); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void addVideoFromEmptyBase64Test() { + extent().createTest("Test") + .addVideoFromBase64String(""); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void addVideoFromNullBase64Test() { + extent().createTest("Test") + .addVideoFromBase64String(null); + } + + @Test + public void addVideoFromBase64Test() { + ExtentReports extent = new ExtentReports(); + ExtentTest test = extent.createTest("Test") + .addVideoFromBase64String(VIDEO_BASE64, TITLE) + .pass("Pass"); + Assert.assertEquals(test.getModel().getMedia().size(), 1); + Assert.assertEquals(((Video) test.getModel().getMedia().get(0)).getBase64(), VIDEO_BASE64_ENCODED + VIDEO_BASE64); + Assert.assertEquals(test.getModel().getMedia().get(0).getTitle(), TITLE); + } + + @Test + public void addVideoFromBase64Node() { + ExtentTest test = extent().createTest("Test"); + ExtentTest node = test + .createNode("Node") + .addVideoFromBase64String(VIDEO_BASE64, TITLE) + .pass("Pass"); + Assert.assertEquals(test.getModel().getMedia().size(), 0); + Assert.assertEquals(node.getModel().getMedia().size(), 1); + Assert.assertEquals(((Video) node.getModel().getMedia().get(0)).getBase64(), VIDEO_BASE64_ENCODED + VIDEO_BASE64); + Assert.assertEquals(node.getModel().getMedia().get(0).getTitle(), TITLE); + } + + @Test + public void addVideoFromBase64NodeOverloads() { + ExtentTest test = extent().createTest("Test"); + ExtentTest node = test + .createNode("Node") + .addVideoFromBase64String(VIDEO_BASE64) + .pass("Pass"); + Assert.assertEquals(test.getModel().getMedia().size(), 0); + Assert.assertEquals(node.getModel().getMedia().size(), 1); + Assert.assertEquals(((Video) node.getModel().getMedia().get(0)).getBase64(), VIDEO_BASE64_ENCODED + VIDEO_BASE64); + } + + @Test + public void addVideoFromBase64TestLog() { + ExtentTest test = extent().createTest("Test") + .pass("Pass", MediaEntityBuilder.createVideoFromBase64String(VIDEO_BASE64, TITLE).build()); + Assert.assertEquals(test.getModel().getMedia().size(), 0); + Assert.assertNotNull(test.getModel().getLogs().get(0).getMedia()); + Assert.assertEquals(((Video) test.getModel().getLogs().get(0).getMedia()).getBase64(), + VIDEO_BASE64_ENCODED + VIDEO_BASE64); + Assert.assertEquals(test.getModel().getLogs().get(0).getMedia().getTitle(), TITLE); + } + + @Test + public void addVideoFromBase64NodeLog() { + ExtentTest test = extent().createTest("Test"); + ExtentTest node = test + .createNode("Node") + .pass("Pass", MediaEntityBuilder.createVideoFromBase64String(VIDEO_BASE64, TITLE).build()); + Assert.assertEquals(node.getModel().getMedia().size(), 0); + Assert.assertNotNull(node.getModel().getLogs().get(0).getMedia()); + Assert.assertEquals(((Video) node.getModel().getLogs().get(0).getMedia()).getBase64(), + VIDEO_BASE64_ENCODED + VIDEO_BASE64); + Assert.assertEquals(node.getModel().getLogs().get(0).getMedia().getTitle(), TITLE); + } + + @Test + public void addVideoFromBase64NodeLogOverloads() { + ExtentTest test = extent().createTest("Test"); + ExtentTest node = test + .createNode("Node") + .pass("Pass", MediaEntityBuilder.createVideoFromBase64String(VIDEO_BASE64).build()); + Assert.assertEquals(node.getModel().getMedia().size(), 0); + Assert.assertNotNull(node.getModel().getLogs().get(0).getMedia()); + Assert.assertEquals(((Video) node.getModel().getLogs().get(0).getMedia()).getBase64(), + VIDEO_BASE64_ENCODED + VIDEO_BASE64); + } } diff --git a/src/test/java/com/aventstack/extentreports/entity/LogEntityTest.java b/src/test/java/com/aventstack/extentreports/entity/LogEntityTest.java index 5f62572..8a63a76 100644 --- a/src/test/java/com/aventstack/extentreports/entity/LogEntityTest.java +++ b/src/test/java/com/aventstack/extentreports/entity/LogEntityTest.java @@ -7,6 +7,7 @@ import com.aventstack.extentreports.model.Log; import com.aventstack.extentreports.model.Media; import com.aventstack.extentreports.model.ScreenCapture; +import com.aventstack.extentreports.model.Video; public class LogEntityTest { @@ -84,4 +85,18 @@ public void addMediaWithResolvedPathToLog() { Log log = Log.builder().media(m).build(); Assert.assertTrue(log.hasMedia()); } + + @Test + public void addVideoWithPathToLog() { + Media m = Video.builder().path("./vid.mp4").build(); + Log log = Log.builder().media(m).build(); + Assert.assertTrue(log.hasMedia()); + } + + @Test + public void addVideoWithResolvedPathToLog() { + Media m = Video.builder().resolvedPath("./vid.mp4").build(); + Log log = Log.builder().media(m).build(); + Assert.assertTrue(log.hasMedia()); + } } diff --git a/src/test/java/com/aventstack/extentreports/entity/TestEntityTest.java b/src/test/java/com/aventstack/extentreports/entity/TestEntityTest.java index 5e4140b..114c95e 100644 --- a/src/test/java/com/aventstack/extentreports/entity/TestEntityTest.java +++ b/src/test/java/com/aventstack/extentreports/entity/TestEntityTest.java @@ -12,6 +12,7 @@ import com.aventstack.extentreports.model.Log; import com.aventstack.extentreports.model.ScreenCapture; import com.aventstack.extentreports.model.Test; +import com.aventstack.extentreports.model.Video; import com.aventstack.extentreports.model.service.TestService; public class TestEntityTest { @@ -228,6 +229,16 @@ public void hasScreenCapture() { test.addMedia(ScreenCapture.builder().path("/img.png").build()); Assert.assertTrue(test.hasScreenCapture()); } + + @org.testng.annotations.Test + public void hasVideo() { + Test test = getTest(); + Assert.assertFalse(test.hasVideo()); + test.addMedia(Video.builder().build()); + Assert.assertFalse(test.hasVideo()); + test.addMedia(Video.builder().path("/vid.mp4").build()); + Assert.assertTrue(test.hasVideo()); + } @org.testng.annotations.Test public void computeTestStatusNoLog() { diff --git a/src/test/java/com/aventstack/extentreports/entity/service/MediaServiceTest.java b/src/test/java/com/aventstack/extentreports/entity/service/MediaServiceTest.java new file mode 100644 index 0000000..5678c4e --- /dev/null +++ b/src/test/java/com/aventstack/extentreports/entity/service/MediaServiceTest.java @@ -0,0 +1,83 @@ +package com.aventstack.extentreports.entity.service; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.testng.Assert; +import org.testng.annotations.Test; + +import com.aventstack.extentreports.model.Media; +import com.aventstack.extentreports.model.Media.MediaType; +import com.aventstack.extentreports.model.ScreenCapture; +import com.aventstack.extentreports.model.Video; +import com.aventstack.extentreports.model.service.MediaService; + +public class MediaServiceTest { + + @Test + public void testMediaWithExactFilePath() throws IOException { + Path tmpDir = Files.createTempDirectory(Paths.get("target"),"known"); + Path tmpFile = Files.copy(Paths.get("src/test/resources/img.png"),Paths.get(tmpDir.toString()+"/img.png")); + Media m = ScreenCapture.builder().path("img.png").build(); + String[] known = {tmpDir.toString()}; + MediaService.tryResolveMediaPath(m, known); + Assert.assertEquals(m.getResolvedPath(), tmpFile.toAbsolutePath().toString()); + tmpFile.toFile().delete(); + tmpDir.toFile().delete(); + } + + @Test + public void testMediaWithSameFileName() throws IOException { + Path tmpDir = Files.createTempDirectory(Paths.get("target"),"known"); + Path tmpFile = Files.copy(Paths.get("src/test/resources/img.png"),Paths.get(tmpDir.toString()+"/img.png")); + Media m = ScreenCapture.builder().path("folder/img.png").build(); + String[] known = {tmpDir.toString()}; + MediaService.tryResolveMediaPath(m, known); + Assert.assertEquals(m.getResolvedPath(), tmpFile.toAbsolutePath().toString()); + tmpFile.toFile().delete(); + tmpDir.toFile().delete(); + } + + @Test + public void testMediaWithUnavailableFile() throws IOException { + Path tmpDir = Files.createTempDirectory(Paths.get("target"),"known"); + Media m = ScreenCapture.builder().path("img.png").build(); + String[] known = {tmpDir.toString()}; + MediaService.tryResolveMediaPath(m, known); + Assert.assertEquals(m.getResolvedPath(), null); + Assert.assertEquals(m.getPath(), "img.png"); + tmpDir.toFile().delete(); + } + + @Test + public void testScreenCaptureCreation() { + Media m = MediaService.createMedia(MediaType.SCREENCAPTURE.toString(), "img.png", "known/img.png", "title", null); + Assert.assertTrue(m instanceof ScreenCapture); + Assert.assertEquals(m.getTitle(), "title"); + Assert.assertEquals(m.getPath(), "img.png"); + Assert.assertEquals(m.getResolvedPath(), "known/img.png"); + Assert.assertNull(((ScreenCapture)m).getBase64()); + } + + @Test + public void testVideoCreation() { + Media m = MediaService.createMedia(MediaType.VIDEO.toString(), "vid.mp4", "known/vid.mp4", "title", null); + Assert.assertTrue(m instanceof Video); + Assert.assertEquals(m.getTitle(), "title"); + Assert.assertEquals(m.getPath(), "vid.mp4"); + Assert.assertEquals(m.getResolvedPath(), "known/vid.mp4"); + Assert.assertNull(((Video)m).getBase64()); + } + + @Test + public void testBase64VideoCreation() { + Media m = MediaService.createMedia(MediaType.VIDEO.toString(), null, null, null, "base64 video"); + Assert.assertTrue(m instanceof Video); + Assert.assertNull(m.getTitle()); + Assert.assertNull(m.getPath()); + Assert.assertNull(m.getResolvedPath()); + Assert.assertEquals(((Video)m).getBase64(), "base64 video"); + } +} diff --git a/src/test/java/com/aventstack/extentreports/entity/service/TestServiceTest.java b/src/test/java/com/aventstack/extentreports/entity/service/TestServiceTest.java index bd8ebe5..719fef5 100644 --- a/src/test/java/com/aventstack/extentreports/entity/service/TestServiceTest.java +++ b/src/test/java/com/aventstack/extentreports/entity/service/TestServiceTest.java @@ -13,6 +13,7 @@ import com.aventstack.extentreports.model.Log; import com.aventstack.extentreports.model.ScreenCapture; import com.aventstack.extentreports.model.Test; +import com.aventstack.extentreports.model.Video; import com.aventstack.extentreports.model.service.TestService; public class TestServiceTest { @@ -140,4 +141,55 @@ public void testHasScreenCaptureDeepNodeLog() { Assert.assertFalse(test.hasScreenCapture()); Assert.assertTrue(TestService.testHasScreenCapture(test, true)); } + + @org.testng.annotations.Test + public void testHasMedias() { + Test test = getTest(); + test.addMedia(Video.builder().path("/vid.mp4").build()); + test.addMedia(ScreenCapture.builder().path("/img.png").build()); + Assert.assertTrue(test.hasScreenCapture()); + Assert.assertTrue(test.hasVideo()); + Assert.assertTrue(test.hasMedia()); + } + + @org.testng.annotations.Test + public void testHasVideoDeepLog() { + Test test = getTest(); + Log log = Log.builder().status(Status.PASS).details("").build(); + log.addMedia(Video.builder().path("/vid.mp4").build()); + test.addLog(log); + Assert.assertFalse(test.hasScreenCapture()); + Assert.assertFalse(test.hasVideo()); + Assert.assertFalse(TestService.testHasScreenCapture(test, true)); + Assert.assertTrue(TestService.testHasMedia(test, true)); + Assert.assertTrue(TestService.testHasVideo(test, true)); + } + + @org.testng.annotations.Test + public void testHasVideoDeepNodeLog() { + Test test = getTest(); + Test node = getTest(); + test.getChildren().add(node); + Log log = Log.builder().status(Status.PASS).details("").build(); + log.addMedia(Video.builder().path("/vid.mp4").build()); + node.addLog(log); + Assert.assertFalse(test.hasScreenCapture()); + Assert.assertFalse(test.hasVideo()); + Assert.assertFalse(TestService.testHasScreenCapture(test, true)); + Assert.assertTrue(TestService.testHasMedia(test, true)); + Assert.assertTrue(TestService.testHasVideo(test, true)); + } + + @org.testng.annotations.Test + public void testHasVideoDeepNode() { + Test test = getTest(); + Test node = getTest(); + test.getChildren().add(node); + node.addMedia(Video.builder().path("/vid.mp4").build()); + Assert.assertFalse(test.hasScreenCapture()); + Assert.assertFalse(test.hasVideo()); + Assert.assertFalse(TestService.testHasScreenCapture(test, true)); + Assert.assertTrue(TestService.testHasMedia(test, true)); + Assert.assertTrue(TestService.testHasVideo(test, true)); + } } diff --git a/src/test/java/com/aventstack/extentreports/reporter/SparkMediaTest.java b/src/test/java/com/aventstack/extentreports/reporter/SparkMediaTest.java new file mode 100644 index 0000000..978cb17 --- /dev/null +++ b/src/test/java/com/aventstack/extentreports/reporter/SparkMediaTest.java @@ -0,0 +1,58 @@ +package com.aventstack.extentreports.reporter; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import com.aventstack.extentreports.ExtentReports; +import com.aventstack.extentreports.MediaEntityBuilder; +import com.aventstack.extentreports.Status; + +public class SparkMediaTest { + private static final String PATH = "target/spark.html"; + + private ExtentReports extent; + + @BeforeMethod + public void before() { + extent = new ExtentReports(); + extent.attachReporter(new ExtentSparkReporter(PATH)); + } + + @Test + public void testMediaInTest() throws IOException { + extent.createTest("Test").addScreenCaptureFromPath("../src/test/resources/img.png") + .addVideoFromPath("../src/test/resources/vid.mp4"); + extent.flush(); + Assert.assertEquals(Files.readAllLines(new File(PATH).toPath()).stream() + .filter(x -> x.contains("src=\"../src/test/resources/img.png\"") + || x.contains("src='../src/test/resources/vid.mp4'")) + .count(), 2); + Assert.assertTrue(Files.readAllLines(new File(PATH).toPath()).stream() + .anyMatch(x -> x.contains("src=\"../src/test/resources/img.png\""))); + Assert.assertTrue(Files.readAllLines(new File(PATH).toPath()).stream() + .anyMatch(x -> x.contains("src='../src/test/resources/vid.mp4'"))); + } + + @Test + public void testMediaInTestNode() throws IOException { + extent.createTest("Test").createNode("node") + .log(Status.PASS, "Screen Log", + MediaEntityBuilder.createScreenCaptureFromPath("../src/test/resources/img.png").build()) + .log(Status.PASS, "Screen Log", + MediaEntityBuilder.createVideoFromPath("../src/test/resources/vid.mp4").build()); + extent.flush(); + Assert.assertEquals(Files.readAllLines(new File(PATH).toPath()).stream() + .filter(x -> x.contains("src=\"../src/test/resources/img.png\"") + || x.contains("src='../src/test/resources/vid.mp4'")) + .count(), 2); + Assert.assertTrue(Files.readAllLines(new File(PATH).toPath()).stream() + .anyMatch(x -> x.contains("src=\"../src/test/resources/img.png\""))); + Assert.assertTrue(Files.readAllLines(new File(PATH).toPath()).stream() + .anyMatch(x -> x.contains("src='../src/test/resources/vid.mp4'"))); + } +} diff --git a/src/test/resources/vid.mp4 b/src/test/resources/vid.mp4 new file mode 100644 index 0000000..6b668ce Binary files /dev/null and b/src/test/resources/vid.mp4 differ