检测到 Angular 2 已完成运行 [英] Detecting that Angular 2 is done running
问题描述
我们使用 Selenium WebDriver 来自动化我们基于 UI 的测试.我们面临的挑战之一是检测页面何时真正完成加载,Angular 1 在这方面也是一个挑战.我们最终专门执行了这段代码来检测 Angular 1 是否完成:
We use Selenium WebDriver to automate our UI based tests. One of our challenges is to detect when a page is truly done loading, and Angular 1 was a challenge in that regard as well. We ended up executing this piece of code specifically to detect if Angular 1 is done:
if(typeof window.angular !== "undefined")
{
var injector = window.angular.element("*[ng-app]").eq(0).injector();
if(injector)
{
var $rootScope = injector.get("$rootScope");
var $http = injector.get("$http");
if($rootScope.$$phase === "$apply" || $rootScope.$$phase === "$digest" || $http.pendingRequests.length !== 0)
{
return false;
}
}
}
我们最近测试的应用切换到使用 Angular 2.上面的代码片段不等待 Angular 2 完成.有什么建议吗?
The app that we are testing recently switched over to use Angular 2. The code snippet above does not wait for Angular 2 to finish. Any suggestions?
推荐答案
如果是 Angular 2,你应该等待 "testabilities" 所有 Angular 2 应用程序:
In case of Angular 2, you should wait for stableness of "testabilities" of all Angular 2 apps:
functions.waitForAllAngular2 = function(callback) {
try {
var testabilities = window.getAllAngularTestabilities();
var count = testabilities.length;
var decrement = function() {
count--;
if (count === 0) {
callback();
}
};
testabilities.forEach(function(testability) {
testability.whenStable(decrement);
});
} catch (err) {
callback(err.message);
}
};
取自 Protractor 源代码.Protractor 是 WebDriverJS javascript selenium 绑定的包装器;旨在测试 AngularJS 应用程序(不仅,而且最适合).
Taken from Protractor source code. Protractor is a wrapper around WebDriverJS javascript selenium bindings; designed to test AngularJS applications (not only, but best suited for).
这篇关于检测到 Angular 2 已完成运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!