赛普拉斯(Cypress)-addContext()保留先前的失败计数,并将其添加到mochawesome报告中的每个“ it”场景中

时间:2019-03-18 05:31:16

标签: javascript node.js cypress mochawesome

在我的mochawesome-report while 'child' in original_json.keys(): original_json = original_json['child'][0] print(original_json) else: print(original_json) 中,保持先前的计数并将其添加到每个'it'方案中,以防万一测试用例失败,我在测试用例中添加了'someValue'作为上下文。因此,如果第二个测试用例失败,那么值将被打印两次。

以下是快照:

enter image description here

以下是我的addContext()方法:

afterEach()

3 个答案:

答案 0 :(得分: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”内部,您将在报告中包含失败测试的屏幕截图

答案 1 :(得分:0)

了解了https://docs.cypress.io/api/events/catalog-of-events.html#Cypress-Events

的内容

enter image description here

尽管我必须删除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

答案 2 :(得分: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
        })  
})