How To Selenium: Code Reuse In Page Objects

In this article, we will show you a couple of techniques to increase the code reuse in your page objects through the use of base pages objects.

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.

Test Case

We will once again automate the main Bing page. All of the code is placed inside the BingMainPage class.

Partial Classes Base Pages

Initial Version without Base Page Objects

First, let us see how a regular page object (without base page) looks like and how we use it in tests. Then we are going to refactor it and make it use a base class.

BingMainPage.Actions NoBasePage Version

Info

BingMainPage.Elements NoBasePage Version

 

BingMainPage.Asserts NoBasePage Version

BingMainPage NoBasePage Version in Tests

With this version, we need to hold a separate instance of WebDriver in each page object. If the page needs to have a navigate logic, we need to implement it over and over again. The previous conclusions are valid for another logic that we can repeatedly use in the pages.

Second Version Page Objects with Base Pages

BingMainPage.Actions Base Page Version


The only change compared to the previous version is that BingMainPage derive now from the AssertedNavigatablePage abstract base class.

Page

 

This is the most basic base page object that we might have. It holds only a protected WebDriver property. We use this base page for pages that we do not need to navigate to them. E.g., middle pages in some wizard or something similar.

Info

NavigatablePage

 

If we need to expose a navigate logic, you can use the below base class. It adds to the child page a GoTo and Refresh methods and obligates the page to set its URL.

 

AssertedNavigatablePage

 

We use this base class if we need to have the methods and properties from the previous two and on top of that be able to make assertions. The IAssert interface exposes methods that are not test framework agnostic. You can read more about the concrete implementation in my article- Create Hybrid Test Framework- Abstract Unit Test Framework

 

BingMainPage Base Page Version in Tests

 

The usage in tests stays entirely 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

CTO and Co-founder of Automate The Planet Ltd, inventor of BELLATRIX Test Automation Framework, author of "Design Patterns for High-Quality Automated Tests: High-Quality Test Attributes and Best Practices" in C# and Java. Nowadays, he leads a team of passionate engineers helping companies succeed with their test automation. Additionally, he consults companies and leads automated testing trainings, writes books, and gives conference talks. You can find him on LinkedIn every day.
Find out more about @angelovstanton