1

在我的 mochawesome-reportaddContext()中,保留先前的计数并将其添加到每个“it”场景中,以防测试用例失败,我将“someValue”作为上下文添加到测试用例中。因此,如果第二个测试用例失败,则值将被打印两次。

以下是快照:

在此处输入图像描述

以下是我的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'
        })
      });
    } 
  })
4

3 回答 3

1

从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-03-18T07:05:32.403 回答
1

您可以添加以下代码:

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”中,您将在报告中看到失败测试的屏幕截图

于 2019-06-18T15:43:38.980 回答
0

如果您需要在测试中使用它的解决方法(使用测试 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
        })  
})
于 2020-05-12T13:25:30.583 回答