I am creating tests for angular based app and I need help.
I have to create a wait which will be waiting until all pending requests will be processed.
Basically in my scenario I want to mark a checkbox but it is only available when GET requests are finished, if I use simple Thread.sleep(2000)
everything is working fine, but I know it is not reliable way to solve this.
i tried two approaches:
First:
AdditionalConditions wait = new AdditionalConditions();
wait.untilAngularFinishHttpCalls();
public void untilAngularFinishHttpCalls() {
final String javaScriptToLoadAngular =
"var injector = window.angular.element('app-root').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(SharedDriver.getDriver(), 20); // timeout = 20 secs
wait.until(pendingHttpCallsCondition);
}
And here I get following error:
org.openqa.selenium.WebDriverException: unknown error: Cannot read property 'element' of undefined (Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 5 milliseconds Build info: version: '2.31.0', revision: '1bd294d185a80fa4206dfeab80ba773c04ac33c0', time: '2013-02-27 13:51:26' System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_121' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8), userDataDir=C:\Users\lpaczek\AppData\Local\Temp\scoped_dir13872_15975}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=59.0.3071.115, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
And I tried also:
WebDriverWait wait = new WebDriverWait(SharedDriver.getDriver(), 15, 500);
wait.until(AdditionalConditions.angularHasFinishedProcessing());
public class AdditionalConditions {
public static ExpectedCondition<Boolean> angularHasFinishedProcessing() {
return new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return Boolean.valueOf(((JavascriptExecutor)
driver).executeScript("return (window.angular !== undefined) && (angular.element(document).injector() !== undefined) && (angular.element(document).injector(). get('$http').pendingRequests.length === 0)").toString());
}
};
}
And here i get Timeout after 15 second.It seems that it does not work. Please help if you can, as far as I know I need to execute JavaScript code and I am newbie in it.