2

[Related philosophical debate about just sleeping it out at programmers.se]

Angualr is not guaranteed to update the DOM completely in the event handler for the AJAX completion (especially if third-party directives are involved), so most of the solutions floating online about using $http interceptors are incomplete.

The answer to a very similar question "Wait for angular to finish updating the DOM", suggests using $timeout. However, its source suggests it uses the $browser.defer mechanism so can actually be executed in any order with other deferred DOM manipulation.

I found $browser.notifyWhenNoOutstandingRequests internal method which happens after all the deferred stuff, but the test is still unstable after waiting for that. I guess I should wait for AJAX to complete first or is there something else I've missed?

Community
  • 1
  • 1
billc.cn
  • 7,187
  • 3
  • 39
  • 79
  • Related: http://stackoverflow.com/questions/29711556/how-to-impact-the-html-rendering-priorities-with-angularjs – sp00m Aug 19 '15 at 14:19

2 Answers2

1

This is how we do it:

function waitForDisplayed(element) {
   var d = protractor.promise.defer(); // the result of this being fulfilled is returned
   browser.wait(element.isDisplayed).
      then(function(isDisplayed) {
         d.fulfill();
      });
      return d.promise;
}
Tone
  • 990
  • 4
  • 14
  • 31
1

After some experiments, it seems notifyWhenNoOutstandingRequests is sufficient.

(Previously, my test code created a new Injector to get the $browser service, which is actually different from the one used by the application. I switched to the "proper" one and it worked.)

Community
  • 1
  • 1
billc.cn
  • 7,187
  • 3
  • 39
  • 79