Everyone uses page object model in Selenium test automation projects.
But what is it exactly?
What does it say?
Who invented it?
Page object model was invented by Martin Fowler in 2013.
It is a model that helps writing better automated tests.
There are multiple things that can be learned from it, all beneficial for your automation projects.
Let’s go through them, one by one.
if you write tests that manipulate the HTML elements directly your tests will be brittle to changes in the UI. A page object wraps an HTML page, or fragment, with an application-specific API, allowing you to manipulate page elements without digging around in the HTML.
In other words,
a test should not interact with an HTML element directly
a test should interact instead with a page object
a page object can wrap an HTML page
a page object can wrap an HTML page fragment (instead of the whole page)
The basic rule of thumb for a page object is that it should allow a software client to do anything and see anything that a human can.
A page object should allow tests to do what users do. What do users do? They execute actions on the page such as searching, going to a page, changing the sort order of results, adding products to a cart, etc.
The page object should encapsulate the mechanics required to find and manipulate the data in the gui control itself.
The page object should allow tests to do what users do. It should hide the details of how the interaction with the page is done. The tests should not be able to know how elements are found, what locators are used for elements, how an action is implemented.
Despite the term "page" object, these objects shouldn't usually be built for each page, but rather for the significant elements on a page.
A page object can be built for a page but it can also be built for a significant elements of the page. In the case of a results page, you could have a page object for the pagination element, another page object for the filtering element, another for the sorting element, one for the header, one for footer, etc.
Similarly if you navigate to another page, the initial page object should return another page object for the new page.
If a page method initiates a navigation to another page, this page method should return an object for the new page.
Page objects are commonly used for testing, but should not make assertions themselves. Their responsibility is to provide access to the state of the underlying page. It's up to test clients to carry out the assertion logic.
Do not use assertions in the page objects. Use them instead in the test methods. The page methods should either interact with the page (and do not return a value) or return a value which can be asserted in the test method.
You can read the full blog post here.
In case you are interested in seeing how all these can be done in code,
please read my Amazon ebook on page object models.
Thank you.