The Page Object model is a kind of design pattern which is popular in test automation for increasing the test maintenance and reducing the code duplication. The page object is an object-oriented class which serves as an interface to a page of AUT. The Page class consists of web elements and methods to interact with web elements. When automating the test cases, we create objects of page classes and interact by calling methods. The benefit is when UI changes for that page the test doesn’t need to change, but the code within that page object needs to change.
The Design Pattern:
The design pattern is probably used in the selenium where web pages are represented by corresponding class and web elements which are represented by variables of the class; possibly interactions happen through the methods.
The advantages are of Page Object Model:
There is a clear separation between a test code and page specific code like locators and layout. The single repository for the services and operations offered by the page other than having these services scattered throughout the test.
Reusability, we can reuse the page class for different tests if needed, that is we don’t have to write code again and again for identifying the code elements and also methods for interactions with them for every test.
Maintainability, the above picture shows that the test case and page class are different from each other which means we can update code if any web element is added or the one which is existing is updated.
Readability, as in the above picture, the page code is separated from the test code in turn which helps in code readability improvement.
Implementation of Project Object Model:
We will see how to implement the page object model, as per the design we have separate classes for all the pages and where the objects are stored for each web page there should be a separate page class.
Consider an example suppose we need to create page object for google search page. Firstly we have to create a page class for each web page like Google search page and create a class for search button and text box and store locators separately.
- Right click on the test file -> New-> package
- Create class by right click on pages -> New-> class
- Write the code to identify the web object separately to text box as well search button.
- In the test case write the code where we are passing driver as a instance to open the class googlesearchpage.java
- For google search button we have to write the test code.
Here we are referring the objects from one page class to another. There will be a lot of flexibility in how page objects should be designed. There are some rules for getting the desired maintainability of our test code.Page objects should not make any verifications and assertions. This is always a part of our test and it should be within the test code. The page object will contain the representation of the page and therefore the services the page provides via methods but no code associated with what’s being tested should be within the page object.