I am currently developing tests for an angular-based application and I find myself in need of assistance. The specific task at hand involves creating a mechanism that will wait until all pending requests within the application have been processed before proceeding. In essence, I am trying to interact with a checkbox element that only becomes available once all GET requests have been completed. While using Thread.sleep(2000)
temporarily solves the issue, I am aware that this is not a reliable long-term solution.
So far, I have attempted two different approaches:
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);
}
However, I encountered the 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=}]
My second attempt involved the following code snippet:
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());
}
};
}
Unfortunately, this resulted in a timeout after 15 seconds indicating that it was not successful. It appears that my understanding of executing JavaScript code is limited, and I would greatly appreciate any guidance or assistance you can offer in resolving this issue.