fujiwara_tofu
fujiwara_tofu

Reputation: 39

How do I return boolean value from ExpectedCondition objects

I do have a method that waits for the JavaScript to load in the browser. From Selenium 3 (3.141.59), I have shifted to Selenium 4 (4.0.0-alpha-7)

This return code/statement doesnt work with Selenium 4

return wait.until(jQueryLoad) && wait.until(jsLoad);

What would be the correct return statement for this? I have tried several options but nothing worked. Please see the code structure for the method/function below. Your thoughts, ideas and answers will be highly appreaciated.

    public static boolean waitForJStoLoad() {
    WebDriverWait wait = new WebDriverWait(getDriver(), Duration.ofSeconds(30));
    // wait for jQuery to load
    ExpectedCondition<Boolean> jQueryLoad = new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver arg0) {
            try {
                Long state = (Long) ((JavascriptExecutor) arg0).executeScript("return jQuery.active");
                return (state == 0);
            } catch (Exception e) {
                return true;
            }
        }
    };
    // wait for Javascript to load
    ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver arg0) {
            String state = (String) ((JavascriptExecutor) arg0).executeScript("return document.readyState;");
            return state.equalsIgnoreCase("complete");
        }
    };
    
    
    return wait.until(jQueryLoad) && wait.until(jsLoad);
    
}

Upvotes: 0

Views: 1288

Answers (1)

Prophet
Prophet

Reputation: 33361

Well, I'm using standard Selenium 3 and not checking JavaScripts, but I do have several simple methods validating some conditions with the Expected Conditons and returning Boolean.
Something like this:

public boolean waitForElementToDisappear(By element){
    try {
        wait.until((ExpectedConditions.invisibilityOfElementLocated(element)));
        return true;
    }catch (Throwable t){
        return false;
    }
}

Upvotes: 1

Related Questions