use of org.openqa.selenium.support.ui.ExpectedCondition in project carina by qaprosoft.
the class AbstractPage method waitForJSToLoad.
/**
* Waits till JS and jQuery (if applicable for the page) are completely processed on the page
*
* @param timeout Completing of JS loading will be verified within specified timeout
*/
public void waitForJSToLoad(long timeout) {
// wait for jQuery to load
JavascriptExecutor executor = (JavascriptExecutor) driver;
ExpectedCondition<Boolean> jQueryLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
try {
return ((Long) executor.executeScript("return jQuery.active") == 0);
} catch (Exception e) {
return true;
}
}
};
// wait for Javascript to load
ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return executor.executeScript("return document.readyState").toString().equals("complete");
}
};
String errMsg = "JS was not loaded on page during expected time";
if ((Boolean) executor.executeScript("return window.jQuery != undefined")) {
Assert.assertTrue(waitUntil(jQueryLoad, timeout) && waitUntil(jsLoad, timeout), errMsg);
} else {
Assert.assertTrue(waitUntil(jsLoad, timeout), errMsg);
}
}
Aggregations