0

I'm looking for a way to write this block using lambda:

public void waitUntilElemIsDisabled(WebElement element) {
        try {
            wait.until(new Function<WebDriver, Boolean>() {
                @Override
                public Boolean apply(WebDriver driver) {
                    return !element.isEnabled();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
        }
    }

I was trying out wait.until(e -> element.isEnabled()); but I'm getting a syntax error:

The method until(Predicate<WebDriver>) is ambiguous for the type FluentWait<WebDriver>

If it helps, here's how I initialized wait:

wait = new FluentWait<>(webDriver).withTimeout(impTimeout, s).pollingEvery(pollingMsInt, ms)
                    .ignoring(NoSuchElementException.class);

I'm fairly new at using lambda, currently following this guide but I can't find a pattern that matches the one i'm using.

Thanks in advance.

iamkenos
  • 1,446
  • 8
  • 24
  • 49
  • 1
    It looks like you might be using the deprecated `until()` method. Take a look at this [somewhat related answer](http://stackoverflow.com/a/42421762/1183506) – mrfreester Mar 02 '17 at 15:51
  • I often use the IDE's refactoring ability as a substitute for using my brain to solve such things. In IntelliJ, I would click on the lambda, use Refactor/Extract to Value, correct the type parameters if needed, then use Refactor/Inline to integrate the value back in to the original expression. Usually, it will automatically add any needed casts or explicit type parameters. – Hank D Mar 02 '17 at 16:02

1 Answers1

0

Managed to work this out.

Solution:

private Function<WebDriver, Boolean> webElemBooleanFunction(boolean condition) {
    return x -> condition;
}

Invoke using:

public void waitUntilFieldIsPopulated(WebElement element) {
    try {
        wait.until(webElemBooleanFunction(getElementValue(element).length() > 0));
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.toString());
    }
}

Thanks for the tips!

iamkenos
  • 1,446
  • 8
  • 24
  • 49