/
askui-allure-step-reporter.ts
87 lines (80 loc) · 2.2 KB
/
askui-allure-step-reporter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { Reporter, Step, StepStatus, Annotation, DetectedElement, ReporterConfig } from "askui";
import { Status } from "@askui/jest-allure-circus";
import { ContentType } from "allure-js-commons";
import { convertPngDataUrlToBuffer } from "../utils/image-reporting-utils";
import { convertBase64StringToBuffer } from "../utils/video-reporting-utils";
function mapAskuiToAllureStepStatus(status: StepStatus): Status {
switch (status) {
case "passed":
return Status.PASSED;
case "failed":
return Status.FAILED;
case "erroneous":
return Status.BROKEN;
default:
return Status.SKIPPED;
}
}
function createScreenshotAttachment(
name: string,
screenshot: string,
detectedElements?: Readonly<Readonly<DetectedElement>>[])
{
if (detectedElements === undefined) {
return {
name,
type: "image/png",
content: convertPngDataUrlToBuffer(screenshot),
};
}
const annotation = new Annotation(
screenshot,
detectedElements
);
return {
name: `${name} (annotated)`,
type: {
contentType: "text/html",
fileExtension: ".html",
},
content: annotation.toHtml().serialize(),
};
}
export class AskUIAllureStepReporter implements Reporter {
config?: ReporterConfig;
constructor(config?: ReporterConfig) {
if (config !== undefined) {
this.config = config;
}
}
async onStepEnd(step: Step): Promise<void> {
const status = mapAskuiToAllureStepStatus(step.status);
const attachments = [];
if (step.lastRun?.begin?.screenshot !== undefined) {
attachments.push(createScreenshotAttachment(
"Before Screenshot",
step.lastRun?.begin?.screenshot,
step.lastRun?.begin?.detectedElements
));
}
if (step.lastRun?.end?.screenshot !== undefined) {
attachments.push(createScreenshotAttachment(
"After Screenshot",
step.lastRun?.end?.screenshot,
step.lastRun?.end?.detectedElements
));
}
allure.logStep(
step.instruction.valueHumanReadable,
status,
attachments,
);
}
static attachVideo(webm: string) {
allure.createAttachment(
"Video",
convertBase64StringToBuffer(webm),
ContentType.WEBM
);
}
}