我最近发布了一个问题,关于白色测试基于angular的应用程序的问题(参考:wait until Angular has finished loading issue )
事实证明,完成的检查对angular 1.x应用程序有效,而我们的应用程序运行在angular 6.x上。
然后我找到了那篇文章:Detecting that Angular 2 is done running
这解释了如何对angular 2+应用程序进行类似的检查。我已经以类似于"Michal Filip“解释的方式设置了检查。
我还尝试使用了这篇文章中进一步提出的ngWebdriver解决方案。
这两种方法都有相同的问题:检查总是返回true,就像在完成加载时一样,而这不是true。
尝试反向检查,但无济于事(状态从未更改)
// 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
Actual:从一开始就返回true,我知道事实并非如此。
可能是重复的?不是,因为这是一个基于链接问题中提出的实现的问题问题。
发布于 2019-01-17 00:25:57
以下是我最终使用的解决方案:
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;
}到目前为止,它一直在以一致的方式工作。
https://stackoverflow.com/questions/54183044
复制相似问题