0

I am trying to use FluentWait in my Selenium Java automation.

When I used the until method, I get the following error the method until(Function in the type Wait is not applicable for the argument(new Function(){}

public WebElement fluentWaitForElement() {
    Wait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)  
        .withTimeout(30, TimeUnit.SECONDS)  
        .pollingEvery(5, TimeUnit.SECONDS) 
        .ignoring(NoSuchElementException.class);
    WebElement waitingElement = fluentWait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
    return driver.findElement(By.id("ButtonID"));
    }
   }
 );
}

I also tried the solution in another post. But, I still get the same error

fluentWaitwait.until(new Function<WebDriver, Boolean>() {
    @Override public Boolean apply(WebDriver driver) {
        return driver.findElement(By.ID("ButtonID"));
        }
    }
);

Does anyone know how to solve this problem? Thanks

Fenio
  • 3,528
  • 1
  • 13
  • 27
BAST23
  • 75
  • 5

1 Answers1

0

You really don't have to use new Function<WebDriver, Boolean>().

You can either use ExpectedCondtion<T> class which extends Function<WebDriver, T>; or use lambda expression.

Expected Condition:

.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driver) {
                //return Boolean value
                return null;
            }

        });

Lambda Expression:

.until(driver -> {
    return null; 
//return any type parameter you want. Lambda Expression takes care of what type you return
}

Additional "flaw" in your code is:

Wait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)  
        .withTimeout(30, TimeUnit.SECONDS)  
        .pollingEvery(5, TimeUnit.SECONDS) 
        .ignoring(NoSuchElementException.class);

Wait cannot be parametrized with WebDriver as it's not generic. Change it to FluentWait<WebDriver>

Fenio
  • 3,528
  • 1
  • 13
  • 27