title: Selenium - Determine whether web page is finished loading in Angular 2+
body: <p>I have a Selenium test suite that is running Selenium integration tests against a number of web applications, some that are written in Angular 2+, and some that are written in AngularJS.</p>
  
  <p>We use a custom <code>ExpectedCondition</code> with <code>WebDriverWait</code> that we use to make test cases wait until AngularJS apps have finished loading, in order to avoid waiting an arbitrary amount of time:</p>
  
  <pre><code>private static ExpectedCondition&lt;Boolean&gt; angularIsFinished() {
      return new ExpectedCondition&lt;Boolean&gt;() {
          public Boolean apply(final WebDriver driver) {
              Object result = null;
  
              while(result == null || result.toString().equals("undefined")) {
                  result = ((JavascriptExecutor)driver).executeScript("return typeof angular;");
  
                  try {
                      Thread.sleep(200L);
                  } catch (final InterruptedException ex) {
                      logger.error("Error while trying to sleep", ex);
                  }
              }
  
              final String script = "  var el = document.querySelector(\"body\");\n" +
                      "  var callback = arguments[arguments.length - 1];\n" +
                      "  angular.element(el).injector().get('$browser').notifyWhenNoOutstandingRequests(callback);";
              ((JavascriptExecutor)driver).executeAsyncScript(script);
              return true;
          }
  
          public String toString() {
              return "Wait for AngularJS";
          }
      };
  }
  </code></pre>
  
  <p>However, <code>return typeof angular;</code> will always return <code>undefined</code> for an Angular 2+ app. Is there a similar way to AngularJS's <a href="https://github.com/angular/angular.js/blob/master/src/ng/browser.js#L54" rel="nofollow noreferrer">notifyWhenNoOutstandingRequests</a> that you can use to determine when an Angular 2+ app has finished loading?</p>
  
  <p><a href="https://stackoverflow.com/questions/35926126/does-notifywhennooutstandingrequests-or-an-equivalent-exist-in-angular-2/35926158">This question</a> mentions using <code>NgZone</code> as a possible solution, but how would you get a handle on that via a script executed via <code>JavascriptExecutor</code>?</p>