How To Selenium: Page Objects – App Design Pattern

Welcome to 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 post continues Anton’s guide on using Page Objects with Selenium.

This is the tenth article from the WebDriver Page Objects Series. Here I am going to share with you how to create a single place for handling the WebDriver initialisation and creation of page objects. I call it App (short for application).

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.

Bing Page how to selenium

Page Objects with Base Pages

All of our page objects need to derive from some of the base page classes which you can find after the page object’s code.

BingMainPage.Actions

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

Because of the base class, we can use in tests the GoTo and the Refresh methods. Also, we have access to the Assert property.

Info

BingMainPage.Elements

 

 

We use the WrappedDriver protected property that comes from the base classes to locate the elements.

BingMainPage.Asserts

 


<

We use the Assert protected property that comes from the base class to perform the validations. This property is test framework agnostic, which means that we can use it with MSTest or NUnit. 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

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.

App Design Pattern in Tests

 

 

As you can see, we do not have any WebDriver initialization code in the tests. Everything is happening behind the scenes the App class. Moreover, the App instance is responsible for the initialization and navigation to the different page objects. Here, we use the generic GoTo method to navigate to the BingMainPage.

App

In the constructor of the class, we start the browser. We have two generic methods for creation of the page objects. We use reflection to initialize the objects. However, there are various ways to do the same thing even better. I prefer to use inversion of control containers or service locators. You can read more about the IoC containers in my article- Use IoC Container to Create Page Object Pattern on Steroids

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