0

在过去的 10 天里,我尝试在 afterEach 函数中使用 addContext 函数,就像我在 "test:after:run" 发布操作中使用的那样。这样做的原因是我在测试用例期间进行了可视化测试。这有助于我获得一个非常有用的屏幕截图,它提供了两个图像之间的差异。

我想使用 addContext 函数在 Mochawesome 报告中添加这些屏幕截图,但在“test:after:run”中,我无法查看文件夹以找到屏幕截图或使用 cy.task。另一方面,我可以在 afterEach 中使用 cy.task。现在我尝试用作 addContext 函数的第一个参数的所有内容都不起作用。

我的代码在这里:

Cypress.on("test:after:run", (test, runnable) => {
    let item = runnable
    const fullTestName = giveFullTestName(item, runnable.title)
    // This is to add the failed image when the test fails
    if (test.state === "failed") {
        const imageUrl = `../cypress/snapshots/actual/${Cypress.spec.name
            }/${fullTestName} (failed).png`
        addContext({ test }, imageUrl.replace('#', '%23').replace(':', ''));
    }
});

上一节不允许使用 cy.task 命令。

afterEach(() => {
    const test = cy.state('runnable')?.ctx;
    cy.task('screenshots').then(elements => {
        elements.forEach(element => {
            addContext({ test }, element.replace('#', '%23').replace(':', ''))
        })
    })                                                                                                                                        
});

screenshots 任务正在返回测试用例期间生成的所有屏幕截图。

我尝试用作参数但没有成功:

测试['_runnable']、测试['test']和测试['currentTest']

测试对象

谁能帮我将这些屏幕截图添加到报告中?

先感谢您。

4

0 回答 0