Creating Page Object Model in Selenium: A Beginner’s Guide

If you’re automating UI tests with Selenium and still writing your locators and actions directly inside your test scripts, you might quickly find your code hard to maintain. That’s where the Page Object Model (POM) comes in.

In this blog post, you’ll learn:

  • What the Page Object Model is
  • Why you should use it
  • How to implement it in Selenium step by step
  • Real-world example: Login page

✅ What Is Page Object Model?

Page Object Model (POM) is a design pattern that creates a separate Java class (or file) for each page in your application. This class contains all the web elements and the methods (actions) you can perform on that page.

In short:

  • 1 page = 1 class
  • Tests interact with page classes, not directly with elements

🚀 Why Use Page Object Model?

BenefitExplanation
ReusabilityOne method or locator can be reused across tests
MaintainabilityUpdate locator in one place if the UI changes
ReadabilityTests look like readable steps instead of code blocks
Separation of ConcernsLogic and structure are separated from test flow

🧱 Structure of a Selenium POM Project

Typical folder structure:

cssCopyEditsrc/
  └── pages/
        └── LoginPage.java
  └── tests/
        └── LoginTest.java
  └── utils/
        └── BaseTest.java

✍️ Step-by-Step Example: Login Page POM

Let’s create a basic test for a login page using POM in Java + Selenium.

1. Create the Page Class (LoginPage.java)

javaCopyEditpackage pages;

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

public class LoginPage {
    WebDriver driver;

    // Locators
    By usernameField = By.id("username");
    By passwordField = By.id("password");
    By loginButton   = By.id("loginBtn");

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

    // Actions
    public void enterUsername(String username) {
        driver.findElement(usernameField).sendKeys(username);
    }

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

    public void clickLogin() {
        driver.findElement(loginButton).click();
    }

    public void loginAs(String username, String password) {
        enterUsername(username);
        enterPassword(password);
        clickLogin();
    }
}

2. Create the Test Class (LoginTest.java)

javaCopyEditpackage tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
import pages.LoginPage;

public class LoginTest {
    WebDriver driver;
    LoginPage loginPage;

    @BeforeClass
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("https://example.com/login");
        loginPage = new LoginPage(driver);
    }

    @Test
    public void testValidLogin() {
        loginPage.loginAs("testuser", "password123");
        // Add assertions here
    }

    @AfterClass
    public void tearDown() {
        driver.quit();
    }
}

🔄 Best Practices

  • Keep locators private in page classes
  • Use clear method names like enterEmail(), clickSubmit()
  • Avoid mixing test logic with element finding
  • For large pages, split methods logically (e.g., forms, modals)
  • Reuse helper methods like loginAs() to simplify tests

🧠 Summary

The Page Object Model is a key technique to structure Selenium automation tests for better readability, maintainability, and scalability. If you’re working on any kind of UI automation—POM should be part of your standard setup.

Leave a Reply

Your email address will not be published. Required fields are marked *