1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package dev.aherscu.qa.testrail.reporter;
18
19 import static dev.aherscu.qa.testing.utils.FileUtilsExtensions.*;
20 import static java.lang.Long.*;
21 import static java.nio.charset.StandardCharsets.*;
22 import static org.apache.commons.io.FilenameUtils.*;
23
24 import java.io.*;
25 import java.util.*;
26
27 import com.samskivert.mustache.*;
28 import com.tngtech.jgiven.report.model.*;
29
30 import dev.aherscu.qa.jgiven.reporter.*;
31 import dev.aherscu.qa.testing.utils.*;
32 import lombok.*;
33 import lombok.experimental.*;
34 import lombok.extern.slf4j.*;
35
36
37
38
39
40
41 @SuperBuilder(toBuilder = true)
42 @Slf4j
43 public class TestRailReportModel extends QaJGivenReportModel<ScenarioModel> {
44 public final File outputDirectory;
45
46
47
48
49
50
51
52
53
54 public final Mustache.Lambda saveScreenshot = this::saveScreenshot;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 @SneakyThrows
73 public void saveScreenshot(final Template.Fragment frag, final Writer out) {
74 val screenshotsDirectory = new File(outputDirectory,
75 removeExtension(targetReportFile.getName()));
76 if (!screenshotsDirectory.exists()) {
77 log.trace("creating screenshots directory {}",
78 screenshotsDirectory);
79 forceMkdir(screenshotsDirectory);
80 }
81 val screenshotHash = toHexString(frag.execute().hashCode());
82 out.write(screenshotHash);
83 val screenshotFile =
84 new File(screenshotsDirectory, screenshotHash + ".png");
85 log.trace("saving screenshot to {}", screenshotFile);
86 try (
87 val screenshotOutputStream = new FileOutputStream(screenshotFile);
88 val byteArrayInputStream = new ByteArrayInputStream(Base64
89 .getMimeDecoder()
90 .decode(frag.execute().getBytes(UTF_8)));
91 val fileOutputStream = ImageUtils.Pipeline
92 .from(byteArrayInputStream)
93 .into(screenshotOutputStream, "png")) {
94
95 }
96 }
97 }