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.
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 == self.CLICK_SEARCH: self.search() elif args == self.SET_QUERY: self.set_query(args) 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.