How To Selenium: More Readable Page Objects – Partial Classes C# 7 Features

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 third article from the WebDriver Page Objects Series. Read the first and second posts of the series. It is dedicated to enhancing page objects with partial classes using the new C# 7.

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 I will enhance the code from the last article to make it even more readable though the new features of C#.

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.


BingMainPage C# 7

First, we use the new body expression syntax for both constructors. Moreover, the second one uses the ?? operator and validates the wait parameter if it is null or not. The third change is that the Navigate method is also converted as a single-line lambda expression.

BingMainPage.Map Prior C# 7


BingMainPage.Map C# 7

Now the elements span over a single line of code instead of seven! Even more, we use the getters and setters new lambda syntax in the last property to wrap the element type’s action.


BingMainPage.Asserter Prior C# 7

BingMainPage.Asserter C# 7


Now the assert method takes only a single line instead of three!

Download Source Code

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.


See more articles in the How To Selenium Series


About The Author:

anton aangelov-august-2017-version2I 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.

About the Author

Ronan Healy

Hi everyone. I'm part of the EuroSTAR team. I'm here to help you engage with the EuroSTAR Huddle Community and get the best out of your membership. Together with software testing experts, we have a range of webinars and eBooks for you to enjoy and we have lots of opportunities for you to come together online. If you have any thoughts about the community, please get in contact with me.
Find out more about @ronan