Apply Factory Pattern over Page Object to enrich the test design flow

The information described here requires a basic understanding of how Page Object Pattern is applied. It is recommended that you read this before moving forward with the content of this article.

Factory pattern is useful when there is a large number of page object implementations. With great numbers comes great responsibility and this way the need to provide a centralized and easy way to access the pages.

Implementation

Step 1 – Defining

In order to write down generic methods for all pages we first create a base class which we call “Component”:


class Component(object):
    @abstractmethod
    def set_name(self):
        pass

    @abstractmethod
    def get_actions(self, *args):
        pass

Step 2 – Implementing

Next, we need to apply this to our custom pages. To achieve this, we will use a concept of Object-Oriented Programming called “Inheritance”. Using Python a class can freely inherit multiple classes. For this, we create a new class in which we merge the Component and the Page class. As a convention, we will name these classes like PageComponent.


class SearchComponent(Component, Search):
    """
    Each new product will implement specific actions
    """
    CLICK_SEARCH = 'click_search'
    SET_QUERY = 'set_query'

    def set_name(self):
        return 'Youtube search component'

    def get_actions(self, *args: list):
        if args[0] == self.CLICK_SEARCH:
            self.search()
        elif args[0] == self.SET_QUERY:
            self.set_query(args[1])
        else:
            raise NotImplemented

Notice that the factory principle is present in the get_actions method, where the action is provided in a natural language. Based on this parameter it decides what action to build. Additional parameters specific for the desired method are passed alongside the action name.

Step 3 – Testing

Now that we have finished mapping our methods to simpler words, the next and final step is to link them together to form logical test flows.


class SearchTest(object):
    
    def test_run():
        search = SearchComponent()
        search.get_actions(search.CLICK_SEARCH)
        search.get_actions(search.SET_QUERY, 'my search text')

Step 4 – Enjoy

Page object allows you to organize the elements and the methods specific to a portion/context of an application, while the factory offers support at the page level. By using this approach the implementation takes a while longer, but the value is reflected in the long run, especially in the maintenance process when the test cases need to be updated.

Having applied modularization on both the page methods and on the pages themselves, each change that will occur during the development process can easily be rectified with minimum changes.

2 thoughts on “Apply Factory Pattern over Page Object to enrich the test design flow

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.