'How to wait for API in Selenium if JQuery is not implemented in the application?

In my application, we are not using JQuery, and instead we use standard js using xmlhttprequest web APIs.

Here is the selenium snippet we generally use if the application waits for ajax with JQuery implementation:

wait = Selenium::WebDriver::Wait.new(:timeout => 20)
wait.until { @driver.execute_script("return jQuery.active").to_i == 0 }

How can we handle wait_for_ajax/wait for API response call if the application doesn't support JQuery?



Solution 1:[1]

The way I generally handle this is to wait for the page to load by waiting for a specific element (or group of elements) to be visible on the page. Once that condition is met, the page is loaded.

If after the page is loaded you perform some action on the page that triggers part of the page to reload, you would then find an element in the dynamic portion of the page to wait for which would indicate that the dynamic load is done.

I use the page object model so I take care of page load in my page object constructor and any dynamic issues I handle in the method that triggers the action, e.g. clickButton() would not only click the button but have a wait that waits for the dynamic load to finish.

Solution 2:[2]

As you mentioned in your Question if the application doesn't support JQuery the simple alternatively will be to use WebDriverWait to get back the availability of the next intended element as follows :

  • WebDriverWait

    WebElement myElement = new WebDriverWait(driver, 10).until(driver => driver.FindElement(By.Id("next_intended_element_id")));
    

Solution 3:[3]

In my java code I do like this :

//Wait for JQuery Load
public static void waitForJQueryLoad() {
    //Wait for jQuery to load
    ExpectedCondition<Boolean> jQueryLoad = driver -> ((Long) ((JavascriptExecutor) jsWaitDriver)
            .executeScript("return jQuery.active") == 0);

    //Get JQuery is Ready
    boolean jqueryReady = (Boolean) jsExec.executeScript("return jQuery.active==0");

    //Wait JQuery until it is Ready!
    if(!jqueryReady) {
        System.out.println("JQuery is NOT Ready!");
        //Wait for jQuery to load
        jsWait.until(jQueryLoad);
    } else {
        System.out.println("JQuery is Ready!");
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JeffC
Solution 2 undetected Selenium
Solution 3 Philippe