How to send ExpectedConditions to a function?


I want to write a generic function that gets ExpectedConditions and Locator:

public WebElement findElementElements(ExpectedConditions expectedConditions, By locator){
    WebDriver driver...
    WebDriverWait webDriverWait...

    return driver.webDriverWait().until(expectedConditions(locator));

But I get this compiler error: "Method call expected"


Not all ExpectedCondtions return the same type… some return WebElement, others return Boolean. This method expects ExpectedConditions that return WebElements.

public WebElement findElement(Function<WebDriver, WebElement> expectedCondition) {
    return new WebDriverWait(driver, 10).until(expectedCondition);

and you can call it like

WebElement e = findElement(ExpectedConditions.elementToBeClickable("myId")));

NOTE: You don’t need the locator parameter because that’s a required part of the ExpectedCondition.

Answered By – JeffC

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published