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.

https://gist.github.com/angelovstanton/e5bfec4b8ea8e540c4a2780d4979bf2b

Info

BingMainPage C# 7

https://gist.github.com/angelovstanton/8d8b82037d0cf633e4a91f357f24fd9c

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

https://gist.github.com/angelovstanton/1dcfa1918cee115ed0c523faa0592b6b

 

BingMainPage.Map C# 7

 

https://gist.github.com/angelovstanton/a01fd5f2159adc274866a02243a83174

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.

Info

BingMainPage.Asserter Prior C# 7

 https://gist.github.com/angelovstanton/870b81106102b8d3af6e4883e05ed290

BingMainPage.Asserter C# 7

 

https://gist.github.com/angelovstanton/a713960f0448c1f96c68ae2035e792c7

 

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