等到angular 2+完成加载问题 [英] wait until angular 2+ has finished loading issue
问题描述
我最近发布了一个有关我对基于角度的应用程序进行白色测试的问题(参考:
I recently posted a question about an issue I have white testing an angular based application (ref: wait until Angular has finished loading issue )
事实证明,完成的检查对角度1.x应用程序有效,而我们的应用程序在角度6.x上运行.
Turns out that the check done was valid for angular 1.x apps, while our application runs on angular 6.x.
然后我发现了该帖子:确定Angular 2已完成正在运行
I've then found out that post: Detecting that Angular 2 is done running
解释了如何对角度超过2个的应用程序执行类似的检查.我已经按照"Michal Filip"的解释设置了支票.
which explains how to do a similar check but for angular 2+ apps. I've set up the check in a similar fashion to "Michal Filip" explained.
我还尝试使用帖子中进一步提出的ngWebdriver解决方案.
I've also tried to use the ngWebdriver solution proposed further down in the post.
两者都遇到相同的问题:检查将始终返回true,就像在完成加载时返回true一样.
Both suffers of the same problem: the check will always return true as in its done loading which isn't true.
试图逆转检查,但没有帮助(状态从未更改)
tried to inverse the check, it didn't help (state never changed)
// Will check if Angular still has pending http_requests ongoing and wait if required
public void untilAngular2HasFinishedProcessing()
{
until(() ->
{
log.info("Waiting on angular 2+ to finish processing");
final boolean isReady = ((JavascriptExecutor) driver).executeAsyncScript(
"var callback = arguments[arguments.length - 1];" +
"if (document.readyState !== 'complete') {" +
" callback('document not ready');" +
"} else {" +
" try {" +
" var testabilities = window.getAllAngularTestabilities();" +
" var count = testabilities.length;" +
" var decrement = function() {" +
" count--;" +
" if (count === 0) {" +
" callback('complete');" +
" }" +
" };" +
" testabilities.forEach(function(testability) {" +
" testability.whenStable(decrement);" +
" });" +
" } catch (err) {" +
" callback(err.message);" +
" }" +
"}"
).toString().equals("complete");
log.info("Is angular 2+ ready? " + isReady);
return isReady;
}
);
}
// sample call would be
untilAngular2HasFinishedProcessing();
意外:测试将等到Angular完成加载后再返回true
Excpected: the test would wait until Angular is done loading before returning true
实际:从一开始就返回true,我知道情况并非如此.
Actual: Returns true from the start, which I know isn't the case.
可能重复吗?不,因为这是一个基于链接问题中建议的实现的问题.
Possible duplicate? No, because this is a problem question based on the implementation proposed in the linked question.
推荐答案
这是我最终使用的解决方案:
Here's the solution I ended up using:
public boolean untilAngular2HasFinishedProcessing()
{
until(() ->
{
log.info("Waiting on angular 2+ to finish processing");
final boolean isReady = (Boolean.valueOf(((JavascriptExecutor) driver)
.executeScript(
"try{" +
"return (window.getAllAngularTestabilities()[0]._ngZone.hasPendingMicrotasks == " +
"false && " +
"window.getAllAngularTestabilities()[0]._ngZone.hasPendingMacrotasks == false && " +
"window.getAllAngularTestabilities()[0]._ngZone._nesting == 0 &&" +
"window.getAllAngularTestabilities()[0]._ngZone.isStable == true)" +
"}" +
"catch(err) {" +
"if (err.message == ('window.getAllAngularTestabilities is not a function'))" +
"{" +
"return true" +
"}" +
"}")
.toString()));
log.info("Is Angular 2+ ready? " + isReady);
return isReady;
}
);
return true;
}
到目前为止,它一直以一致的方式工作.
That worked on a consistent fashion so far.
这篇关于等到angular 2+完成加载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!