Wednesday, December 2, 2015

Page Object pattern

Selenium WebDriver - Page Object pattern


The Page Object pattern represents the screens of your web app as a series of objects. [Source]

There may be problems in the code we write. For example the tests are not very readable as there are lot of references to the WebDriver API that is a bit low-level. Secondly there can be many CSS selectors throughout all the tests. This produces two main problems:

- Excessive verbosity and a lack of readability in the tests due to all these references to CSS selectors and the WebDriver API.
- Difficult maintenance.

The Page Object pattern intends to solve these problems by encapsulating the details of accessing the elements of a page and interacting with it. The idea is to create an object that offers a logical view of the page and move all the references to the WebDriver and CSS selectors inside this object.

Besides this, the Page Object pattern has an additional advantage: it decouples our test from WebDriver so that we can switch to another tool if we want to . After all, technology moves fast, and in a year or two there could be better tools than WebDriver out there. In this event, it would be nice to change to such a tool without modifying our whole test suite, just the page object.
Although normally it is used for testing, the Page Object pattern can be used for any purpose related to UI automation - load testing, for example. [Source: Learning Behavior-driven Development with JavaScript by Enrique Amodeo]

The Page Object pattern provides tests for an interface, where a test can operate on the logical functionality offered by the page in a manner similar to the user accessing the page, but by hiding its internals. For example, if we build a Page Object for a login page that will provide a method to log in by accepting the username and password, and will take the user to the one page of the application. the test need not worry about what type of input controls are used for the login page, their locator details, navigation and so on.

Tests should use objects of a page at a high level, where any change in layout or attributes used for the fields in the underlying page should not break the test.

Selenium WebDriver provides outstanding support for implementing the Page Object pattern via it PageFactory class. The Page Object pattern brings the following advantages for your tests:

- It helps in building a layer of abstraction separating automation code, which knows about locating application elements and the one which interacts with these elements for actual testing.
- It provides a central repository of pages from the application for tests.
- It provides high maintainability and reduction in code duplication. [Source: Instant Selenium Testing Tools Starter by Unmesh Gundecha]

Few links on Page Object Pattern:

1. Page Object Pattern - Assert Selenium. [Link]
2. What is the Page Object Pattern in Selenium WebDriver? [Link]
3. Page Object Design Pattern. [Link]
4. Page Object Pattern in Automation Testing. [Link]
5. Selenium's Page Object Pattern: The Key to Maintainable Tests. [Link]
6. Page Object Pattern - Youtube Video. [Link]
7. Page Object Pattern - White Box Testing. [Link]