How does FluentWait in Selenium implements the until() method

喜夏-厌秋 提交于 2021-02-08 10:14:56

问题


The syntax of the until() method in selenium docs is as below:

public <V> V until(java.util.function.Function<? super T,V> isTrue)

The usage of the same is like:

WebDriver wait = new WebDriver(driver, 20);
WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("lgn-btn")));

I couldn't relate to the syntax and the usage of the until() method. I want to know how the syntax is implemented.

Yeah, I know about Generics, which we use to know about the errors at compile time so that we can avoid ClassCastException at runtime. Also, I know about the functional interface, which we use to achieve behavior parameterization.

What I didn't get is the equivalence between the java.util.function.Function<? super T,V> isTrue) and the ExpectedConditions.elementToBeClickable(By.id("id)).

What does the expression java.util.function.Function<? super T,V> isTruemean?


回答1:


There are four different topics mentioned within your question and you can find the details below:

java.util.function

The java.util.function package includes the Functional interfaces which provide the target types for lambda expressions and method references.

A few examples are:

  • BiConsumer<T,U>: Represents an operation that accepts two input arguments and returns no result.
  • BiFunction<T,U,R>: Represents a function that accepts two arguments and produces a result.
  • BinaryOperator<T>: Represents an operation upon two operands of the same type, producing a result of the same type as the operands.
  • BiPredicate<T,U>: Represents a predicate (boolean-valued function) of two arguments.
  • Consumer<T>: Represents an operation that accepts a single input argument and returns no result.
  • Function<T,R>: Represents a function that accepts one argument and produces a result.

Class FluentWait

The public class FluentWait<T> class extends java.lang.Object and implements Wait<T> which implies it is an implementation of the Wait interface that may have its timeout and polling interval configured on the fly. Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

One of the modifier is:

Modifier and Type       Method and Description
-----------------       ----------------------
<V> V                   until(java.util.function.Function<? super T,V> isTrue)

Specified by:
    until in interface Wait<T>
Type Parameters:
    V - The function's expected return type.
Parameters:
    isTrue - the parameter to pass to the ExpectedCondition
Returns:
    The function's return value if the function returned something different from null or false before the timeout expired.
Throws:
    TimeoutException - If the timeout expires.

This implementation repeatedly applies this instance's input value to the given function until one of the following occurs:

  • The function returns neither null nor false
  • The function throws an unignored exception
  • The timeout expires
  • The current thread is interrupted

Interface ExpectedCondition

The public interface ExpectedCondition<T> interface extends com.google.common.base.Function<WebDriver,T> which models a condition that is expected to evaluate to something that is neither null nor false. Examples would include determining if a web page has loaded or that an element is visible.

Note that it is expected that ExpectedConditions are idempotent. They will be called in a loop by the WebDriverWait and any modification of the state of the application under test may have unexpected side-effects.


Class ExpectedConditions

The ExpectedConditions Class is the canned ExpectedConditions which are generally useful within webdriver tests.

A few usage examples:

  • elementToBeClickable():

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("element_css")));
    
  • visibilityOfElementLocated():

    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("element_css")));
    
  • frameToBeAvailableAndSwitchToIt():

    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("element_css")));
    
  • visibilityOfAllElementsLocatedBy():

    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("element_css")));
    
  • attributeContains():

    new WebDriverWait(driver, 20).until(ExpectedConditions.attributeContains(driver.findElement(my_element), "src", "https"));
    


来源:https://stackoverflow.com/questions/54555243/how-does-fluentwait-in-selenium-implements-the-until-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!