Editorial Note: I originally wrote this post for the T﻿﻿est H﻿﻿﻿﻿﻿u﻿﻿﻿ddle Blog. You can check out the original here, at their site.

This is the fourth article from the WebDriver Page Objects Series. It is dedicated to page objects using partial classes and the so-called fluent API. In the previous articles from the series, I showed you how to create more maintainable page objects through separating the code of the pages in three different files. Moreover, you are no more obligated to use the Selenium.Support NuGet package. Here we will create a different way for accessing the methods of the page in a single uninterrupted chain.

If you are using WebDriver often, you may find useful my Most Complete Selenium WebDriver C# Cheat Sheet. All you need to know- the most basic operations to the most advanced configurations.

Definition In software engineering, a fluent interface is a method for constructing object-oriented APIs, where the readability of the source code is close to that of ordinary written prose. A fluent interface is usually implemented by using method cascading to relay the instruction context of a subsequent call.

Two years ago while we were working on the first version of the BELLATRIX test automation framework, I did this research so that we can find the most convenient way for creating page objects.

Test Case We will once again automate the main SearchEngine page. However, this time we will write logic for using the advanced images’ filtering options.

Page Objects using Fluent API Code For each filter option, we have a dedicated enum- Colors, Dates, Layouts, Licenses, People, Sizes and Types.

We will use these enums in the primary class of our page. The code of the rest of the enums is identical. SearchEngineMainPage Most of the differences compared to the other implementations of the pattern are located in this file.

Everything stays the same with the difference that each service method now returns the instance of the page itself. This way the fluent syntax is supported. SearchEngineMainPage.Map

The map does not contain any differences compared to the other versions. Here, the file contains the various elements present in the advanced filtering menu. SearchEngineMainPage.Asserter

To support the fluent API, the assert method returns the instance of the page. Fluent API in Tests