How To Selenium: Code Reuse In Page Objects

Reading Time: 2 minutes

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

Anton Angelov is a Quality Assurance Architect at Telerik a Progress Company, a leading vendor of products that make the life of programmers easier. He is passionate about automation testing and designing test harness and tools, having the best industry development practices in mind. In addition, he is an active blogger and the founder of Automate The Planet. He strives to make the site one of the leading authorities in Automation Testing by presenting compelling articles, inspiring ardent discussions amongst the community. He is also one of the most-rated-answer authors of questions about Test Automation Frameworks (WebDriver) on Stack Overflow.
Find out more about @angelovstanton

Leave a Reply

Upcoming WebinarTurning Uncertainty into a Catalyst for Change