未捕获异常上的Cypress不工作



我有下面的示例脚本来实验Cypress中的异常处理。但这个例外并没有被发现。我在这里错过了什么?

Cypress.on('uncaught:exception', (err, runnable) => {
Cypress.log("this is top-level exception hadling")
return false
})
context('Actions', () => {

it('sample_testing', () => {

cy.on('uncaught:exception', (err, runnable) => {
cy.log("this is test-level exception hadling")
return false
})

cy.get("#notfound", {timeout:1000})
})

})

请注意,在我的网页中没有id为的元素。

未捕获的异常仅适用于应用程序错误。Cypress会捕获测试代码中的失败,但随后会报告为测试失败。

测试失败

为了防止这种情况,您可以使用失败事件

Cypress.on('fail', (error, runnable) => {
debugger
// we now have access to the err instance
// and the mocha runnable this failed on
throw error // throw error to have test still fail
})
it('calls the "fail" callback when this test fails', () => {
// when this cy.get() fails the callback
// is invoked with the error
cy.get('element-that-does-not-exist')
})

未捕获的异常

查看配方处理应用程序错误

app.js-抛出错误

document.getElementById('error').addEventListener('click', () => {
console.log('application will throw an error in 1 second')
setTimeout(() => {
console.log('application is about to throw an error')
throw new Error('Things went bad')
}, 1000)
})

测试-捕获错误并选择性地忽略是否有特定消息

it('can be ignored', () => {
/**
* By using "cy.on()" we can ignore an exception in the current test only.
* If you want to register exception handler for all tests using "Cypress.on()"
* @see https://on.cypress.io/catalog-of-events
* @param {Error} e The exception we caught
* @param {Mocha.Runnable} runnable is the current test or hook during which the error is caught
*/
cy.on('uncaught:exception', (e, runnable) => {
console.log('error', e)
console.log('runnable', runnable)
// we can simply return false to avoid failing the test on uncaught error
// return false
// but a better strategy is to make sure the error is expected
if (e.message.includes('Things went bad')) {
// we expected this error, so let's ignore it
// and let the test continue
return false
}
// on any other error message the test fails
})
cy.visit('index.html')
cy.get('button#error').click()
// the error happens after 1000ms
// we can use hard-coded wait, see the other test
// to learn how to avoid an unnecessary wait
cy.wait(1500)
})

我为这样的解决方案尝试了许多选项,这一个为我调试未捕获的异常提供了更好的方法。这个在浏览器控制台中记录异常:

Cypress.on('uncaught:exception', (err, runnable) => {
// returning false here prevents Cypress from
// failing the test
cy.log('Uncaught exception... continuing...')
return false
})

Cypress.on('fail', (error, runnable) => {
console.log(error)
// we now have access to the err instance
// and the mocha runnable this failed on
throw error // throw error to have test still fail
})
Cypress.on('window:before:load', (win) => {
Cypress.log({
name: 'console.log',
message: 'wrap on console.log',
});
// pass through cypress log so we can see log inside command execution order
win.console.log = (...args) => {
Cypress.log({
name: 'console.log',
message: args,
});
};
});
Cypress.on('log:added', (options) => {
if (options.instrument === 'command') {
// eslint-disable-next-line no-console
console.log(
`${(options.displayName || options.name || '').toUpperCase()} ${
options.message
}`,
);
}
});

最新更新