Skip to main content

Aug 22, 2012 - 2 minute read - Selenium Simplified Blog WebDriver

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 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.

You will need a Github account to comment. Or you can contact me with your comment.

I reserve the right to delete spam comments e.g. if your comment adds no value and its purpose is simply to create a backlink to another site offering training, or courses, etc.