Sorry to flood this topic, but I can’t help myself… I got curious about the SmartWait implementation:
public static void doSmartWait() {
WebDriver main = DriverFactory.getWebDriver();
JavascriptExecutor js = (JavascriptExecutor) main;
try {
js.executeAsyncScript(WAIT_AJAX_SCRIPT);
js.executeAsyncScript(WAIT_DOM_SCRIPT);
} catch (Exception e) {
// Ignore exceptions to avoid clogging user's console log
}
}
private static String WAIT_AJAX_SCRIPT = "\tvar callback = arguments[arguments.length - 1].bind(this);\r\n\twindow.katalonWaiter.katalon_smart_waiter_do_ajax_wait(callback);";
private static String WAIT_DOM_SCRIPT = "\tvar callback = arguments[arguments.length - 1].bind(this);\r\n\twindow.katalonWaiter.katalon_smart_waiter_do_dom_wait(callback);";
}
Basically it executes some custom JS in the katalonWaiter class to wait for certain things (AJAX, DOM changes I’m assuming), but I’m not able to find where this katalonWaiter class is sourced from in the repo
More to the original topic, I do have somewhat of a catch all wait condition that I use every now and then:
public static void waitForElementRendering() {
By locator = By.xpath("//*");
int elementCount = driver.findElements(locator).size();
while(driver.findElements(locator).size() != elementCount) {
elementCount = driver.findElements(locator).size();
}
}
Hopefully what I’m doing here is obvious: I’m just waiting for the number of elements in the DOM to stop changing. This actually works pretty well, but has a couple of caveats:
1.) It doesn’t consider changing attributes. Only the actual number of elements in the DOM at any given time.
2.) For very large pages, this is quite inefficient. It will often wait 2 or 3 seconds or even more, simply because locating 1000’s of elements in a loop is expensive.