forked from RaghavendhraK/cucumber-html-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cucumber-html-report.js
413 lines (352 loc) · 11.2 KB
/
cucumber-html-report.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
var
fs = require("fs"),
path = require("path"),
slug = require("slug"),
atob = require("atob"),
Mustache = require("mustache"),
Directory = require("./lib/directory.js"),
Summary = require("./lib/summary.js"),
R = require("ramda");
if (!Object.assign) {
Object.assign = require("object-assign");
}
/**
* Rouds a number to the supplied decimals. Only makes sense for floats!
* @param decimals The maximum number of decimals expected.
* @param number The number to round.
* @returns {number} The rounded number. Always returns a float!
*/
var round = function (decimals, number) {
return Math.round(number * Math.pow(10, decimals)) / parseFloat(Math.pow(10, decimals));
};
function getDataUri(file) {
var bitmap = fs.readFileSync(file);
return new Buffer(bitmap).toString("base64");
}
var defaultTemplate = path.join(__dirname, "templates", "sample.html");
var CucumberHtmlReport = module.exports = function(options) {
this.options = options || {};
};
CucumberHtmlReport.prototype.createReport = function() {
var options = this.options;
var features = parseFeatures(options, loadCucumberReport(this.options.source));
durationCounter(features);
var templateFile = options.template || defaultTemplate;
var template = loadTemplate(templateFile);
var stepsSummary = [];
var scenarios = [];
//Extracts steps from the features.
features.map(function (feature, index) {
feature.index = index;
var steps = R.compose(
R.flatten(),
R.map(function (scenario) {
return scenario.steps;
}),
R.filter(function (element) {
return element.type === "scenario";
})
)(feature.elements);
stepsSummary.push({
"all": 0,
"passed": 0,
"skipped": 0,
"failed": 0
});
//Counts the steps based on their status.
steps.map(function (step) {
switch (step.result.status) {
case "passed":
stepsSummary[index].all++;
stepsSummary[index].passed++;
break;
case "skipped":
stepsSummary[index].all++;
stepsSummary[index].skipped++;
break;
default:
stepsSummary[index].all++;
stepsSummary[index].failed++;
break;
}
stepDurationConverter(step);
});
scenarios.push({
all: 0,
passed: 0,
failed: 0
});
R.compose(
R.map(function (status) {
scenarios[index].all++;
scenarios[index][status]++;
}),
R.flatten(),
R.map(function (scenario) {
return scenario.status;
}),
R.filter(function (element) {
return element.type === "scenario";
})
)(feature.elements);
});
var scenariosSummary = R.compose(
R.filter(function (element) {
return element.type === "scenario";
}),
R.flatten(),
R.map(function (feature) {
return feature.elements
})
)(features);
var summary = Summary.calculateSummary(features);
//Replaces "OK" and "NOK" with "Passed" and "Failed".
summary.status = summary.status === "OK" ? "passed" : "failed";
var tags = mappingTags(features);
var tagsArray = createTagsArray(tags);
var mustacheOptions = Object.assign({}, options, {
features: features,
featuresJson: JSON.stringify(R.pluck("name", scenariosSummary)),
stepsSummary: stepsSummary,
scenariosSummary: JSON.stringify(scenariosSummary),
stepsJson: JSON.stringify(stepsSummary),
scenarios: scenarios,
scenariosJson: JSON.stringify(scenarios),
summary: summary,
logo: encodeLogo(options.logo),
charts: displayCharts(options),
screenshots: encodeScreenshot(options),
tags: tagsArray,
tagsJson: JSON.stringify(tagsArray),
image: mustacheImageFormatter,
duration: mustacheDurationFormatter
});
var html = Mustache.to_html(template, mustacheOptions);
saveHTML(options.dest, options.name, html);
console.log("Report created successfully!");
return true;
};
function durationCounter(features){
R.map(function (feature) {
var duration = R.compose(
R.reduce(function (accumulator, current) {
return accumulator + current;
}, 0),
R.flatten(),
R.map(function (step) {
return step.result.duration ? step.result.duration : 0;
}),
R.flatten(),
R.map(function (element) {
return element.steps;
})
)(feature.elements);
if (duration && duration / 60000000000 >= 1) {
//If the test ran for more than a minute, also display minutes.
feature.duration = Math.trunc(duration / 60000000000) + " m " + round(2, (duration % 60000000000) / 1000000000) + " s";
} else if (duration && duration / 60000000000 < 1) {
//If the test ran for less than a minute, display only seconds.
feature.duration = round(2, duration / 1000000000) + " s";
}
})(features);
}
function createTagsArray(tags){
return (function (tags) {
var array = [];
for (var tag in tags) {
if (tags.hasOwnProperty(tag)) {
//Converts the duration from nanoseconds to seconds and minutes (if any)
var duration = tags[tag].duration;
if (duration && duration / 60000000000 >= 1) {
//If the test ran for more than a minute, also display minutes.
tags[tag].duration = Math.trunc(duration / 60000000000) + " m " + round(2, (duration % 60000000000) / 1000000000) + " s";
} else if (duration && duration / 60000000000 < 1) {
//If the test ran for less than a minute, display only seconds.
tags[tag].duration = round(2, duration / 1000000000) + " s";
}
array.push(tags[tag]);
}
}
return array;
})(tags);
}
function encodeScreenshot(options){
if(!options.screenshots) {
return undefined;
} else {
return fs.readdirSync(options.screenshots).map(function (file) {
if (file[0] === ".") { return undefined; };
var name = file.split(".");
var extension = name.pop();
extension === "svg" ? extension = "svg+xml" : false;
return {
name: name.join(".").replace(/\s/, "_"),
url: "data:image/" + extension + ";base64," + getDataUri(options.screenshots + "/" + file)
};
}).filter(function (image) {
return image;
});
}
}
function stepDurationConverter(step){
//Converts the duration from nanoseconds to seconds and minutes (if any)
var duration = step.result.duration;
if (duration && duration / 60000000000 >= 1) {
//If the test ran for more than a minute, also display minutes.
step.result.convertedDuration = Math.trunc(duration / 60000000000) + " m " + round(2, (duration % 60000000000) / 1000000000) + " s";
} else if (duration && duration / 60000000000 < 1) {
//If the test ran for less than a minute, display only seconds.
step.result.convertedDuration = round(2, duration / 1000000000) + " s";
}
}
function mappingTags(features) {
var tags = {};
features.map(function (feature) {
[].concat(feature.tags).map(function (tag) {
if (!(tag in tags)) {
tags[tag] = {
name: tag,
scenarios: {
all: 0,
passed: 0,
failed: 0
},
steps: {
all: 0,
passed: 0,
failed: 0,
skipped: 0
},
duration: 0,
status: "passed"
};
}
feature.elements.map(function (element) {
if (element.type === "scenario") {
tags[tag].scenarios.all++;
tags[tag].scenarios[element.status]++;
}
element.steps.map(function (step) {
if (step.result.duration) {
tags[tag].duration += step.result.duration;
}
tags[tag].steps.all++;
tags[tag].steps[step.result.status]++;
});
});
if(tags[tag].scenarios.failed > 0) {
tags[tag].status = "failed";
}
})
});
return tags;
}
function displayCharts(options) {
if(!options.displayCharts) {
return undefined;
} else {
return true;
}
}
function isValidStep(step) {
return step.name !== undefined;
}
function loadCucumberReport(fileName) {
return JSON.parse(fs.readFileSync(fileName, "utf-8").toString());
}
function parseFeatures(options, features) {
return features
.map(getFeatureStatus)
.map(parseTags)
.map(function(feature) {
return processScenarios(feature, options);
});
}
function loadTemplate(templateFile) {
return fs.readFileSync(templateFile).toString();
}
function createFileName(name) {
return slug(name, "_");
}
function saveHTML(targetDirectory, reportName, html) {
fs.writeFileSync(path.join(targetDirectory, reportName || "index.html"), html);
}
function writeImage(fileName, data) {
fs.writeFileSync(fileName, new Buffer(data, "base64"));
console.log("Wrote %s", fileName);
}
function getFeatureStatus(feature) {
feature.status = Summary.getFeatureStatus(feature);
return feature;
}
function getScenarioStatus(scenario) {
return Summary.getScenarioStatus(scenario);
}
function parseTags(feature) {
if (feature.tags !== undefined) {
feature.tags = feature.tags.map(function(tag) {
return tag.name;
}).join(", ");
} else {
feature.tags = "";
}
return feature;
}
function isScenarioType(scenario){
return scenario.type === "scenario";
}
function processScenario(options) {
return function(scenario) {
scenario.status = getScenarioStatus(scenario);
saveEmbeddedMetadata(options.dest, scenario, scenario.steps);
scenario.steps = scenario.steps.filter(isValidStep);
}
}
function processScenarios(feature, options) {
var scenarios = (feature.elements || []).filter(isScenarioType);
scenarios.forEach(processScenario(options));
return feature;
}
function saveEmbeddedMetadata(destPath, element, steps) {
steps = steps || [];
steps.forEach(function(step) {
if (step.embeddings) {
step.embeddings.forEach(function(embedding) {
if (embedding.mime_type === "image/png") {
var imageName = createFileName(element.name + "-" + element.line) + ".png";
var fileName = path.join(destPath, imageName);
// Save imageName on element so we use it in HTML
element.imageName = imageName;
writeImage(fileName, embedding.data);
}
else if (embedding.mime_type === "text/plain") {
// Save plain text on element so we use it in HTML
element.plainTextMetadata = element.plainTextMetadata || [];
var decodedText = atob(embedding.data);
element.plainTextMetadata.push(decodedText);
}
});
}
});
}
function mustacheImageFormatter() {
return function (text, render) {
var src = render(text);
if (src.length > 0) {
return "<img src=" + src + "/>";
} else {
return "";
}
};
}
function mustacheDurationFormatter() {
// nanoseconds according to:
// https://groups.google.com/forum/#!topic/cukes/itAKGVwJHFg
return function(text, render) {
return render(text);
};
}
function encodeLogo (logoPath) {
var logoExtension = logoPath.split(".").pop();
return "data:image/" + (logoExtension === "svg" ? "svg+xml" : logoExtension) + ";base64," + getDataUri(logoPath);
}