Fluent Interface Method Chaining.: Homepage Loginpage Registrationpage
Fluent Interface Method Chaining.: Homepage Loginpage Registrationpage
Implementing OOPs concept in any automation framework involves dealing with lots of page classes
and objects. Most of the time, the test code required to implement these objects and methods
becomes very complex. This complexity can be minimised by using Fluent Interface and Method
Chaining.
Page Object Model is the most popular and widely used design pattern as it helps in code readability
and maintainability. And it can be more simplified and readable by using Fluent Page Object Model
as it uses method chaining.
In this design pattern, every action method in the page class returns this to implement chaining
methods for the business logic. This does not mean you cannot return other page class objects. You
can either return this (same class) or another page class too.
Method chaining is a technique where one method is called directly on another method forming a
chain-like structure.
Let us understand Fluent Page Object Design Pattern with the help of registration example
in http://automationpractice.com/index.php website
Step 1 Create HomePage, LoginPage, and RegistrationPage page classes with locators and action
methods.
Code snippet for RegistrationPage. Likewise you can create other pages.
WebDriver driver;
@FindBy(css = "div#uniform-id_gender2")
@FindBy(css = "input#customer_firstname")
@FindBy(css = "input#customer_lastname")
@FindBy(css = "input#passwd")
@FindBy(css = "input#firstname")
private WebElement addFirstName;
@FindBy(css = "input#lastname")
@FindBy(css = "input#city")
@FindBy(css = "input#postcode")
@FindBy(css = "button#submitAccount")
RegistrationPage(WebDriver driver) {
this.driver=driver;
PageFactory.initElements(driver, this);
mrsRadio.click();
return this;
firstName.sendKeys(first);
return this;
}
public RegistrationPage enterLastName(String last) {
lastName.sendKeys(last);
return this;
password.sendKeys(pass);
return this;
addFirstName.sendKeys(first);
return this;
addLastName.sendKeys(last);
return this;
city.sendKeys(cityName);
return this;
postcode.sendKeys(post);
return this;
RegistrationPage.using(driver)
.selectFemaleTitle()
.enterFirstName("Peter")
.enterLastName("John")
.enterPassword("Test@123")
.enterAddFirstName("Peter")
.enterAddLastName("John")
.enterCity("Los Angeles")
.enterCode("88205")
.clickRegister();
In the above test class, we are not creating objects of page classes to access the associated methods.
Instead, a single chain is created to call all the required methods of a page class in one call itself.
The fluent page objects significantly improve the readability of tests. Also, it is quite easy to write
tests with Fluent style.
Conclusion
As an automation tester, you might come across several problems in framework on a daily basis and
sometimes you might do a quick fix at test level to keep the test execution up and running. However,
this solution may not last for long. It is recommended that QAs should identify problems and try to
fix it at base level by creating common wrapper methods that can be used by multiple test classes.
You should also figure out if this problem can be solved by implementing proper design patterns and
try to implement it in the early stages of automation framework development. Constantly evaluating
the problems and updating the automation code/structure is the key for a successful automation
framework.
Hence, choosing and implementing the right Design Pattern which suits your test automation
requirements is the key to efficient testing.
Using BrowserStack Automate for web testing and App Automate for mobile app testing, you can
seamlessly integrate your Selenium, Cypress, Playwright, Puppeteer, Cucumber, and Appium tests to
make automation testing easier through your desired Design Pattern. Not just that, you can speed up
your tests by 30x with parallel testing to expand your test and browser coverage without
compromising on build times.