Implementing Page Object Model

Now that you’re familiar with POM design, loadable components, and WebDriver manager, now it’s time to learn implementation of a POM.

Page object model implementation #

We will learn to implement the page object model in accordance with the design discussed in Designing Page Object Model.

First, we will create a base class AbstractBasePage that extends org.openqa.selenium.support.ui.LoadableComponent and associate a file containing locators. We initialize AbstractBasePage with org.openqa.selenium.WebDriver and Locators file in the constructor of the class.

Anatomy of locators file #

Locators file could be of any format, such as .properties, .xml, .json, .yaml, .ini, .toml. For demonstration purposes, .properties is considered.

Keys present in the locators file should be suffixed with the type of locator. Selenium supports:

  • id – id attribute of a HTML tag.
  • linkText – text of <a> tag.
  • partialLinkText – substring text of <a> tag
  • name – name attribute of a HTML tag.
  • tagName – name of the HTML tag.
  • xpath – querying nodes or values from XML.
  • className – class attribute of a HTML tag.
  • cssSelector – querying nodes or values from HTML using CSS.

And the value of keys is the locator itself.

search_field.name = q

next_page.xpath = //span[contains(text(), 'Next')]

The above code snippet shows how to represent the locator key having suffix of one of the locator types.

Using appropriate libraries, the locator file is read and stored in appropriate data structures for querying. As we are using a properties file, we will use java.util.Properties for storing the key-value pairs.

Anatomy of AbstractBasePage

...