Skip to main content
Course/Module 4/Topic 4 of 5Intermediate

Page Object Model (POM)

Master Page Object Model pattern for maintainable and reusable test automation

55 minBy Priygop TeamLast updated: Feb 2026

Page Object Model Benefits

  • maintainability: Centralized element management
  • reusability: Page objects can be reused across tests
  • Readability: Tests become more readable and clear
  • Scalability: Easy to add new pages and elements
  • Separation of Concerns: UI changes don't affect test logic

Page Object Implementation

Example
// Login Page Object
public class LoginPage {
    private WebDriver driver;
    
    // Page Elements
    @FindBy(id = "username")
    private WebElement usernameField;
    
    @FindBy(id = "password")
    private WebElement passwordField;
    
    @FindBy(id = "loginButton")
    private WebElement loginButton;
    
    @FindBy(className = "error-message")
    private WebElement errorMessage;
    
    // Constructor
    public LoginPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }
    
    // Page Actions
    public void enterUsername(String username) {
        usernameField.clear();
        usernameField.sendKeys(username);
    }
    
    public void enterPassword(String password) {
        passwordField.clear();
        passwordField.sendKeys(password);
    }
    
    public void clickLoginButton() {
        loginButton.click();
    }
    
    public DashboardPage login(String username, String password) {
        enterUsername(username);
        enterPassword(password);
        clickLoginButton();
        return new DashboardPage(driver);
    }
    
    public String getErrorMessage() {
        return errorMessage.getText();
    }
    
    public boolean isErrorMessageDisplayed() {
        return errorMessage.isDisplayed();
    }
}

// Dashboard Page Object
public class DashboardPage {
    private WebDriver driver;
    
    @FindBy(className = "welcome-message")
    private WebElement welcomeMessage;
    
    @FindBy(linkText = "Logout")
    private WebElement logoutLink;
    
    public DashboardPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }
    
    public String getWelcomeMessage() {
        return welcomeMessage.getText();
    }
    
    public LoginPage logout() {
        logoutLink.click();
        return new LoginPage(driver);
    }
}

// Test Class using Page Objects
public class LoginTest extends BaseTest {
    
    @Test
    public void testValidLogin() {
        LoginPage loginPage = new LoginPage(driver);
        DashboardPage dashboardPage = loginPage.login("testuser", "password123");
        
        Assert.assertTrue(dashboardPage.getWelcomeMessage().contains("Welcome"));
    }
    
    @Test
    public void testInvalidLogin() {
        LoginPage loginPage = new LoginPage(driver);
        loginPage.enterUsername("invaliduser");
        loginPage.enterPassword("wrongpassword");
        loginPage.clickLoginButton();
        
        Assert.assertTrue(loginPage.isErrorMessageDisplayed());
        Assert.assertEquals(loginPage.getErrorMessage(), "Invalid credentials");
    }
}
Chat on WhatsApp
Priygop - Leading Professional Development Platform | Expert Courses & Interview Prep