Stop 07 · Web Automation TestingTopics · 5/10

Page Object Model (POM)

Part of the QA Testing Roadmap

In short

Forty tests all contain the login locators. A developer renames one button and forty tests fail. Page objects put each page's details in one place so one fix covers everyone.

A design pattern separating page structure (locators, actions) from test logic — dramatically improves maintainability, since a UI change only requires updating one page object, not every test that touches it.

The purple blob at a hotel front desk with a service bell hands a room key to a guest.

Think of it like this

The hotel reception desk

Guests don't wander the corridors hunting for towels. They ask reception, and reception knows which room, which floor, which staff. A page object is reception: tests ask for 'login', and only the page object knows the locators.

Watch it happen

A test talks to pages, not locators

Test
LoginPage
Browser

login(user, pwd)

1/5The test says what it wants in plain words. No locators in sight.

The big ideas

What you need to know

One class per page

LoginPage, CartPage, CheckoutPage. Each holds that page's locators and actions.

Tests hide details

Tests read like steps a user takes: login, add to cart, check total.

One fix, many tests

A changed locator is edited once in the page object, and every test benefits.

Keep asserts in tests

Page objects do actions and return data. Tests decide what is correct.

See it in code

Walk through the code

python
1from selenium.webdriver.common.by import By2 3 4class LoginPage:5    USER = (By.ID, "user-name")6    PASSWORD = (By.ID, "password")7    SUBMIT = (By.ID, "login-button")8 9    def __init__(self, driver):10        self.driver = driver11 12    def login(self, user, password):13        self.driver.find_element(*self.USER).send_keys(user)14        self.driver.find_element(*self.PASSWORD).send_keys(password)15        self.driver.find_element(*self.SUBMIT).click()16# In a test: LoginPage(driver).login("standard_user", "secret_sauce")

1/4All locators for this page live at the top, in one place.

A page object for Sauce Demo's login page. The * unpacks each (By, value) pair into find_element.

Save yourself the pain

Common mistakes

Oops

Putting assert statements inside page object methods.

Do this instead

Return data or the next page object, and let the test do the asserting.

Oops

One giant BasePage class with every locator for the whole site.

Do this instead

Split by page or component so each file stays small and easy to find.

Your mission

Refactor into pages

~40 min

Quick check

The login button's id changes. With POM done right, how many files do you edit?

Go deeper

  1. 1Refactor a simple test script into the Page Object Model structure.
  2. 2Learn why POM is considered a near-mandatory best practice for any serious automation framework.
  3. 3Practice adding a new test that reuses an existing page object without duplicating locators.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge