Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not understand the implementation of until() method in FluentWait

The until() method in org.openqa.selenium.support.ui.FluentWait is overloaded for Predicate<T> and Function<? super T, V> interfaces as parameters.

It should take as argument one of the following (which implement the apply() method):

  • Anonymous class
  • Lambda expression
  • Method reference

Any lambda that I define as argument for this method throws following error:

The method until(Predicate) is ambiguous for the type WebDriverWait

My Lambda:

x -> x.findElement(byLocator).isDisplayed()

I assume this will be the case for any lambda, as apply() for either Function or Predicate can be implemented by use of these lambda.

So my question is what is the use of the until method which takes Predicate as parameter?

Updated: Removed the first part of question which was answered by @drkthng.

like image 872
Husam Avatar asked Oct 30 '22 23:10

Husam


1 Answers

Answering your first question:

Have a look at the Selenium API docs for ExpectedConditon.

I quote:

public interface ExpectedCondition<T>
extends com.google.common.base.Function<WebDriver,T>

You see, that ExpectedCondition inherited from google's Function interface, thus you can use it as an argument for the until() method.

As for your second question:

I don't think you can hand over a lambda just like that. Until method is waiting for a Predicate or Function (as you correctly mentioned).

Regarding the differences between Predicates and lambdas, have a look for example here

So you can try something like this instead, still using your lambda-expressions:

Predicate<WebDriver> myPredicate = x ->x.findElement(By.id("id")).isDisplayed();

WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver, 10);
wait.until(myPredicate);
like image 56
drkthng Avatar answered Nov 09 '22 06:11

drkthng