FluentLenium is a test automation framework built on top of Selenium WebDriver to write readable, reusable, reliable and resilient UI functional tests.
It provides a wrapper to the selenium and does avoid common issues any selenium users face. This can include the passing of driver sessions between pages, automatic wait etc.
FluentLenium is 100% java based and supports adapters for JUnit4, JUnit5, TestNG, Spock and Cucumber.
Supports AsserJ seamlessly but you can use any assertions of your need.
Note: You can download the FluentLenium dependencies from Maven Repository. FluentLenium 4.x will be supported only with JDK 11 and 3.x supports Java 8.1. Make sure you use the right version of jars. In case if it is mismatched you will be thrown “java.lang.UnsupportedClassVersionError” exception
Now lets deep-dive into code
Step 1: Add the dependencies in pom.xml
<dependency>
<groupId>org.fluentlenium</groupId>
<artifactId>fluentlenium-junit</artifactId>
<version>3.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.fluentlenium</groupId>
<artifactId>fluentlenium-assertj</artifactId>
<version>3.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>2.34.0</version>
<scope>test</scope>
</dependency>
Step 2: Create a Java Class and add the below code
import static org.assertj.core.api.Assertions.assertThat;
import org.fluentlenium.adapter.junit.FluentTest;
import org.fluentlenium.core.hook.wait.Wait;
import org.junit.Test;
@Wait
public class TestClass extends FluentTest {
@Test
public void swagLabsTitleTest() {
goTo("https://www.saucedemo.com/");
$("#user-name").fill().with("standard_user");
$("#password").fill().with("secret_sauce");
$("#password").submit();
assertThat(window().title()).contains("Swag Labs");
}
}
Step 3: Run as JUnit Test
Note that the tests will NOT be visible as it will be executed in Headless Mode by default.
Page Object Model
FluentLenium supports Page Object Model at its best. All we need to do is use @Page annotation to import the page object class
PageObjectTestClass.java
import org.fluentlenium.adapter.junit.FluentTest;
import org.fluentlenium.core.annotation.Page;
import org.fluentlenium.core.hook.wait.Wait;
import org.junit.Test;
import com.fluentlenium.tests.pageObjects.LoginPage;
@Wait
public class PageObjectTestClass extends FluentTest {
@Page
LoginPage loginPage;
@Test
public void titleOfDuckDuckGoShouldContainSearchQueryName() {
goTo(loginPage).typeCredentials("standard_user","secret_sauce").submitLogin();
}
}
The POM for this example will be looking something like this – LoginPage.java
import org.fluentlenium.core.FluentPage;
import org.fluentlenium.core.annotation.PageUrl;
import org.fluentlenium.core.domain.FluentWebElement;
import org.openqa.selenium.support.FindBy;
@PageUrl("https://www.saucedemo.com/")
public class LoginPage extends FluentPage {
@FindBy(css = "#user-name")
private FluentWebElement username;
@FindBy(css = "#password")
private FluentWebElement password;
public LoginPage typeCredentials(String uname, String pwd) {
username.write(uname);
password.write(pwd);
return this;
}
public LoginPage submitLogin() {
password.submit();
return this;
}
}
The Page URL has to be given in the entry level page object class. The magic of @Wait will eventually provide the wait time for the tests.
The code for this example can be found in GitHub
Conclusion:
FluentLenium is an amazing Selenium Wrapper used to write tests faster and more reliable. With its headless mode, the tests are executed quicker and accurate. This is one such tool that can be used for Shift-Left approach in DevOps.
Leave a Reply