Skip to content

Commit 5a66c24

Browse files
Merge pull request #2 from fijijavis/main
Adding plugin functionality to work with CodeceptJS screenshotOnFail plugin
2 parents 7550f37 + 89ac210 commit 5a66c24

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ const defaultConfig = {
2525
module.exports = function (config) {
2626
// eslint-disable-next-line no-param-reassign
2727
config = Object.assign(defaultConfig, config);
28+
const plugin = {};
29+
30+
plugin.addScreenshot = (file_path) => {
31+
addScreenshotToReport(file_path);
32+
};
2833

2934
// Before suite starts, parse the feature file and set report feature position
3035
event.dispatcher.on(event.suite.before, (suite) => {
@@ -68,7 +73,10 @@ module.exports = function (config) {
6873
});
6974

7075
event.dispatcher.on(event.step.finished, (step) => {
71-
if (step.helperMethod === 'saveScreenshot') addScreenshotToReport(step);
76+
if (step.helperMethod === 'saveScreenshot') {
77+
const filePath = path.join(global.output_dir, step.args[0]);
78+
addScreenshotToReport(filePath);
79+
}
7280
});
7381

7482
event.dispatcher.on(event.step.comment, (step) => {
@@ -317,19 +325,18 @@ module.exports = function (config) {
317325
return bddCheck;
318326
}
319327

320-
function addScreenshotToReport(step) {
328+
function addScreenshotToReport(file_path) {
321329
if (!config.attachScreenshots) return;
322-
const filename = path.join(global.output_dir, step.args[0]);
323330
try {
324-
const convertedImg = fs.readFileSync(filename, 'base64');
331+
const convertedImg = fs.readFileSync(file_path, 'base64');
325332
const screenshot = {
326333
data: convertedImg,
327334
mime_type: 'image/png',
328335
};
329-
output.log(`[CucumberJsonReporter] Adding ${filename} to:\n ${JSON.stringify(report.step)}`);
336+
output.log(`[CucumberJsonReporter] Adding ${file_path} to:\n ${JSON.stringify(report.step)}`);
330337
report.step.embeddings.push(screenshot);
331338
} catch (error) {
332-
output.log(`[CucumberJsonReporter] Couldn't add ${filename} to:\n ${JSON.stringify(report.step)}`);
339+
output.log(`[CucumberJsonReporter] Couldn't add ${file_path} to:\n ${JSON.stringify(report.step)}`);
333340
}
334341
}
335342

@@ -342,4 +349,6 @@ module.exports = function (config) {
342349
output.log(`[CucumberJsonReporter] Adding comment "${step}" to:\n ${JSON.stringify(report.step)}`);
343350
report.step.embeddings.push(comment);
344351
}
352+
353+
return plugin;
345354
};

0 commit comments

Comments
 (0)