Archive for February, 2014

Waiting for Angular

Posted by on Friday, 7 February, 2014

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