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 fifth article from the WebDriver Page Objects Series. It is dedicated to page objects using partial classes and exposing elements as string properties. 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. The primary difference compared to the other versions of the pattern will be that here we will not expose the whole interface of the elements. Instead, we will use them as string properties, simplifying the API.

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. All of the code is placed inside the SearchEngineMainPage class.

Page Objects using String Properties Code

SearchEngineMainPage Initial Version

SearchEngineMainPage String Properties’ Version

The difference here is that we do not call the SendKeys method anymore. Instead, we assign the value directly to the element’s property.

SearchEngineMainPage.Map Initial Version

SearchEngineMainPage.Map String Properties Version

As you can see we wrap the nitty gritty details of the WebDriver’s API here directly in the properties. Then return only the required information instead of the whole IWebElement object. This way the users of your pages/API will not be able to shoot themselves in the foot.

SearchEngineMainPage.Asserter Initial Version

As demonstrated, here we get the text in the element through the Text property of the element.

SearchEngineMainPage.Asserter String Properties Version

The primary difference is that we access the inner text of the element without the call to the Text property.

Elements’ String Properties in Tests

You can create service methods for most common operations as we did with the Search method. However, you can use the elements’ string properties directly as shown in the example tests.