【问题标题】:wait until angular 2+ has finished loading issue等到 Angular 2+ 完成加载问题
【发布时间】:2019-01-14 14:05:37
【问题描述】:

我最近发布了一个关于我在测试基于 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,因为在完成加载时这是不正确的。

试图反转检查,但没有帮助(状态从未改变)

// 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,我知道不是这样。

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

【问题讨论】:

    标签: java angular selenium


    【解决方案1】:

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

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

    到目前为止,这种方式一直有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-11
      • 2023-03-24
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-18
      相关资源
      最近更新 更多