1

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.

Jonah
  • 614
  • 7
  • 18

1 Answers1

1

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

Swapnil Soni
  • 965
  • 1
  • 10
  • 26
  • Yes that is always my first choice. But in my case sometimes those elements are already present and after request they are updated, and some other issues. So it's essential in my case to wait for specific request to be made and loaded. Wait for element fails in this case. – Muhammad Hamza Yousuf Sep 17 '20 at 08:06
  • 1
    in that case you can automate by capturing request and doing with your favorite language's http module. – Swapnil Soni Sep 17 '20 at 13:11