No description, website, or topics provided.
Latest commit d674726 Mar 11, 2017 @paul-hammant paul-hammant another upgrade
Failed to load latest commit information.
java another upgrade Mar 10, 2017
.gitignore tbody and nav added Sep 12, 2013
COPYING add copyright headers Aug 19, 2011 oops Feb 22, 2017


FluentSelenium is a layer on top of Selenium 2.0 (WebDriver) that adds a fluent interface style for working with the browser. For now, it is for the Java binding to WebDriver.

Basic Use

HTML elements have Java methods that are named for them. Locators are optional, and are from WebDriver's regular set:

WebDriver wd = new FirefoxDriver();
FluentWebDriver fwd = new FluentWebDriver(wd);


fwd.span(id("results")).getText().shouldBe("1 result");

Hyperlinks are marked as 'a' (anchor) in HTML, but we have represented those as link() in the fluent API.

As with all fluent interfaces, there is no point looking at strict API documentation (JavaDoc for Java), and you're better looking at example code, and this page is it (perhaps the blog entries of others too).

Situations where the DOM is changing slowly


There's a "within" capability in the fluent language. It will keep retrying a locator for a given period of time. The fluent expression (the locator) is given a chance to get past a slowly appearing element in the page:



This will throw an exception after the elapsed time, if the element still hasn't appeared in the page's DOM.


The opposite of "within", the "without" capability is going to wait for something to disappear. If the element is still in the page, it will keep waiting upto a given period of time, for it to disappear:

// No button in the div after 5 seconds:

The element disappearing in the page means that the fluent expression stops there. Also, disappear means that the locator used to find the element does not find it. Thus the following does not mean that there is no span element, it just means that there is no span element with a class of "baz":


This will throw an exception after the elapsed time, if it still hasn't disappeared from the page's DOM.

Selenium 1.0 had an API function isElementPresent. The 'without' functionality is akin to isElementNotPresent, or rather waitForElementToNotBePresent.

Elements in the DOM, but not visible 'yet'

Sometimes elements are within the DOM, but they are invisible for a period of time after an action of some sort. You can wait for elements to become visible, before fluently progressing:

fwd.input(id("textArea")).sendKeys("Mary Had A Little Lamb...");

Locators for Advanced JavaScript Frameworks

AngularJS 1.x

AngularJS is an example of framework that does a huge amount of the heavy lifting in browser. While it's doing its magic, you are going to encounter timing issues. If you prefer, the 'within' and 'without' fluent methods above will help you overcome those issues, but there is a way of being smarter about waiting for Angular's magic to stop:

There's another library you can use in conjunction with Selenium/WebDriver and/or FluentSelenium called ngWebDriver that makes it far easier to test Angular applications.

Other frameworks

Backbone, Knockout (etc) may have similar tricks, that you can use 'executeScript' to invoke, but we've not done the research to hook into them.

Stale Elements

WebDriver, by default, does not handle findElement traversals from elements that have gone stale transparently. It prefers to throw StaleElementReferenceException, which you have to catch and then do something with. Retry is one option. FluentSelenium has retry capability:

