Skip to content

Instantly share code, notes, and snippets.

@priyanshus
Created September 4, 2015 07:07
Show Gist options
  • Save priyanshus/d64a51249f214392b14f to your computer and use it in GitHub Desktop.
Save priyanshus/d64a51249f214392b14f to your computer and use it in GitHub Desktop.
Wait until angular finishes $http calls in Selenium webdriver
/**
* Sometime it happens while automating the angular app, view gets loaded entirely but performing any action
* on that view fails the test. This could happen because angular $http calls are still pending in backend.
* We can have explicit wait in this way to ensure that angular has made all the $http calls.
* Wait until angular finishes the $http calls while loading the view
*/
public void waitForAngular() {
final String javaScriptToLoadAngular =
"var injector = window.angular.element('body').injector();" +
"var $http = injector.get('$http');" +
"return ($http.pendingRequests.length === 0)";
ExpectedCondition<Boolean> pendingHttpCallsCondition = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver).executeScript(javaScriptToLoadAngular).equals(true);
}
};
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(pendingHttpCallsCondition);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment