The Page Object Model (POM) is an approach/design pattern to implement the Data Driven Framework, Keyword Driven Framework or Hybrid Framework. POM in itself is not an automation framework, but it outlines some rules which help in creating a robust and easy to maintain code which should be dynamic enough to incorporate any future changes.
This approach mandates the separation of application in:
- Pages – Separate class is created for each page. These classes should contain the manipulation of locators and business logic.
- Test Classes – Tests are created in the separate classes which utilize the Page classes.
Above points will be clear in subsequent tutorials as you start to learn how to implement the Page Object Model.
Why Use Page Object Model
Every organization is moving towards rapid development where frequent changes are inevitable. Agile methodology is being used to develop and publish the product in the market as soon as possible.
If you have ever worked on an agile environment, you would have witnessed frequent enhancements or changes in the functionality. These changes/ enhancements could be user requests, change in business logic, etc. If a framework is developed with business logic and tests on a single class then maintenance is a nightmare.
To cope with the dynamic nature of application development, the QA automation framework should be robust and scalable enough to accommodate any new module or change in existing functionality.
Page Object Model – 1 Webpage 1 Java Class
Page Object Model approach suggests creating one java class per application page (web page) and separate test class for each web page.
Take an example of any online shopping website. It usually contains the following pages:
- Login Page
- Home Page
- Registration Page
- Product Search Result Page
- Products Page (The page which opens when user click on an item)
- Add To Cart Page
- Payment Page, etc
Since the number of pages in a website are limited, there should be no problem in creating separate java class for each page.
Demo Website and Tests
For this tutorial, the demo website is http://automationpractice.com/index.php. Navigate to this website and try to get familiar with the basic functionality. The first page displayed on navigation is the landing page. Searching for any product should display the product search page. Similarly, clicking on Sign In button should display the Sign In page.
I would urge you to register yourself (you may use a fake email address) to this website.
We will automate the following tests on this website:
- Is the landing page displayed?
- Verify if the user is able to search for a particular product?
- Validate if the user is able to sign in successfully?
Following classes should be created with their respective business logic:
- LandingPage.java – Class created for the landing page.
- ProductSearchPage.java – This class should handle the page which is displayed after user search for a particular product.
- SignInPage.java – If you click on Sign In button at the landing page, another web page containing the new user registration and sign in options should get displayed. This class refers to this webpage.
- HomePage.java – This class refers to the page displayed after the user is signed in successfully.
Once the page classes are created, next is to create separate test classes for each page class. These test classes contain the actual tests. If in future, the functionality of any of the above page(s) is changed then the user only needs to update the respective page class.
Above images demonstrate the various pages. We will create one java class for each web page and corresponding test class. Below is the one to one mapping of page classes and test classes:
Creating The Maven Project
We will create a maven project since it is mostly being used in all the organizations. Goto the eclipse File -> New -> Project and select Maven Project.
In new Maven project pop up, select Create a simple project checkbox and click on Next keeping the default values selected. Enter the following values in the mentioned fields on the next page:
- Group Id: com.appliedselenium
- Artifact Id: PageObjectNoPageFactory
Click on Finish button and a new maven project should get created as following.
In the next tutorial, we will add more components to this framework.