在我的模拟报告中,addContext()保留了之前的计数并将其添加到每个“它”场景中,在测试用例失败的情况下,我将“someValue”作为上下文添加到测试用例中。因此,如果第二个测试用例失败,那么value将打印两次。
以下是快照:

下面是我的afterEach()方法:
afterEach(function () {
if (this.currentTest.state === 'failed') {
var test = this.currentTest
Cypress.on('test:after:run', (test) => {
addContext({ test }, {
title: 'Failing Screenshot: ' + '>> screenshots/' + Cypress.spec.name + '/' + test_name + ' -- ' + test.title + ' (failed)' + '.png <<',
value: 'screenshots/' + Cypress.spec.name + '/' + test_name + ' -- ' + test.title + ' (failed)' + '.png'
//value: 'data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAA+gAAABkCAYAAAAVORraAAACH0lEQVR'
})
});
}
})发布于 2019-03-18 15:05:32
从https://docs.cypress.io/api/events/catalog-of-events.html#Cypress-Events得到了我想要的东西

不过,我得移除Cypress.on('test:after:run', afterEach()
因此,我必须在每个规范文件中指定Cypress.on('test:after:run',
const spec_name = this.title
Cypress.on('test:after:run', (test) => {
if (test.state === 'failed') {
addContext({ test }, {
title: 'Failing Screenshot: ' + '>> screenshots/' + Cypress.spec.name + '/' + spec_name + ' -- ' + test.title + ' (failed)' + '.png <<',
value: 'screenshots/' + Cypress.spec.name + '/' + spec_name + ' -- ' + test.title + ' (failed)' + '.png'
})
}
});这是一种延迟,最好将整个代码放在support/command.js中
发布于 2019-06-18 23:43:39
您可以添加以下代码:
const addContext = require('mochawesome/addContext');
Cypress.on('test:after:run', (test, runnable) => {
if (test.state === 'failed') {
addContext({test}, { title: "Screenshot", value:`../cypress/screenshots/${Cypress.spec.name}/${runnable.parent.title} -- ${test.title} (failed).png` })
}
})在"support/index.js“中,您将在报告中看到失败测试的屏幕截图
发布于 2020-05-12 21:25:31
如果您需要在测试中使用它(使用test id),解决方法
在你的support/index.js中
Cypress.on('test:before:run', (test, runnable) => {
if (!window['extra']) {
window['extra'] = []
}
if (!window['extra'][test.id]) {
window['extra'][test.id] = []
}
})
Cypress.on('test:after:run', (test, runnable) => {
window['extra'][test.id].map((item) => {
addContext({ test }, item)
})
})现在你可以在你的测试中使用它(获取test.id)
it('some test', function() {
// Using window to bypass issue with context
window['extra'][this.test.id].push( {
title: 'Hello',
value: 'World
})
})https://stackoverflow.com/questions/55215026
复制相似问题