Benefit from a richer Page Object Model with abstract classes and functions

Published on November 25, 2024

The Page Object Model we create to support our automated tests can be described as a model of the application we are testing. We can make it a richer model using abstract classes and functions.

“classes, methods, and fields in TypeScript may be abstract.
An abstract method or abstract field is one that hasn’t had an implementation provided. These members must exist inside an abstract class, which cannot be directly instantiated.
The role of abstract classes is to serve as a base class for subclasses which do implement all the abstract members. When a class doesn’t have any abstract members, it is said to be concrete.” [1]

One feature of abstract classes is that instances can not be created of them. The base case classes in your Page Object Model probably should be abstract because instances of them can not be created in the tests and the abstract base class can include an abstract method which can then be implemented in sub-classes. 

Another feature of abstract classes is that they can contain abstract methods. If an abstract class contains an abstract method sub-classes must implement the method. This helps to create a consistent interface for the tests making them easier to read and maintain. 

If I wanted to create Playwright tests that test different search engines I would create an abstract class that contains an abstract method makeSearch(), as shown in the image below:

If I try to create an instance of SearchPage an error is returned because it is an abstract class:

 To test Google and Bing I created concrete classes that extend SearchPage:


The concrete classes must contain the method makeSearch() because they extend SeachPage. If they do not contain makeSearch() an error is returned:

When I created tests I created instances of the Google and Bing classes and called makeSearch() on the instances of both classes.

If there is a new requirement to test another search engine a new concrete class for the search engine can be created by extending the SearchPage and makeSearch() can be used to test the additional search engine.

This example uses an abstract class to make the POM richer and provide control over how tests are developed. Tests should be written using the GoogleSearch and BingSearch classes, not the SearchPage class. SearchPage is an abstract class so testers are prevented from creating an instance of the class SearchPage, but can create instances of GoogleSearch and BingSearch. 

The abstract class also contains an abstract function which its subclasses have to implement. The GoogleSearch and BingSearch classes both contain an implementation of the method makeSearch() which is used in the tests. The tests are easier to read because the tests for both search engines use makeSearch().

Abstract classes are a way to enrich the Page Object Model which provides control over how tests are written and also makes tests easier to read.

More information:

References

[1] Typescriptlang.org