当步骤失败时,如何将屏幕截图附加到 cypress 中的黄瓜报告? [英] How I could attach screenshots to cucumber report in cypress when the step fails?

查看:113
本文介绍了当步骤失败时,如何将屏幕截图附加到 cypress 中的黄瓜报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始使用 cypress 处理黄瓜 html 报告器,但我没有设法将失败步骤的屏幕截图附加到报告中.有没有人对我如何做到这一点有任何想法?现在我的报告如下图所示:=>

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: =>

我想实现这种格式:=>

I would like to achieve this format: =>

推荐答案

看起来你在使用 cypress-cucumber-preprocessor

It looks like you are using cypress-cucumber-preprocessor

我正在考虑使用钩子来执行此操作,但据我所知,您无法访问场景对象(如黄瓜.js)来附加屏幕截图.

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.

但是我确实找到了这个脚本 https://github.com/jcundill/cypress-cucumber-preprocessor/blob/master/fixJson.js 它将遍历并将 cypress 拍摄的屏幕截图(和视频)附加/嵌入到 cypress-cucumber.json 文件中黄瓜预处理器生成

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();

}

其次,我必须在cucumber.json中创建.embeddings数组(否则当你尝试推送它时它会失败,如果它不存在,它不适合我)

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 = [];

之前

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

这篇关于当步骤失败时,如何将屏幕截图附加到 cypress 中的黄瓜报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