Earlier versions of WebDriver used a
FluentWait class, the
WebDriverWait now offers a fluent DSL approach for configuring the wait.
My synchronisation strategies with WebDriver have generally revolved around the WebDriverWait and creating custom ExpectedCondition objects.
A traditional basic use of
WebDriverWait uses minimal customisation, because most of the defaults are good enough for our use.
WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions. elementToBeClickable( By.id("htmlformtest"))); driver.findElement(By.id("htmlformtest")).click();
A basic wait, is already fluent and we can chain the methods:
new WebDriverWait(driver, 10) .until(ExpectedConditions.elementToBeClickable( By.id("htmlformtest"))) .click();
This doesn’t mean that the code is easier to read or maintain, because it might be better to re-use the wait later in the test.
But the fluent interface allows us to use code completion to build it up.
The fluent interface goes beyond the simple use case allowing us to configure the wait.
WebDriverWait extends a
FluentWait can be useful for waiting on ‘anything’ and we cover that in our Synchronisation Course.
Allowing us to chain methods to configure
withTimeouthow long before timeout
withMessagethe message to display when it timesout
pollingEveryhow long between polling intervals
ignoringwhich exceptions to ignore during the polling
All of the above methods can be ‘chained’ to create a fluent configuration of a
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
The additional configuration offered by Fluent configuration of a
WebDriverWait can prove pretty handy for Ajax because of the flexibility you have for ignoring exceptions and easily tweaking the wait and polling times.
The full source for this is in my Webdriver Java FAQs project:
If you want to learn how to use Selenium WebDriver with Java then check out our online courses. And we have a whole course on effective synchronisation strategies.
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.