当angularjs引发错误时,如何使TestCafe测试失败? [英] How do I make a TestCafe test fail when angularjs throws an error?
问题描述
当angularjs抛出错误时,它会被捕获在try/catch中,然后异常处理程序会将其记录为console.error.结果,testcafe不会使测试在任何时候都失败,但是我们希望这被认为是失败的测试.我们尝试遵循此线程并编写以下代码:
When angularjs throws an error, it's caught in a try/catch and then an exception handler logs it as console.error. As a result, testcafe does not make a test fail whenever this happens, but we would like this to be considered a failing test. We tried to follow a suggestion in this thread and wrote this code:
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
.
A controller calls $scope.undefined()
so it will always throw an error in angular code, but the test still passes. We put console.error = msg => {throw new Error(msg)};
directly in our index.html, and saw that the error printed out like this: 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
.
If we add window.undefined();
directly into our index.html, TestCafe does consider this an error and fails the test. The error message looks like this: Uncaught TypeError: window.undefined is not a function
.
此外,如果将console.error = msg => {throw new Error(msg)};
放在index.html中,则在出现angularjs错误时测试将失败.因此,handleErrors
似乎无法正常工作或在该线程中宣传.
Also, if we put console.error = msg => {throw new Error(msg)};
in the index.html, the test will fail when there's an angularjs error. So it seems like the handleErrors
isn't working as expected or advertised in that thread.
我们已经做了很多这样的测试,现在得出的结论是,角度保存了对console
的引用,因此当我在此handleErrors
中重新分配它时,不会影响控制台的角度用途.我该如何在testcafe测试失败的情况下使角度代码出错?
We've done a bunch of tests like this and have now come to the conclusion that angular saves a reference to console
, so when I re-assign it in this handleErrors
, it's not affecting the console that angular uses. How can I make errors in angular code fail testcafe tests?
对于它的价值,我们使用的是角度1.2.
For what it's worth, we're using angular 1.2.
推荐答案
我们将其放入测试中,这导致控制台错误失败:
We put this in our test and that caused console errors to fail:
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,则可能有不同的解决方案
There's probably a different solution if you're > angularjs 1.2
这篇关于当angularjs引发错误时,如何使TestCafe测试失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!