Time for action – using the Page Object Pattern to design tests - Selenium

Imagine that you have a site that has a number of different pages that you need to test. This is quite common for a number of sites. We can create an object that represents the page and then pass the Selenium object in the programming language. So let us now create our first Page Object against the home page.

  1. Create a new Java class in IDEA called HomePage.

  2. Import the relevant packages for the tests to run.

  3. We will now need a constructor to handle Selenium. You may want to make it go to the home page when it is instantiated too. An example of this can be seen as follows:

    Chapter2.java BestPractises3.java
  4. If you create these three files you will see it pass. The test is a lot more succinct and easier to maintain.

What just happened?

In this section we had a look at creating tests using the Page Object design pattern.This allows us to create objects in a programming language and then pass the Selenium object to it to drive the browser. This creates a really nice DSL that allows all parties in the development cycle to understand. We create a Java object for each of the pages that we want to work against on the site. We then just instantiate the class to work against that page.

When we are moving between pages you click on a link and the method controlling the page transition will return an object representing a new page.

The objects will not hold the asserts; this should always be done within the tests.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Selenium Topics