当测试用例失败时,我想记录其他数据。我最好在哪里插入自定义错误处理程序?
例如:
cy.get('something')
.should('have.property', 'blah')
在Cypress仪表板中失败时,我得到:
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}
最终,我想记录一些其他上下文数据,以便我可以调查和调试测试可能失败/剥落的原因。
答案 0 :(得分: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 event handling mechanism。这是它的摘录,
// 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
})