Design Pattern – Page Object Model (POM)

December 25, 2016

POM (Page Object Model)

In the previous tutorials, we have learned about How to automate scenarios using Selenium WebDriver? This tutorial will help you understand the Page Object Model.

It is important to write your codes and automation scripts in a maintainable, reusable and extendable manner. These key factors will distinguish you as a Good Automation Tester!

Design Patterns in Framework defines the standard of writing code. They help you in maintaining reusability and readability. POM (Page Object Model) is one such design pattern.

In Page object model there is one to one mapping, between the Java classes and pages of the application under test. For Example: Let us consider an application which has multiple pages – login page, homepage etc. Using Page Object Model design pattern there will be multiple java classes, one for Login Page, another for Homepage ad so on.

PS: People often get confused between design patterns and frameworks. POM is a design pattern, not a framework. It is an old concept. Developers are using it to maintaining reusability and readability for a very long time.

There are two different POM design pattern which I will be discussing here.

POM 1 Style of designing classes:

In this design pattern, there will be one Java class for each page of the application under test which will behave like an object repository.

All the possible Web Elements on a page are defined in the respective Java class and are then initialized in the constructor.

Page_Object_Model_Design_pattern _Style 1_Object Repository

For Demo purpose, let us consider a scenario on Ebay site, where we have to search a product, choose the category and click on search button. Let us capture this using POM.

Let us look in the code:

In the above code, all the WebElements required on the home page of Ebay are defined and then are initialised in the constructor thus acting like an Object Repository.

To execute a scenario using the above example, let us create another class with the main method:

In the above code, calling of a scenario will be in the format:

PageName.WebElement.method();

POM 2 Style of designing classes:

In this design pattern, there will be one Java class for each page of the application under test. This Java class will have all Web Elements and all the possible method on that page.

Let us consider an application which has a login page, all the Elements like username textbox, password field, login button, different links etc. on this page will be defined first and all the methods like Login, Signup, forget password etc. will be defined.

Page_Object_Model_Design_pattern_style_2

For Demo purpose, let us consider a scenario on Ebay site that we have to search a product, choose the category and click on search button. Let us capture this using POM.

Let us look at code section:

Let us create another class with the main method to execute the code:
In the above code, calling of a scenario will be in the format:

PageName.method();

In the next tutorial we will cover up the enhanced POM design patter (Page Factory).

PS: For any questions, queries or comments feel free to write us at support@qatechub.com or saurabh@qatechhub.com

Saurabh

About the Author

Saurabh

Follow Saurabh:

Leave a Comment: