首页 > 解决方案 > 当步骤失败时,如何将屏幕截图附加到柏树中的黄瓜报告?

问题描述

最近我开始使用柏树与黄瓜 html 记者合作,但我没有设法将失败步骤的屏幕截图附加到报告中。有人知道我该怎么做吗?现在我的报告如下图所示:=> 在此处输入图像描述

我想实现这种格式:=>

在此处输入图像描述

标签: cucumberreportingui-automationcypress

解决方案


看起来您正在使用 cypress-cucumber-preprocessor

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

但是我确实找到了这个脚本https://github.com/jcundill/cypress-cucumber-preprocessor/blob/master/fixJson.js它将通过并将赛普拉斯拍摄的屏幕截图(和视频)附加/嵌入到cypress-cucumber-preprocessor 生成的 cucumber.json 文件

然后,当您生成报告时,您将看到失败测试的屏幕截图和视频

请注意,我不得不稍微摆弄一下才能让它为我工作

首先,从屏幕截图中确定场景名称的正则表达式确实对我有用,我用一个函数替换了它

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 数组(否则当你尝试推送它时它会失败,如果它不存在,它不适合我)

myStep.embeddings = [];

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

虽然,看着它,最好先检查它的存在,并在需要时创建它,但是嘿它对我有用

但除此之外,它就像一个魅力


推荐阅读