Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

What is the Page Object Model (POM) in Selenium WebDriver?

The Page Object Model (POM) in Selenium WebDriver is a design pattern that represents each web page of an application as a separate class. Each page class holds the web elements on that page (buttons, fields, links) along with the actions performed on them, so your test code interacts with page methods instead of raw locators. This separation makes automation code cleaner, easier to read, and far simpler to maintain.

Instead of scattering locators across dozens of tests, POM keeps them in one place per page. If an element's locator changes, you update it in a single page class rather than in every test that uses it, which is why POM is one of the most widely adopted patterns in Selenium test automation.

Understanding the Page Object Model

In POM, every page or major component of the application under test becomes an object repository: a class that exposes methods for the operations a user can perform on that page. The test script never touches locators directly; it calls high-level methods such as loginPage.login(user, password). This keeps the test readable and shields it from UI implementation details. For a detailed walkthrough, see the TestMu AI guide on the Page Object Model design pattern in Selenium Java.

Why Use the Page Object Model?

  • Maintainability: Locators live in one place per page, so a UI change requires a single update instead of edits across many tests.
  • Reusability: The same page class and its methods can be reused across multiple test cases, reducing duplication.
  • Readability: Tests read like user stories because low-level locator logic is hidden behind meaningful method names.
  • Scalability: Adding new tests takes less effort, which increases coverage and helps uncover more defects.
  • Separation of concerns: Page structure and test logic are decoupled, keeping the framework organized as it grows.

How the Page Object Model Is Structured

A typical POM implementation follows a clear separation:

  • Page classes: One class per page, such as LoginPage or HomePage, containing that page's element locators.
  • Page methods: Actions on the page, like enterUsername() or clickLogin(), that encapsulate the locators.
  • Test classes: Test scripts that create page objects and call their methods, never referencing locators directly.

Implementing the Page Object Model in Selenium

Here is a simple Java page class for a login page. It stores the locators and exposes a single login method:

package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginPage {
    private final WebDriver driver;

    // Locators for the login page
    private final By usernameField = By.id("user-name");
    private final By passwordField = By.id("password");
    private final By loginButton = By.id("login-btn");

    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    // Action that hides the locators from the test
    public void login(String username, String password) {
        driver.findElement(usernameField).sendKeys(username);
        driver.findElement(passwordField).sendKeys(password);
        driver.findElement(loginButton).click();
    }
}

The test class then stays clean and expressive, with no locators in sight:

import org.openqa.selenium.WebDriver;
import pages.LoginPage;

public class LoginTest {
    public void validLoginTest(WebDriver driver) {
        LoginPage loginPage = new LoginPage(driver);
        loginPage.login("standard_user", "secret_sauce");
        // assertions to verify successful login go here
    }
}

POM vs Page Factory

Page Factory is an extension of POM that reduces boilerplate using annotations. The key differences are:

  • Element declaration: Plain POM uses By locators, while Page Factory uses @FindBy annotations initialized with PageFactory.initElements.
  • Initialization: Page Factory lazily initializes elements when they are first used, whereas plain POM locates them on demand in each method.
  • Boilerplate: Page Factory reduces repetitive findElement calls, making large page classes more concise.
  • Dynamic locators: Plain POM handles dynamic locators more flexibly, since @FindBy cannot accept runtime values directly.

Common Mistakes and Troubleshooting

  • Adding assertions inside page classes: Page objects should perform actions and return data; keep assertions in the test layer to preserve separation of concerns.
  • Fat page classes: Putting an entire large page in one class becomes unmanageable. Split complex pages into component objects.
  • Hardcoding waits: Sprinkling Thread.sleep across methods causes flakiness. Use explicit waits inside page methods instead.
  • Brittle locators: Relying on absolute XPaths breaks easily. Prefer stable IDs, names, or CSS selectors.
  • Duplicating locators: Defining the same element in multiple classes defeats the purpose of POM. Keep each element in exactly one page object.

Running POM-Based Tests Across Real Browsers and Devices

Because POM cleanly separates page logic from test logic, the same page objects can drive tests across many environments without changes. Running your POM-based Selenium suite on TestMu AI lets you execute across 3000+ real browsers, browser versions, and operating systems in parallel, so you validate behavior everywhere your users are.

By pointing your WebDriver at a cloud Selenium automation grid and enabling cross browser testing, your maintainable POM framework scales from a single local browser to a full matrix of real browsers and devices with minimal effort.

Conclusion

The Page Object Model organizes Selenium WebDriver tests by turning each page into a class of elements and actions. It centralizes locators, improves readability, and makes large suites maintainable and reusable. Whether you use plain POM or extend it with Page Factory, applying the pattern, avoiding common pitfalls, and running the resulting tests across real browsers will keep your automation reliable as your application grows.

Frequently Asked Questions

What is the Page Object Model in Selenium?

The Page Object Model is a design pattern that represents each page of an application as a separate class holding that page's elements and actions. Tests call methods on these page classes instead of using locators directly, which keeps test code clean and maintainable.

What is the difference between POM and Page Factory?

POM is the overall design pattern of organizing pages into classes. Page Factory is an extension of POM that uses @FindBy annotations and lazy initialization via PageFactory.initElements to reduce boilerplate when declaring web elements.

What are the advantages of the Page Object Model?

POM centralizes locators so UI changes only require updates in one class, improves readability by hiding low-level details, enables reuse of page methods across tests, and reduces duplication, making automation suites easier to scale and maintain.

Is Page Object Model a framework?

No. POM is a design pattern, not a framework. It is commonly combined with test frameworks like TestNG or JUnit and build tools like Maven to form a complete automation framework, but on its own it is simply a way to structure code.

When should you use the Page Object Model?

Use POM whenever your test suite grows beyond a handful of scripts or the UI changes frequently. By isolating locators and actions in page classes, POM keeps large suites maintainable and prevents small UI changes from breaking many tests at once.

Does POM work only with Java?

No. POM is language-agnostic and works with any Selenium binding, including Python, C#, and JavaScript. The idea of representing pages as classes with elements and actions applies equally across all supported languages.

Related Questions

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests