1 | const fs = require("fs");
|
2 | const path = require("path");
|
3 | const report = require("multiple-cucumber-html-reporter");
|
4 | const xml2js = require("xml2js");
|
5 | const JunitReport = require("./JunitReport");
|
6 | const os = require("os");
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | class Reporter {
|
13 |
|
14 | |
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | static generateHTMLReport(capabilities, reportPath, jsonDir) {
|
21 |
|
22 | const GLUED_REPORT_PATH = "glued_report/";
|
23 |
|
24 | this.glueReports(jsonDir + "report.json", jsonReport => {
|
25 | const gluedReport = JSON.parse(jsonReport).reduce((report, feature) => {
|
26 | const featureIndex = report.findIndex(reportFeature => reportFeature.id === feature.id);
|
27 | if (featureIndex !== -1) {
|
28 | report[featureIndex].elements.push(...feature.elements);
|
29 | } else {
|
30 | report.push(feature);
|
31 | }
|
32 | return report
|
33 | }, []);
|
34 |
|
35 | if (!fs.existsSync(path.resolve(jsonDir + GLUED_REPORT_PATH))) {
|
36 | fs.mkdirSync(path.resolve(jsonDir + GLUED_REPORT_PATH))
|
37 | }
|
38 | fs.writeFileSync(path.resolve(jsonDir + GLUED_REPORT_PATH + "report.json"), JSON.stringify(gluedReport));
|
39 |
|
40 | report.generate({
|
41 | jsonDir: path.resolve(jsonDir + "glued_report"),
|
42 | reportPath: path.resolve(reportPath),
|
43 | metadata: {
|
44 | browser: {
|
45 | name: capabilities.browserName,
|
46 | version: capabilities.version
|
47 | },
|
48 | device: "PC",
|
49 | platform: {
|
50 | name: os.platform() === "win32" ? "windows" : os.platform(),
|
51 | version: os.release()
|
52 | }
|
53 | },
|
54 | customData: {
|
55 | title: 'Run info',
|
56 | data: [
|
57 | {label: 'Project', value: capabilities.project},
|
58 | {label: 'Duration', value: capabilities.duration},
|
59 | {label: 'Execution Start Time', value: capabilities.startTime},
|
60 | {label: 'Execution End Time', value: capabilities.endTime}
|
61 | ]
|
62 | }
|
63 | });
|
64 | });
|
65 | }
|
66 |
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 | static generateXMLReport(pathToJson, pathToXml) {
|
74 | const builder = new xml2js.Builder();
|
75 | this.glueReports(pathToJson, jsonReport => {
|
76 | const xml = builder.buildObject(new JunitReport(jsonReport).build());
|
77 | fs.writeFile(path.resolve(pathToXml), xml, (err) => {
|
78 | if (err) throw err
|
79 | })
|
80 | });
|
81 | }
|
82 |
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | static glueReports(pathToJson, cb) {
|
90 | return fs.access(path.resolve(pathToJson), (notExist) => {
|
91 | if (notExist) {
|
92 | const dirPath = pathToJson.replace(/^(.+[\/\\])(.+)$/g, (match, p1) => p1);
|
93 | fs.readdir(path.resolve(dirPath), (err, files) => {
|
94 | const REPORT_REGEXP = /^report\.\d+\.json$/;
|
95 | const reports = files.filter(item => REPORT_REGEXP.test(item));
|
96 | const fullReport = reports
|
97 | .map(item => require(path.resolve(dirPath + item)))
|
98 | .reduce((prev, curr) => {
|
99 | if (curr.length > 0) {
|
100 | prev.push(curr[0]);
|
101 | }
|
102 | return prev
|
103 | }, []);
|
104 | cb(JSON.stringify(fullReport))
|
105 | })
|
106 | } else {
|
107 | cb(JSON.stringify(require(path.resolve(pathToJson))));
|
108 | }
|
109 | })
|
110 | }
|
111 |
|
112 | }
|
113 |
|
114 | module.exports = Reporter; |
\ | No newline at end of file |