How To Selenium: Page Objects – Partial Classes String PropertiesGo Back
- Posted by Ronan
This is the latest post as part of the Huddle Selenium series.
Each month on the first Tuesday of the month, we will post a new blog post to take you through a step-by-step guide on how to address a particular aspect of using Selenium as part of our How To series.
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.
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.
We will once again automate the main Bing page. All of the code is placed inside the BingMainPage class.
Page Objects using String Properties Code
BingMainPage Initial Version
BingMainPage 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.
BingMainPage.Map Initial Version
BingMainPage.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.
BingMainPage.Asserter Initial Version
As demonstrated, here we get the text in the element through the Text property of the element.
BingMainPage.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.
In future articles, I will share with you other modifications of the design pattern that can make your tests even more maintainable. You can find even more articles dedicated to the design patterns in automated testing on my site- Automate The Planet
Read the previous post in the series here.
About The Author:
I am Anton Angelov, a Quality Assurance Architect at Innovative Labs. I am passionate about automation testing and designing test harness and tools, having the best industry development practices in mind. Furthermore, I am the owner and Chief Editor of Automate The Planet. I am ardent about technologies such as C#, .NET Framework, T4, WPF, SQL Server, Selenium WebDriver, Jenkins. I won MVP status at Code Project (2016, 2017) and MVB (Most Valuable Blogger) at DZone. You can find even more information on my site.