Fluentlenium
My team have been wanting to improve our testing process for our front-end web code. One area we have been looking at is browser automation testing.
Although there have been new tools in this space (i.e. Puppeteer, Playwright, Cypress), it is still Selenium and its WebDriver that seem to dominate. Unfortunately, Selenium-based testing code is quite verbose, and the library can be hard to get started with. Thankfully, a library called Fluentlenium has been written to make things easier.
As the name suggests, Fluentlenium provides a fluent interface over the top of Selenium, creating a more intuitive API for interacting with the browser. There is built-in support for the PageObject pattern to help neaten test code structure, AssertJ support for fluent assertions, and an easy to use hook mechanism which can be used to automatically add await clauses to actions (i.e. when clicking a button, implicitly wait for it to be clickable). All these features make it significantly easier to write tests compared to regular Selenium.
There are many examples of how to use the library on the Fluentlenium website. However, most use Maven as the build tool whereas my team uses Gradle. The differences in setup aren’t too surprising, except for one gotcha.
Fluentlenium allows you to create factories for customising how the connection to a Selenium WebDriver instance is established.
@FactoryName("customWebDriver")
public class CustomWebDriverFactory implements WebDriverFactory {
@Override
public WebDriver newWebDriver(Capabilities desiredCapabilities, ConfigurationProperties configuration) {
return new HtmlUnitDriver(true);
}
}
However, for them to be found at runtime, you need to register Fluentlenium-core as a test annotation processor so that the @FactoryName
annotation is processed and some service descriptors generated.
dependencies {
testAnnotationProcessor("org.fluentlenium", "fluentlenium-core", "4.3.1")
testImplementation("org.junit.jupiter", "junit-jupiter-api", "5.6.2")
testImplementation("org.fluentlenium", "fluentlenium-junit-jupiter", "4.3.1")
testImplementation("org.fluentlenium", "fluentlenium-assertj", "4.3.1")
testImplementation("org.seleniumhq.selenium", "htmlunit-driver", "2.34.0")
}