问题描述:
我们最近在 Protractor 端到端测试中打开应用程序中的一个页面时遇到了这个臭名昭著的错误:
Failed: Timed out waiting for asynchronous Angular tasks to finish after 50 seconds. This may be because the current page is not an Angular application.
这发生在我们的一个测试中的 browser.get("/some/page/");
调用上:
describe("Test", function () {
beforeEach(function () {
browser.get("/some/page/");
});
it("should test something", function () {
// ...
});
)};
而且,我们的案例的奇怪之处在于,错误不会在我们的 Angular Web 应用程序的任何其他页面上引发 - Protractor 与 Angular 同步没有任何问题。 ng-app
位置方面的东西在所有页面上都是相同的 - ng-app
是在根 html
标签上定义的:
<html class="ng-scope" lang="en-us" ng-app="myApp" ng-strict-di="">
行为是一致的 - 每次我们使用 browser.get()
导航到此页面时,我们都会收到此错误。每当我们导航到我们应用程序中的任何其他页面时,同步都会起作用。
请注意,当然,我们可以关闭此页面的同步并将其视为非 Angular ,但这只能被视为一种解决方法。
问题:
还有什么会导致 Protractor-to-Angular 同步失败?我们应该检查什么?
而且,一般来说,在 Protractor 中调试同步问题的推荐方法是什么?
使用当前最新的 Protractor 5.5.1,Angular 1.5.6。
最佳答案
好吧,这个问题引起了我的兴趣,所以我提出了一个关于如何确定 Protractor 在等待什么的编程解决方案:
var _injector = angular.element(document).injector();
var _$browser = _injector.get('$browser');
var _$http = _injector.get('$http');
var pendingTimeout = true;
//this is actually method that protractor is using while waiting to sync
//if callback is called immediately that means there are no $timeout or $http calls
_$browser.notifyWhenNoOutstandingRequests(function callback () {
pendingTimeout = false
});
setTimeout(function () {
//this is to differentiate between $http and timeouts from the "notifyWhenNoOutstandingRequests" method
if (_$http.pendingRequests.length) {
console.log('Outstanding $http requests', _$http.pendingRequests.length)
} else if (pendingTimeout) {
console.log('Outstanding timeout')
} else {
console.log('All fine in Angular, it has to be something else')
}
}, 100)
这里是 plunker http://plnkr.co/edit/O0CkpnsnUuwEAV8I2Jil?p=preview您可以尝试超时和 $http 调用,我的延迟端点将等待 10 秒才能解决调用,希望这对您有所帮助
关于javascript - 调试 Protractor 到 Angular 同步问题的规范方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42865376/