繁体   English   中英

当angularjs抛出错误时,如何使TestCafe测试失败?

[英]How do I make a TestCafe test fail when angularjs throws an error?

当angularjs抛出错误时,它会被try / catch捕获,然后异常处理程序将其记录为console.error。 因此,只要发生这种情况,testcafe就不会使测试失败,但我们希望将其视为失败的测试。 我们尝试按照此主题中的建议编写此代码:

fixture `Getting Started`
    .page `http://localhost:${port}/`;

const handleErrors = ClientFunction(() => {
    console.error = msg => {throw new Error(msg)};
});


test('Test', async t => {
    await handleErrors();
    //visit page with error, go somewhere else, return to page
    await t
        .expect(Selector('body').innerText).contains('hello world')

});

控制器调用$scope.undefined()因此它将始终在角度代码中引发错误,但测试仍然通过。 我们把console.error = msg => {throw new Error(msg)}; 直接在我们的index.html中,看到错误打印出来像这样: Uncaught Error: Error: Error: Error: TypeError: $scope.undefined is not a function

如果我们添加window.undefined(); 直接进入我们的index.html,TestCafe确实认为这是一个错误并且未通过测试。 错误消息如下所示: Uncaught TypeError: window.undefined is not a function

另外,如果我们把console.error = msg => {throw new Error(msg)}; 在index.html中,当出现angularjs错误时,测试将失败。 所以似乎handleErrors没有按预期工作或在该线程中公布。

我们做了一堆这样的测试,现在得出结论,angular保存了对console的引用,所以当我在这个handleErrors重新分配它时,它不会影响角度使用的控制台。 如何在角度代码失败的testcafe测试中出错?

对于它的价值,我们使用角度1.2。

我们把它放在我们的测试中,导致控制台错误失败:

fixture `Getting Started`
    .page `http://localhost:${port}/`
    .beforeEach(async t => await failOnJsErrors());

const failOnJsErrors = ClientFunction(() => {
    angular.element(document.body).injector().invoke(function($log) {
        $log.error = function(message) {
            throw new Error(message)
        };
    });
});

如果你是> angularjs 1.2,可能会有不同的解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM