How To Selenium: Code Reuse In Page ObjectsGo Back
- Posted by Anton
Welcome to the newest post 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 post continues Anton’s guide on using Page Objects with Selenium.
This is the eight article from the WebDriver Page Objects Series. The copy-paste development is popular among the QA folks in the test automation world. Here, I 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.
We will once again automate the main Bing page. All of the code is placed inside the BingMainPage class.
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
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.
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.
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.
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.
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.
About Anton Angelov
I am the Founder and Editor in Chief of Automate The Planet. My passion is taking software quality to new heights through the integration of the best practices in the Quality Assurance and Automated Testing. I have considerable professional experience (6+ years) as a Senior Automated Testing Architect. Currently, as QA Architect and IT Consultant, I am improving the tooling and processes in a company named Innovative Lab. I am ardent about technologies such as .NET Framework, Azure, Bot Framework, SQL Server, Selenium WebDriver, and Jenkins. Outside my work as a consultant, I am an IT trainer leading training related to high-quality code, design patterns and automated testing.