等到angular 2+完成加载问题

时间:2019-01-14 14:05:38

标签: java angular selenium

我最近发布了一个有关我对基于角度的应用程序进行白色测试的问题(参考:wait until Angular has finished loading issue

事实证明,完成的检查对角度1.x应用程序有效,而我们的应用程序在角度6.x上运行。

然后我发现了该帖子:Detecting that Angular 2 is done running

解释了如何对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

实际:从一开始就返回true,我知道情况并非如此。

可能重复吗?不,因为这是一个基于链接问题中建议的实现的问题。

1 个答案:

答案 0 :(得分:0)

这是我最终使用的解决方案:

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;
  }

到目前为止,它一直以一致的方式工作。

相关问题