FluentWait with WebElement

My synchronisation strategies with WebDriver have generally revolved around the WebDriverWait and creating custom ExpectedCondition objects.

When I first saw the FluentWait, I dismissed it as a slightly more customisable version of the WebDriverWait.

But then I looked more closely at the signature of the constructor, and I noticed the Generics being used.

The use of Generics means I don’t have to pass in a WebDriver, I could use a WebElement, or a By, or anything I want really.

Which could prove really handy for Ajaxy apps.

I’ve knocked up this little code example using countdown timer at stuntsnippets. I uploaded the example page to this site at javascript_countdown

The code waits for the element to become visible using a WebDriverWait then drops down to a lower level of granularity to check that the seconds count down to 04

        WebElement countdown = driver.findElement(

        new WebDriverWait(driver,10).

        new FluentWait<WebElement>(countdown).
                withTimeout(10, TimeUnit.SECONDS).
                until(new Function<WebElement  , Boolean>() {
                    public Boolean apply(WebElement element) {
                        return element.getText().endsWith("04");

Note in the above code I tell FluentWait that it will be sending a <WebElement> through to the apply method.

FluentWait can take a Predicate, or a Function, as an argument in the until method.

I chose to use a Function, which can return any object, I chose to return a Boolean.

A predicate will only return a boolean.

FluentWait can prove pretty handy for Ajax because of the flexibility you have for ignoring exceptions and easily tweaking the wait and polling times.

You can learn about the differences between Function and Predicate from the guava-libraries documentation.

I have uploaded the code for this example to github, in the uploaded code I also have an example of using the Predicate.

This entry was posted in Selenium Simplified Blog, WebDriver. Bookmark the permalink.

3 Responses to FluentWait with WebElement

  1. Pingback: A Smattering of Selenium #114 « Official Selenium Blog

  2. Jon says:

    I wrote a Fluent page object pattern: https://github.com/djangofan/FluentPageObject

  3. Pingback: Add some ‘Weight’ to your Selenium Tests with ‘Waits’! – Part 3: Fluent Waits | James Willett – Technical Testing

Leave a Reply

Your email address will not be published. Required fields are marked *