VpnT
VpnT

Reputation: 65

Can you create a generic or reusable method for explicit wait in Java and Selenium?

Can we create a generic or reusable method for explicit wait in Java and Selenium?

use expected conditions like , visibilityOfElementLocated,presenceOfElementLocated,elementToBeClickable

Any help is appreciated.

thanks

Upvotes: 3

Views: 3628

Answers (1)

Julian
Julian

Reputation: 1675

You totally can. I'm taking this from another answer I've written here: Selenium java SafariDriver wait for page to load after click

public ExpectedCondition<Boolean> myCustomCondition(/* some arguments from the outside */) {
return new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {
            // Check something on the page and return either true or false,
            // this method will be run repeatedly until either the time
            // limit is exceeded, or true is returned
        }

        public String toString() {
            return "a description of what this is waiting for";
        }
    };
}

Wrapping this in a function is the most common way in my experience. But the points to know here is that you want to obtain an instance of ExpectedCondition that provides an implementation for the apply() and toString() method.

apply() should return true only when the condition is satisfied.

Here is a simple real example:

public ExpectedCondition<Boolean> waitForElementToHaveText(final WebElement element, final String expectedText) {
    return new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {
            try {
                return element.getText().equals(expectedText);
            } catch (Exception e) {
                return false; // catchall fail case
            }
        }

        public String toString() {
            return "an element to have specific text";
        }
    };
}

This can be used like:

WebDriverWait wait = new WebDriverWait(driver, maxSecondsToWait);
wait.until(waitForElementToHaveText(anElement, "my visible text"));

Edit:

Some extra notes, if you notice that the declared generic type that ExpectedCondition uses is Boolean, that means that you want to return true or false to determine if the condition is met. But null also works as alternative to false. So another way you can do this is to declare your ExpectedCondition to have a generic type of WebElement and return null when the condition isn't met.

public static ExpectedCondition<WebElement> waitForElementToContainText(final String cssSelector, final String visibleText) {
    return new ExpectedCondition<WebElement>() {
        @Override
        public WebElement apply(WebDriver webDriver) {
            WebElement anElement = webDriver.findElement(By.cssSelector(cssSelector);
            if (anElement.getText().contains(visibleText)) {
                return anElement; // Condition passed
            }
            return null; // Condition failed
        }

        public String toString() {
            return "first occurance of text '" + visibleText + "' in element " + cssSelector;
        }
    };
}

Of course, either of these examples could use <Boolean> or <WebElement> and would just want to choose an appropriate return value for it.

Upvotes: 1

Related Questions