DEV Community

Saras Growth Space
Saras Growth Space

Posted on

Selenium Simplified — Page Object Model (POM) Explained

Introduction

Throughout this series, we have learned how to:

  • Launch browsers using Selenium
  • Locate elements on a webpage
  • Handle waits and dynamic content
  • Work with alerts, frames, and multiple windows
  • Perform advanced user interactions
  • Validate results using assertions

These skills allow us to write automation scripts.

But when test suites grow larger, another challenge appears:

How do we organize automation code so it stays clean and maintainable?

This is where the Page Object Model (POM) comes in.


What is the Page Object Model?

The Page Object Model (POM) is a design pattern used in test automation.

It organizes code by separating:

  • Page elements
  • Page actions
  • Test logic

Instead of writing all Selenium code inside test classes, we create separate classes representing web pages.

Each page class contains:

  • Locators for elements
  • Methods that perform actions on the page

The Problem Without POM

Let’s look at a simple login test written without POM.

driver.findElement(By.id("username")).sendKeys("user");
driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.id("loginButton")).click();
Enter fullscreen mode Exit fullscreen mode

This works for small tests.

But in a large project:

  • Many tests use the same elements
  • Locators get repeated in multiple files
  • UI changes require updates in many places

This quickly becomes hard to maintain.


How Page Object Model Solves This

With POM, we create a page class representing the login page.

Example structure:

LoginPage.java
LoginTest.java
Enter fullscreen mode Exit fullscreen mode

The page class contains the elements and actions.

The test class contains the test logic.


Example: Login Page Class

public class LoginPage {

    WebDriver driver;

    By username = By.id("username");
    By password = By.id("password");
    By loginButton = By.id("loginButton");

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

    public void enterUsername(String user) {
        driver.findElement(username).sendKeys(user);
    }

    public void enterPassword(String pass) {
        driver.findElement(password).sendKeys(pass);
    }

    public void clickLogin() {
        driver.findElement(loginButton).click();
    }
}
Enter fullscreen mode Exit fullscreen mode

This class represents the Login Page.


Example: Test Class Using POM

LoginPage loginPage = new LoginPage(driver);

loginPage.enterUsername("user");
loginPage.enterPassword("password");
loginPage.clickLogin();
Enter fullscreen mode Exit fullscreen mode

Now the test code becomes much cleaner and easier to read.


Benefits of Page Object Model

Using POM provides several advantages.

Better Code Organization

Each page has its own class.

Reusability

Multiple tests can reuse the same page methods.

Easier Maintenance

If a locator changes, it only needs to be updated in one place.

Improved Readability

Tests become easier to understand.

Example:

loginPage.enterUsername("user");
loginPage.enterPassword("password");
loginPage.clickLogin();
Enter fullscreen mode Exit fullscreen mode

This reads almost like natural language.


Typical Automation Framework Structure

A common Selenium project using POM might look like this:

project
 ├── pages
 │    └── LoginPage.java
 │
 ├── tests
 │    └── LoginTest.java
 │
 └── utilities
      └── DriverSetup.java
Enter fullscreen mode Exit fullscreen mode

This structure keeps the automation framework organized and scalable.


When Should You Use POM?

Page Object Model becomes especially useful when:

  • The test suite grows larger
  • Multiple testers contribute to the automation code
  • The application UI changes frequently

Most modern Selenium frameworks use POM as a standard practice.


Final Thoughts

As automation projects grow, maintaining clean and scalable code becomes essential.

The Page Object Model helps organize Selenium tests by separating page structure from test logic.

This results in:

  • Cleaner tests
  • Easier maintenance
  • Better collaboration among teams

Because of these benefits, POM is widely used in real-world automation frameworks.


Wrapping Up the Series

In this series, we explored the core concepts of Selenium automation:

  • Selenium architecture
  • Locators
  • Waits
  • Alerts, frames, and windows
  • Mouse and keyboard interactions
  • Assertions
  • Page Object Model

These topics provide a strong foundation for building reliable automation tests.

If you're starting with Selenium, mastering these concepts will help you move from basic scripts to real automation frameworks.

Top comments (0)