我需要在我的TestCafe测试中抑制一个错误(该错误来自一个模拟页面)。示例代码:
function stoperror() {
try {
this.clickMockContinueButton();
} catch(e) {
console.log(e)
}
return true;
}
Call it:
window.onerror = stoperror;尽管我已经添加了节点窗口包:https://www.npmjs.com/package/window
错误= ReferenceError:未定义窗口
发布于 2019-08-06 21:08:32
TestCafe只提供了两种在浏览器中执行JavaScript代码的方法:ClientFunction和t.eval。例如,如果要通过window.onerror属性安装全局错误处理程序,则可以使用以下代码:
const installErrorHandler = ClientFunction(() => {
window.onerror = error => {
// handle error here
};
});
test('Install the error handler', async t => {
await installErrorHandler();
});但我应该警告您,如果您警告要抑制另一个问题中描述的错误,则此方法将不起作用:TestCafe ClientFunction TypeError error as document is undefined
这个问题中的错误发生在ClientFunction上下文中,不能传播到全局错误处理程序。如果要禁止在ClientFunction实例中发生错误,请使用try ... catch语句将代码包装在其主体中:
const dangerousFunction = ClientFunction(() => {
try {
// dangerous code
}
catch (e) {
// handle error
}
});https://stackoverflow.com/questions/57362305
复制相似问题