0

当测试用例失败时,我想记录额外的数据。我在哪里可以最好地插入我的自定义错误处理程序?

例如:

cy.get('something')
  .should('have.property', 'blah')

当这在赛普拉斯仪表板中失败时,我得到:

CypressError: Timed out retrying: expected 'something' to have a property 'blah'
  at Object.cypressErr (https://my.website.com/__cypress/runner/cypress_runner.js:65727:11)
  at Object.throwErr (https://my.website.com/__cypress/runner/cypress_runner.js:65692:18)
  at Object.throwErrByPath (https://my.website.com/__cypress/runner/cypress_runner.js:65719:17)
  at retry (https://my.website.com/__cypress/runner/cypress_runner.js:59237:16)
  at https://my.website.com/__cypress/runner/cypress_runner.js:51312:18
  at tryCatcher (https://my.website.com/__cypress/runner/cypress_runner.js:131273:23)
  at Promise._settlePromiseFromHandler (https://my.website.com/__cypress/runner/cypress_runner.js:129291:31)
  at Promise._settlePromise (https://my.website.com/__cypress/runner/cypress_runner.js:129348:18)
  at Promise._settlePromise0 (https://my.website.com/__cypress/runner/cypress_runner.js:129393:10)
  at Promise._settlePromises (https://my.website.com/__cypress/runner/cypress_runner.js:129468:18)
  at Async._drainQueue (https://my.website.com/__cypress/runner/cypress_runner.js:126197:16)
  at Async._drainQueues (https://my.website.com/__cypress/runner/cypress_runner.js:126207:10)
  at Async.drainQueues (https://my.website.com/__cypress/runner/cypress_runner.js:126081:14)
  at <anonymous>

我可以做些什么来自定义这个错误,比如:

CypressError: ...

My additional logging: {foo: 'bar', etc, etc}

最终,我想记录一些额外的上下文数据,这样我就可以调查 + 调试为什么测试可能会失败/失败。

4

1 回答 1

0

我希望这个库能帮助你 - https://github.com/cypress-io/error-message

这里有一个要点,

//Load the function from the module
const {formErrorText} = require('@cypress/error-message')

//Handle the error with custome messages
const fileSaveError = {
  description: 'We could not save an important file',
  solution: `Please check folder permissions and try again

    more details on our FAQ page: https://faq.company.name
  `
}
fs.writeFile(name)
  .catch(
    formErrorText(info).then(console.error)
  )
/*
  shows nice error message

  ------
  We could not save an important file
  Please check folder permissions and try again

    more details on our FAQ page: https://faq.company.name

  Exception message
  ------
  Platform: darwin
  Version: 15.6.2
*/

有关更多信息,您可以查看此博客 - https://www.cypress.io/blog/2017/07/26/good-error-messages/#Useful-E2E-assertion-failures

编辑1:

如评论中所述,如果您正在寻找处理神秘故障的方法,建议您查看Cypress 事件处理机制。这是它的摘录,

// likely want to do this in a support file
// so it's applied to all spec files
// cypress/support/index.js

Cypress.on('uncaught:exception', (err, runnable) => {
  // Handle your logging logic here
})
于 2019-04-23T03:22:17.057 回答