Selenium/Explanation/Page object pattern

Purpose
There are two main purposes of the page object pattern.


 * Move parts of the application that change a lot (user interface) to a central location. When the user interface changes, the test suite only needs to be updated in one place.
 * Make tests easier to read and understand by moving complexity to page objects.

Let's describe the two purposes with code samples from MediaWiki Core (as of June 2021).

With page object pattern
The code is split into two files. contains the test. contains the page object. Code is available at 702327.

Please notice how readable  is. You can immediately see that the point of  is to create an user account with   and   as arguments.



Please notice that  has two major sections. In the first section are elements on the page ( ...). The second section contains actions ( ...).



Without page object pattern
Let's compare that to a solution without page object pattern. Code is available at 702341.



Conclusion
With page object pattern, the framework is more complex, there are more files (two instead of one) and there is more overall code, but the tests are more readable and more maintainable.

Without page object pattern, the framework is simpler, there are less files (one instead of two) and there is less overall code, but the tests are less readable and less maintainable.

To keep this page short, I've selected a simple example. If the test suite creates accounts a lot, without page object pattern, code for creating account would have to be copy/pasted, leading to duplication and harder maintenance.

With the page object pattern, creating an account is just a call to  function. If any of the elements for creating the account changes, only  needs to be updated. If the logic of creating and account changes (for example, not requiring password re-entry) only  needs to be updated.

More information

 * PageObject by Martin Fowler (2013)
 * PageObjects by Simon Stewart (Selenium, 2015)
 * Page object models by Diego Molina (Selenium, 2019)
 * Page Object Pattern by Christian Bromann (WebdriverIO, 2021)
 * Selenium/Explanation/Stack
 * Selenium/Reference/Stack