首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cypress - addContext()会记录以前的失败次数,并将其添加到mochawesome报告中的每个“it”场景中

Cypress - addContext()会记录以前的失败次数,并将其添加到mochawesome报告中的每个“it”场景中
EN

Stack Overflow用户
提问于 2019-03-18 13:31:16
回答 3查看 2K关注 0票数 1

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

以下是快照:

下面是我的afterEach()方法:

代码语言:javascript
复制
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'
        })
      });
    } 
  })
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 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',

代码语言:javascript
复制
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

票数 1
EN

Stack Overflow用户

发布于 2019-06-18 23:43:39

您可以添加以下代码:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2020-05-12 21:25:31

如果您需要在测试中使用它(使用test id),解决方法

在你的support/index.js

代码语言:javascript
复制
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)

代码语言:javascript
复制
it('some test', function() {
  // Using window to bypass issue with context
        window['extra'][this.test.id].push( {
          title: 'Hello',
          value: 'World
        })  
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55215026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档