Waiting for Angular

This entry was posted by on Friday, 7 February, 2014 at

Recently I’ve spent a fair bit of time working with Angular JS applications and as great as Angular is, it can be a pain when it comes to automating it.

The main problem you will probably see is due to the fact that Angular does everything asynchronously, so you’re never quite sure when the page has finished loading, if only there was a way to know Angular had finished before you started doing stuff on a page…

Here’s an ExpectedCondition that will wait for Angular to finish processing stuff on the page:

public static ExpectedCondition angularHasFinishedProcessing() {
        return new ExpectedCondition() {
            @Override
            public Boolean apply(WebDriver driver) {
                return Boolean.valueOf(((JavascriptExecutor) driver).executeScript("return (window.angular != null) && (angular.element(document).injector() != null) && (angular.element(document).injector().get('$http').pendingRequests.length === 0)").toString());
            }
        };
    }

2 Responses to “Waiting for Angular”

  1. Suresh

    Hi Ardesco,
    Thanks for the solution you’ve provided.
    I tried but it returns javascript undefined method error.
    Could you please help me out?

    Cheers,
    Suresh

    • Ardesco

      Try modifying it to:

      return (window.angular != null) && (angular.element(document).injector() != null) && (angular.element(document).injector().get(‘$http’).pendingRequests.length === 0)


Leave a Reply




five − = three