new RetryAfterStaleElement() {
    public void toRetry() {

In this example, the element can go stale any amount of times in eight seconds, and the whole traversal is restarted again and again. If you're trying to store values, you'll have a problem with Java's inner-class rules, and have to use member fields or do dirty tricks like:

final String selectedFlight[] = new String[1];
new RetryAfterStaleElement() {
    public void toRetry() {
        selectedFlight[0] = div(className("fromto-column")).getText().toString();

Use of the one element array is the dirty trick, because of the need for final.

FluentSelenium can recover from a subset of StaleElementReferenceException situations. If the item going stale is the one that is leaf-most in your fluent expression, then it can be recovered automatically (and silently). This is a one-time deal though - if it persistent in its staleness after recovery, then the exception is throw. Recovery means finding it again in the DOM, relative to its parent with the same locator. In the case above, the "fromto-column" div being stale can be recovered automatically - even during the getText(). The "thirdAddress" div cannot be, at least when execution has transferred to the next div().

Built-in Assertions

String Assertions

Many things return a string (actually a TestableString). Some elements of a page are designed to have a string representation. Input fields and spans are obvious, but any element supports getText() and WebDriver will try to make a chunk of text that represents that (often with carriage returns).

fwd.div(id("foo")).getText().shouldBe("1 bar");
fwd.div(id("foo")).getText().shouldNotBe("0 bars");

Text Changers

The getText() method can also take one or more TextChanger implementations now. These can change the value of getText() before handing it rightwards to an assertion, like so:

fwd.div(id("foo")).getText(new MyToUppperCase()).shouldBe("1 BAR");

There are supplied ones too: multiSpaceEliminator(), trimmer(), tabsToSpaces() and crToChars("|")

There is also a Concatenator that is available for getText() where that was implicitly a findElements (plural). There is one supplied concatenator, delimitWithChars(..) used like so:



Regex is possible too, and it will ignore carriage returns (which Java pre-processes like so \n -> \\n)

fwd.div(id("foo")).getText().shouldMatch("\d bar");
fwd.div(id("foo")).getText().shouldMatch("[1-9] bar");
fwd.div(id("formErrors")).getText().shouldNotMatch("\d errors");

Hamcrest mactchers

Hamcrest mactchers, similarly:

fwd.div(id("foo")).getText().shouldMatch(IsEqual<String>("1 bar"));

Within a period of time

As shown above, you can transparently wait for the thing to become true (within/without to the right of the TestableString, and the shouldXxx rightmost):

fwd.div(id("foo")).getText().within(secs(10)).shouldBe("1 bar");
// text is '1 bar' to start, but within 10 secs is not:
fwd.div(id("foo")).getText().without(secs(10)).shouldBe("1 bar");

The assertion is retried for the advised period.

Changing text before assertions

Sometimes FluentWebWlement TODO

Non-String Assertions

Any element has a location via getLocation(), which yields a Point Any element has a size via getSize(), which yields a Dimension Some elements have boolean from isDisplayed(), isEnabled() and isSelected()

All of those have assertions:

fwd.div(id("foo")).getLocation().shouldBe(new Point(1, 1));
fwd.div(id("foo")).getLocation().shouldNotBe(new Point(1, 1));

fwd.div(id("foo")).getSize().shouldBe(new Dimension(640, 480));
fwd.div(id("foo")).getSize().shouldNotBe(new Dimension(640, 480));


Like for Strings, you can transparently wait for the thing to become true:


The assertion is retried for the advised period.

Locating Elements

WebDriver's own "By" locator mechanism is what is used. Here are examples using that:

by ="id")
by = By.className("name")
by = By.tagName("table")

Class FluentBy adds a few more:

by = FluentBy.attribute("ng-model")
by = FluentBy.attribute("ng-model", "shopperSelection.payPalPreferred") {
by = FluentBy.composite(tagName("table"), className("paymentType"))
by = FluentBy.composite(tagName("table"), attribute("ng-click")) {

One more strictClassName is used like so:

by = FluentBy.strictClassName("name")

Strict is where there is only one class for that element. The built-in WebDriver one allows for many classes for an element, with the one specified amongst them.

If an locator cannot find the element in the DOM, then an exception - 'FluentExecutionStopped' - is thrown (see below).

Multiple elements

Just like WebDriver, FluentSelenium can return a collection of Elements matching a locator:

FluentWebElements elems = fwd.div(id("foo")).div(className("bar")).buttons();
elems = fwd.div(id("foo")).divs(className("bar"));
elems = fwd.divs(id("foo");

Look at the pluralization of the methods above, and that it only makes sense if it's the last in the fluent expression.

Fluently matching/filtering over multiple elements

Use a FluentMatcher instance (which is just a predicate)

class MyIntricateFluentMatcher implements FluentMatcher {
  public boolean matches(FluentWebElement webElement, int ix) {
    // do what you like here as long as it return true/false.

FluentMatcher fm = new MyIntricateFluentMatcher();
// click on first matching one...

// click on last matching one...

// click on all matching matching ones...
listofMatching elements = fwd.inputs(className("bar").filter(fm) // click them all

There are no instances of FluentMatcher built in, other than CompositeFluentMatcher which allows you to build up a larger matcher, and has 'both', 'any', 'all', 'either' functionality. There's also 'and' & 'or' cabailities to CompositeFluentMatcher.

Visit each element to do something custom

class MyFluentWebElementVistor implements FluentWebElementVistor {
  public void visit(FluentWebElement webElement, int ix) {
    // do what you like here

FluentWebElementVistor v = new MyFluentWebElementVistor();
// do something on each element in a list, then click on them

Make a map from the matching elements

class MyFluentWebElementMap<String,String> implements FluentWebElementMap<String,String> {
  public void map(FluentWebElement webElement, int ix) {
    // note: <String,String> is only an example
    String key == webElement. // something
    String value == webElement. // something

MyFluentWebElementMap m = new MyFluentWebElementMap();
Map<String,String> myMap = fwd.inputs(className("bar").map(m);
// map() effectively stops fluency, here.


Obviously you want tests using FluentSelenium to pass. Getting tests to be stable has also been a historical challenge for the Selenium world, but a real failure of previously working test, is worth debugging (before or after a developer commit that may have broken the build).

Fluent-Selenium throws 'FluentExecutionStopped' like so:

"WebDriver exception during invocation of : ?.div(By.className: item-treasury-info-box')).h3()"

That exception's getCause() will be the WebDriverException derivative that happened during the h3() invocation - implicitly before any subsequent operation like click(). That could well be 'NoSuchElementException' for when an element was not found.

Alternate boolean handling of missing elements.

Normal operation is for FluentSelenium to throw 'FluentExecutionStopped' wrapping WebDriver's 'NoSuchElementException' for the root cause.

With 'has()' and 'hasMissing()' you can receive true/false instead of getting exceptions like so:

boolean isMissing = fwd.hasMissing().div(id("foo"))
boolean isPresent = fwd.has().div(id("foo"))

As mentioned before, Selenium 1.0 had an API function called 'isElementPresent'. With FluentSelenium we're getting close to that again, as 'has' and 'hasMissing' preceding a thing that should or should not be there, are functionally equivalent.


Fluent Selenium can generate monitors failing interactions with the browser. It can also see what fluent operation were started/ended. Refer the Monitor interface.

You specify a monitor choice by using the right constructor for FluentWebDriver (and pass in a Monitor instance). There's a default monitor that does nothing, so you don't have to choose a constructor that uses a monitor.

We have three implementations presently, and if you want to use more than one, wrap them in a CompositeMonitor:

new FluentWebDriver(new FirefoxDriver(), new CompositeMonitor(one, two, three));

Takes a Screenshot (on error)

When a 'FluentExecutionStopped' failure happens, you can get automatic screenshots. In the case of running from JUnit or TestNG under Maven control do the following, to get automatic Test-Class name & Method name in the file-name of the PNG:

ffd = new FirefoxDriver();
myScreenShotOnError = new ScreenShotOnError.WithUnitTestFrameWorkContext(ffd, OneOfYourClasses.class, "test-classes", "surefire-reports/");
fwd = new FluentWebDriver(ffd, myScreenShotOnError);

If you're not wanting that JUnit/TestNG automatic file naming, do this instead:

ffd = new FirefoxDriver();
myScreenShotOnError = new ScreenShotOnError(ffd, OneOfYourClasses.class, "test-classes", "surefire-reports/");
fwd = new FluentWebDriver(ffd, myScreenShotOnError);


Highlights on error

This draws a red dotted two-pixel line around the relevant part of the page, when an FluentExecutionStopped is thrown. You'd use it in conjunction with ScreenShotOnError above:

ffd = new FirefoxDriver();
myScreenShotOnError = ...
fwd = new FluentWebDriver(ffd, new CompositeMonitor(new HighlightOnError(ffd), myScreenShotOnError));

If you don't want a red dashed two-pixel line, subclass HighlightOnError and override one of executeScript(), highlightOperation() or highlightValue().

Coda Hale's Metrics library

Also shown here is how to hook that up to a JUnit4 suite running under Maven.

The separate listener class:

public class MyRunListener extends RunListener {
    public static final CodaHaleMetricsMonitor codahaleMetricsMonitor = new CodaHaleMetricsMonitor("com.paulhammant.fluentSeleniumExamples.");
    public void testRunFinished(Result result) throws Exception {
        final ConsoleReporter reporter = ConsoleReporter.forRegistry(codahaleMetricsMonitor.getMetrics())
	// More likely, you'd send stats to Graphite (etc).

Hooking that into each/all FluentWebDriver usages:

public class Home extends FluentWebDriver {
    public Home(WebDriver delegate) {
        super(delegate, MyRunListener.codahaleMetricsMonitor);
	// etc

// or the more conventional non inner-class style:

fwd = new FluentWebDriver(webDriver, MyRunListener.codahaleMetricsMonitor);

And in Maven's pom.xml:


This will spit our stats in the log like so, that require some interpretation:

package.MyClass.aMethod:div(By.className: aClassName)
             count = 2
         mean rate = 0.00 calls/millisecond
     1-minute rate = 0.00 calls/millisecond
     5-minute rate = 0.00 calls/millisecond
    15-minute rate = 0.00 calls/millisecond
               min = 31.95 milliseconds
               max = 36.66 milliseconds
              mean = 34.31 milliseconds
            stddev = 3.33 milliseconds
            median = 34.31 milliseconds
              75% <= 36.66 milliseconds
              95% <= 36.66 milliseconds
              98% <= 36.66 milliseconds
              99% <= 36.66 milliseconds
            99.9% <= 36.66 milliseconds

There's a fuller example of stats in the 'Fluent Selenium Examples' project

Coda Hale's Metrics library has other reporters you could attach.

Including it in your project



<!-- you need to choose a hamcrest version that works for you too -->

<!-- If you're needing Coda Hale's Metrics integration (optional) -->

Bear in mind that the FluentSelenium maven module has a transitive dependency on Selenium 3.x. You may want to override the version for your project. You'll need an exclusion for FluentSelenium, and an explicit dependency for Selenium 3.x. ...



Also be aware that Selenium depends on Jetty. If you are too in your prod code, you may need to exclude the Selenium's choice of Jetty (v9.2.15.v20160210 - see below), and include your own instead. Jetty v9.4.0.v20161208 is where the Eclipse foundation are at, and v9.2.x is some way behind with incompatible enough methods.


For non-Maven build systems, download it yourself.

Here's what else you might need in your classpath, depending on your needs:

+- junit:junit:jar:4.12:test
+- org.hamcrest:hamcrest-all:jar:1.3:compile
+- org.mockito:mockito-core:jar:1.10.19:test
|  +- org.hamcrest:hamcrest-core:jar:1.1:test
|  \- org.objenesis:objenesis:jar:2.1:test
+- org.seleniumhq.selenium:selenium-java:jar:3.0.1:compile
|  +- org.seleniumhq.selenium:selenium-chrome-driver:jar:3.0.1:compile
|  |  \- org.seleniumhq.selenium:selenium-remote-driver:jar:3.0.1:compile
|  |     +- org.seleniumhq.selenium:selenium-api:jar:3.0.1:compile
|  |     +- cglib:cglib-nodep:jar:3.2.4:compile
|  |     +- org.apache.commons:commons-exec:jar:1.3:compile
|  |     +-
|  |     +-
|  |     \-
|  |        \-
|  +- org.seleniumhq.selenium:selenium-edge-driver:jar:3.0.1:compile
|  +- org.seleniumhq.selenium:selenium-firefox-driver:jar:3.0.1:compile
|  +- org.seleniumhq.selenium:selenium-ie-driver:jar:3.0.1:compile
|  +- org.seleniumhq.selenium:selenium-opera-driver:jar:3.0.1:compile
|  +- org.seleniumhq.selenium:selenium-safari-driver:jar:3.0.1:compile
|  |  \- io.netty:netty:jar:3.5.7.Final:compile
|  +- org.seleniumhq.selenium:selenium-support:jar:3.0.1:compile
|  +- net.sourceforge.htmlunit:htmlunit:jar:2.23:compile
|  |  +- xalan:xalan:jar:2.7.2:compile
|  |  |  \- xalan:serializer:jar:2.7.2:compile
|  |  +- org.apache.commons:commons-lang3:jar:3.4:compile
|  |  +- org.apache.httpcomponents:httpclient:jar:4.5.2:compile
|  |  |  \- org.apache.httpcomponents:httpcore:jar:4.4.4:compile
|  |  +- org.apache.httpcomponents:httpmime:jar:4.5.2:compile
|  |  +- commons-codec:commons-codec:jar:1.10:compile
|  |  +- net.sourceforge.htmlunit:htmlunit-core-js:jar:2.23:compile
|  |  +- net.sourceforge.htmlunit:neko-htmlunit:jar:2.23:compile
|  |  |  \- xerces:xercesImpl:jar:2.11.0:compile
|  |  |     \- xml-apis:xml-apis:jar:1.4.01:compile
|  |  +- net.sourceforge.cssparser:cssparser:jar:0.9.20:compile
|  |  |  \- org.w3c.css:sac:jar:1.3:compile
|  |  +- commons-io:commons-io:jar:2.5:compile
|  |  \- commons-logging:commons-logging:jar:1.2:compile
|  +- com.codeborne:phantomjsdriver:jar:1.3.0:compile
|  \- org.eclipse.jetty.websocket:websocket-client:jar:9.2.15.v20160210:compile
|     +- org.eclipse.jetty:jetty-util:jar:9.2.15.v20160210:compile
|     +- org.eclipse.jetty:jetty-io:jar:9.2.15.v20160210:compile
|     \- org.eclipse.jetty.websocket:websocket-common:jar:9.2.15.v20160210:compile
|        \- org.eclipse.jetty.websocket:websocket-api:jar:9.2.15.v20160210:compile
\- com.codahale.metrics:metrics-core:jar:3.0.2:compile
   \- org.slf4j:slf4j-api:jar:1.7.5:compile


1.17 (Dec 20, 2016)

  • Selenium upgrade to v3.0.1
  • Support for 'body' element
  • New TestableString method shouldMatch(hamcrestMatcher) in addition to the same method that took a regex previously.
  • FluentWebElement getText() can take a varargs of 'TextChanger' now
  • FluentWebElements getText() can too, but also a means to control the between elements chars (CR by default)

1.16.1 (May 22, 2016)

  • Selenium upgrade to v2.53.0 - incl. the new getRect() from WebElement
  • Support for h5 and h6

1.16 (Nov 29, 2015)

  • map function and visitor added

1.15 (Nov 21, 2015)

  • Selenium upgrade to v2.48.2
  • Support for unordered lists (ul elements)
  • FluentWebDriver.element(..) method for finding generic elements (or ones outside the HTML spec)

More Reading

Refer Paul Hammant's Fluent Selenium Examples Blog Entry about this, or the project that showcases Fluent Selenium - Fluent Selenium Examples.