user1860934
user1860934

Reputation: 427

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"

Upvotes: 0

Views: 123

Answers (1)

JeffC
JeffC

Reputation: 25542

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(By.id("myId")));

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

Upvotes: 1

Related Questions