How to Create Good Page Objects For Test Automation

Probably one of the most important automated testing best practices is to follow the Page Object Pattern. I’m sure you heard this one before.

However, I would say at least 80% of people doing test automation still don’t follow the pattern as defined. The pattern that’s existed for over a decade.

In my point of view, that’s a mistake. This is a tried and true pattern that works when followed.

Our team of SAs all follow this pattern and have not had to deviate away from it for any reason. It’s easy and it works. No reason to complicate anything or reinvent the wheel.

A Good Page Object Pattern:

  1. The page object class has a great name that tells us exactly what the HTML page or HTML component does
  2. The page class contains behavior-driven methods
  3. The page class contains properties to store information about the HTML page or component
  4. Properties and methods live in a single class
  5. The page object exposes only methods that an end-user would use to interact with the HTML
  6. A page object doesn’t need to be an entire HTML page, it can be a small component as well

1. The page object class has a great name that tells us exactly what the HTML page or HTML component does

If you cannot name your page object so that it’s 100% clear what’s inside of that page object, then it’s likely your page object does too much.

What does this page object do SauceDemoLoginPage? What methods will be exposed for a user to interact with the HTML?

Are you surprised by what’s inside of this class? Probably not.

2. The page class contains behavior-driven methods to interact with the DOM

The only public methods that are allowed in your page objects are those that an end-user can perform to your web application.

On this HTML page, the only two actions that an end-user can perform are Open() and Login()

The user cannot ConnectToSQL(), OpenExcel(), ReadPDF(). Hence, such actions should never be found in your automated UI tests.

3. The page class contains properties to store information about the HTML page or component

The page object should contain information about the page object such as element locators. They shouldn’t live in an external source because if you need to maintain them, it’s easiest to go to a single place.

When the LoginPage changes, just go to the LoginPage class and fix it there.

4. Properties and methods live in a single class

Similar to point 3, but don’t separate methods and properties of a page object. That actually makes maintenance harder. With the proper use of composition, you page object will never be large (See point 6).

5. The page object exposes only methods that an end-user would use to interact with the HTML

On this HTML page, the only two actions that an end-user can perform are Open() and Login()

The user cannot ConnectToSQL(), OpenExcel(), ReadPDF(). Hence, such actions should never be visible in your automated UI tests and should not be expose in your Page Objects. These actions may be needed, but they don’t belong in a Page Object.

6. A page object doesn’t need to be an entire HTML page, it can be a small component as well

The name page object is very unfortunate as the class that we create doesn’t actually need to be an entire page. Rather, it should be composed of components. A HeaderComponent, a FooterComponent, and CartComponent as in the example below.

Example of a Page Object that uses composition

Here’s a video tutorial on Page Objects where I tackle this topic in depth

If you want to learn more about page objects, automation best practices, CI/CD, and framework development then check out The Complete Selenium WebDriver with Java Bootcamp that will teach you all that you need to know.

Nikolay Advolodkin is a self-driven SDET on a lifelong mission to create profound change in the IT world and ultimately leave a legacy for his loved ones

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store