PO Tutorial Lesson 1: Creating Reusable Code

267

Creating Reusable Code

The main concept behind the Page Object Model for testing is to create code representing different webpages for a website that can used by multiple tests or classes. This saves time and effort when writing tests.

Example Page Object Test


Writing a page object class is very simple, there are two basic guidelines for it:


  1. Write only as much code as you need for your given task
  2. Each class should model a page or a component on a page with the functionality it provides

The example above shows a test that uses a page object to click on a link to another page. In this case it clicks the first link that appears when searching for “France” from Wikipedia’s home page.


The class that models the home page has only got just enough code inside to allow the test to search for and click this link.

To show how this ties in with the concept behind the Page Object Model, here is another example of a test where the same methods are used but instead of just checking that the title changed as expected, it also checks whether the correct page was accessed too.

Example Page Object Test

In the next lesson, we will go more in depth into how these classes work, covering Web Drivers, Locators and Web Elements.