0
0
Selenium-pythonConceptBeginner · 4 min read

Page Object Model in Selenium: What It Is and How It Works

The Page Object Model in Selenium is a design pattern that creates an object repository for web UI elements. It helps organize test code by separating page structure and test logic, making tests easier to maintain and read.
⚙️

How It Works

Think of the Page Object Model (POM) as a map for your web pages. Instead of writing test steps mixed with details about buttons and fields, POM keeps all the page details in one place called a page object. This page object acts like a helper that knows how to find and interact with elements on that page.

When you write tests, you just ask the page object to do things like click a button or enter text. This way, if the page changes, you only update the page object, not every test. It’s like having a remote control for your web page that hides the complex details.

💻

Example

This example shows a simple login page object and a test using Selenium with Java. The page object stores locators and actions, and the test uses them to check login functionality.

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

// Page Object for Login Page
public class LoginPage {
    private WebDriver driver;
    private By usernameField = By.id("username");
    private By passwordField = By.id("password");
    private By loginButton = By.id("loginBtn");

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

    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 boolean isLoginButtonDisplayed() {
        return driver.findElement(loginButton).isDisplayed();
    }
}

// Test class
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoginTest {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com/login");

        LoginPage loginPage = new LoginPage(driver);
        loginPage.enterUsername("user1");
        loginPage.enterPassword("pass123");
        loginPage.clickLogin();

        // Simple assertion
        if (!loginPage.isLoginButtonDisplayed()) {
            System.out.println("Test Passed: Login button is not displayed after login.");
        } else {
            System.out.println("Test Failed: Login button is still displayed.");
        }

        driver.quit();
    }
}
Output
Test Passed: Login button is not displayed after login.
🎯

When to Use

Use the Page Object Model when you have many tests interacting with the same web pages. It helps keep your tests clean and easy to update if the website changes. For example, if a button’s ID changes, you only fix it in one place—the page object—instead of every test.

This pattern is especially useful for large projects with many testers or when tests need to run over a long time with frequent website updates.

Key Points

  • Separates page details from test logic for clarity.
  • Makes tests easier to maintain and read.
  • Reduces duplication of locator code.
  • Improves test reliability when UI changes.

Key Takeaways

Page Object Model organizes web page elements and actions into reusable objects.
It separates test code from page details, making maintenance easier.
Use POM to reduce duplicated code and improve test clarity.
POM is ideal for projects with many tests and frequent UI changes.
Updating locators in one place keeps tests stable and faster to fix.