</>

Technology

Page Object Model

Difficulty

Beginner

Interview Question

What is Page Object Model (POM)? Why is it used in Selenium automation?

Answer

Page Object Model (POM)

Page Object Model is a design pattern in Selenium automation where each web page (or significant page component) is represented as a Java class containing:

  • All element locators for that page
  • All interaction methods (click, type, get text) for those elements

Test classes then call these page methods instead of directly interacting with elements.

Why Use POM?

CODE
WITHOUT POM — tests are brittle:

@Test
public void testLogin() {
    driver.findElement(By.id("email")).sendKeys("user@test.com");
    driver.findElement(By.id("password")).sendKeys("pass123");
    driver.findElement(By.id("login-btn")).click();
    // If id="email" changes to id="user-email"...
    // You must update EVERY test that uses this locator!
}
CODE
WITH POM — one change fixes everything:

// LoginPage.java — locators defined ONCE
By emailField = By.id("email");   // change here only

// Test calls the page method
loginPage.login("user@test.com", "pass123");
// No locator in the test — test survives UI change!

Benefits of POM

BenefitExplanation
MaintainabilityLocator changes in one place (page class), not in every test
ReusabilityloginPage.login() called from 50 tests — write once
ReadabilityTests read like business steps: cartPage.addToCart("Laptop")
Separation of ConcernsTest logic ≠ UI interaction code
Team scalabilityQA team can work on different pages independently

Basic POM Implementation

Java
// LoginPage.java — Page Object
public class LoginPage {
    private WebDriver driver;

    // 1. Locators
    private By emailField   = By.id("email");
    private By passwordField = By.id("password");
    private By loginButton  = By.cssSelector("[data-testid='login-btn']");
    private By errorMessage = By.className("error-msg");

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

    // 3. Actions
    public void enterEmail(String email) {
        driver.findElement(emailField).sendKeys(email);
    }

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

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

    // 4. Composite action — combines multiple steps
    public DashboardPage login(String email, String password) {
        enterEmail(email);
        enterPassword(password);
        clickLogin();
        return new DashboardPage(driver);   // returns next page
    }

    // 5. Assertions / getters
    public String getErrorMessage() {
        return driver.findElement(errorMessage).getText();
    }

    public boolean isErrorDisplayed() {
        return driver.findElement(errorMessage).isDisplayed();
    }
}

Test Using POM

Java
// LoginTest.java — clean, readable test
public class LoginTest {
    WebDriver driver;
    LoginPage loginPage;

    @BeforeMethod
    public void setup() {
        driver = new ChromeDriver();
        driver.get("https://automateqa.online/login");
        loginPage = new LoginPage(driver);
    }

    @Test
    public void testValidLogin() {
        DashboardPage dashboard = loginPage.login("user@test.com", "pass123");
        Assert.assertTrue(dashboard.isWelcomeMessageDisplayed());
    }

    @Test
    public void testInvalidLogin() {
        loginPage.login("wrong@test.com", "wrongpass");
        Assert.assertEquals(loginPage.getErrorMessage(), "Invalid credentials");
    }

    @AfterMethod
    public void teardown() {
        driver.quit();
    }
}

Follow AutomateQA

Related Topics