Sebastian
Sebastian

Reputation: 309

How I could attach screenshots to cucumber report in cypress when the step fails?

recently I started working with cucumber html reporter using cypress, but I didn't manage to attach screenshot on the failed step to the report. Does anybody have any idee on how I could do that? Now my report looks like in the image bellow: => enter image description here

I would like to achieve this format: =>

enter image description here

Upvotes: 1

Views: 3132

Answers (1)

Bill Carter
Bill Carter

Reputation: 9

It looks like you are using cypress-cucumber-preprocessor

I was looking at using hooks to do this but as far as I can tell you dont have any access to the scenario objects (like in cucumber.js) to attach screenshots too.

However I did find this script https://github.com/jcundill/cypress-cucumber-preprocessor/blob/master/fixJson.js which will go through and attach/embed the screenshots (and videos too) that cypress takes, to the cucumber.json files that cypress-cucumber-preprocessor generates

Then when you generate the report you will see screenshots and videos for failed tests

Note I had to fiddle with it a bit to get it working for me

Firstly, The regex for determining the scenario names from the screenshot did work for me, I replaced it with a function

function getScenarioNameFromScreenshot(screenshot) {
const index1 = screenshot.indexOf('--');
let index2;
if (screenshot.indexOf('(example') === -1) {
    // Normal end index of scenario in screenshot filename
    index2 = screenshot.indexOf('(failed)');
} else {
    // End index of scenario if its from a failed BDD example
    index2 = screenshot.indexOf('(example');
}
return screenshot
    .substring(index1 + 2, index2)
    .trim();

}

Secondly, I had to create the .embeddings array in the cucumber.json (otherwise it fails when you try and push to it, if it doesn't exist, which it didn't for me)

myStep.embeddings = [];

Before

myStep.embeddings.push({ data: base64Image, mime_type: "image/png" });

Although, looking at it, it would probably be better to check for its existence first, and create it if needed, but hey it works for me

But otherwise it worked like a charm

Upvotes: 1

Related Questions