Muhammad Hamza Yousuf
Muhammad Hamza Yousuf

Reputation: 92

Selenium wait for specific XHR request to be complete

Hi i am working on a selenium project and the top difficulty that i am having was waiting for XHR request to be completed. What i am currently doing is i wait for a request to be made using following expected condition,

public ExpectedCondition<Boolean> jQueryExpect (int expectedActive) {
    ExpectedCondition<Boolean> jQLoad = new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver dr) {
            try {
                logger.log(Level.INFO,"Checking number of jQueries Active");

                Long active = (Long) ((JavascriptExecutor) driver).executeScript("return jQuery.active");

                logger.log(Level.INFO,"jQuery''s active: {0}",active);
                return (active >= expectedActive);
            }
            catch (Exception e) {

                logger.log(Level.WARNING,"Error executing script in jQueryLoad method");
                // no jQuery present
                return true;
            }
        }
    };
    return  jQLoad;
}

And then i wait for the jQuery to load using this expected condition

public ExpectedCondition<Boolean> jQueryLoad (int expectedActive) {
    ExpectedCondition<Boolean> jQLoad = new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver dr) {
            try {
                logger.log(Level.INFO,"Checking number of jQueries Active");

                Long active = (Long) ((JavascriptExecutor) driver).executeScript("return jQuery.active");

                logger.log(Level.INFO,"jQuery''s active: {0}",active);
                return (active <= expectedActive);
            }
            catch (Exception e) {

                logger.log(Level.WARNING,"Error executing script in jQueryLoad method");
                // no jQuery present
                return true;
            }
        }
    };
    return  jQLoad;
}

This method is working pretty solid for now since i know how many requests to expect. But as you have already noticed it can easily break in future as number of requests made are changed for some reason.

I been looking at cypress documentation and found this. According to cypress documentation this waits for the specified requests to be made.

cy.wait(['@getUsers', '@getActivities', '@getComments']).then((xhrs) => {

// xhrs will now be an array of matching XHR's
  // xhrs[0] <-- getUsers
  // xhrs[1] <-- getActivities
  // xhrs[2] <-- getComments
})

Is there any such method available in Selenium? or Is there any way this can be implemented? So far from what i have googled i got nothing. So any help will be appreciated.

Upvotes: 1

Views: 1679

Answers (1)

Swapnil Soni
Swapnil Soni

Reputation: 1049

You can locate Element and wait for element There are Implicit and Explicit waits in selenium.

You can use either

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

or

wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));

More information: on this answer

Upvotes: 1

Related Questions