Page Object Pattern

Page Object Pattern, the term that selenium users keep buzzing. Page object is a design pattern that can be implemented as a selenium best practices. The functionality classes (PageObjects) in this design represent a logical relationship between the pages of the application.

  • The Page Object pattern represents the screens of your web app as a series of objects and encapsulates the features represented by a page.
  • It allows us to model the UI in our tests.
  • A page object is an object-oriented class that serves as an interface to a page of your AUT.

More on Page Object Pattern at Selenium wiki. Some of the advantages of page object pattern as listed below,

  • Reduces the duplication of code
  •  Makes tests more readable and robust
  • Improves the maintainability of tests, particularly when there is frequent change in the AUT. (Useful in Agile methodology based projects)

Enough of theory, lets get into practical implementation.

Page Object, which models the Google search page:

WebDriver provides a way to map it to a real web page. The PageFactory class provides a convenient way of initializing and mapping the Page Object fields.

By default, it will map Page Object properties to fields with matching ids or names, so the example given here will work fine out of the box. But sometimes we need more control over identifying elements in the HTML page and mapping them to our Page Object fields. One way to do this is to use the @FindBy annotation, as shown in the following code:

6 thoughts on “Page Object Pattern”

  1. This article was very useful, it makes clear to understand the purposes and the use of Page Objects. Thanks a lot. But I think that in the last piece of code the class name should be “AnnotatedGoogleSearchPage” instead of “GoogleSearchPage” to be the same name as the constructor method.


Leave a Reply to Edmilson Ferreira Cancel reply