How To Selenium: Page Objects with Partial Classes and Properties

Reading Time: 2 minutes

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 second article from the WebDriver Page Objects Series. It is dedicated to creating page objects with partial classes without the Selenium.Support NuGet.

In my previous article from the series, I showed you how to use the standard page object model that comes from the Selenium.Support NuGet. However, I believe it has a couple of drawbacks. First, you need to install an additional NuGet package, which is an extra code dependency. Moreover, you do not have a full control over how the elements are located though the PageFactory class and the FindsBy attributes. If you want to create a SelectElement, you cannot since the FindsBy variables/properties return directly IWebElement.

Info

Test Case

For the examples, I will use once again the Bing’s home page, called BingMainPage. The main test case on this page is to search for a term and then assert the count of the returned results.

Post 1 Bing

Page Objects through Partial Classes Revised

Using this approach the BingMainPage class’s definition is placed inside three different files:

  • BingMainPage – contains the constructor(s) and the action methods
  • Map – stores all web elements’ properties
  • Asserter – holds all assertions

BingMainPage

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

 

The only difference with the previous version of this file is that we do not call PageFactory.InitElements(browser, this); in the constructor.

BingMainPage.Map

Info

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

The primary discrepancy between the two versions is here. We locate the elements though the WebDriver’s FindElement method instead of using the FindsBy attributes.

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.Asserter


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

The usage of the page object in tests stays the same.

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

Hi everyone. I'm part of the team here at TEST Huddle. I'm doing my best to make sure everyone gets the best out of the TEST Huddle community. So if you have any thoughts about the community, get in contact with me.
Find out more about @ronan

Leave a Reply

Upcoming WebinarTurning Uncertainty into a Catalyst for Change