Reducing code with common objects and methods
Reducing code duplication and improving maintainability can be achieved by leveraging common objects and methods in your Page Object pattern. Some strategies to achieve code reduction are set out here:
- Base page class: Create a base page class that contains common objects and methods shared across multiple pages. This base class can encapsulate elements and behaviors that are common to multiple pages, such as a Home button, Halloween Party button, and then a Find My Candy! button, to reduce duplication:
Figure 9.1 – Header of CandyMapper party page website with links common to all pages
These elements appear in the header of every page of the website. So, it makes sense to declare them in the top-level page class and extend it to all other pages.
Figure 9.2 – Header page of the Candymapper landing page with three common links
If the selectors were in...