#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Selenium Frameworks

Page Object Model (POM)

What is POM?

The Page Object Model is a design pattern where each web page (or component) is represented as a Java class, encapsulating its locators and the actions that can be performed on it.

Example

public class LoginPage {
    WebDriver driver;
    By username = By.id("username");
    By password = By.id("password");
    By loginBtn = By.id("login");

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

    public void login(String user, String pass) {
        driver.findElement(username).sendKeys(user);
        driver.findElement(password).sendKeys(pass);
        driver.findElement(loginBtn).click();
    }
}

Why POM Is Widely Used

If a locator changes, it only needs updating in one page class rather than in every test that uses it — dramatically improving maintainability as a suite grows.

A Fuller Page Object With Waits

Real pages load slowly and change state, so page classes usually include explicit waits. Returning the next page object from an action keeps tests readable and shows how users move through the application.

In the version below, a few design decisions are worth noticing. Locators are private final, so tests cannot reach inside the page. There are two login methods because a successful login and a failed login lead to different screens, and each method returns the page the user would actually see next. The repeated typing steps live in one private helper, so a change to the form touches a single place.

public class LoginPage {
    private final WebDriver driver;
    private final WebDriverWait wait;

    private final By username = By.id("username");
    private final By password = By.id("password");
    private final By loginBtn = By.id("login");
    private final By error    = By.cssSelector(".alert-error");

    public LoginPage(WebDriver driver) {
        this.driver = driver;
        this.wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    public DashboardPage loginAs(String user, String pass) {
        typeCredentials(user, pass);
        return new DashboardPage(driver);
    }

    public LoginPage loginExpectingError(String user, String pass) {
        typeCredentials(user, pass);
        return this;
    }

    public String errorMessage() {
        return wait.until(ExpectedConditions.visibilityOfElementLocated(error)).getText();
    }

    private void typeCredentials(String user, String pass) {
        wait.until(ExpectedConditions.visibilityOfElementLocated(username)).sendKeys(user);
        driver.findElement(password).sendKeys(pass);
        driver.findElement(loginBtn).click();
    }
}

What the Test Looks Like

With the page object in place, the test reads like a short description of user behaviour. There are no locators in it at all.

public class LoginTest extends BaseTest {

    @Test
    public void validUserReachesDashboard() {
        DashboardPage dashboard = new LoginPage(driver).loginAs("riya", "Secret#123");
        Assert.assertEquals(dashboard.welcomeText(), "Welcome, riya");
    }

    @Test
    public void wrongPasswordShowsError() {
        String msg = new LoginPage(driver)
                .loginExpectingError("riya", "wrong")
                .errorMessage();
        Assert.assertEquals(msg, "Invalid username or password");
    }
}

Here BaseTest opens and closes the browser in setup and teardown methods, so every test class shares the same lifecycle code. If the login form later gains a "remember me" checkbox or a new button ID, only LoginPage changes; both tests keep working untouched.

Organising a POM Project

Folder or classResponsibility
pages/One class per page or reusable component (header, search bar)
pages/BasePageShared helpers such as waits, scrolling and safe clicks
tests/Test classes with assertions, grouped by feature
tests/BaseTestDriver setup, teardown and configuration
utils/ and test data filesReaders for config, test data and environment URLs

Selenium also offers PageFactory with @FindBy annotations as another way to declare elements. It works, but many teams now prefer plain By locators like the example above because they are simpler to debug and combine naturally with explicit waits.

Guidelines and Common Mistakes

  • Keep assertions in tests, not in page classes. Pages describe what can be done; tests decide what is correct.
  • Expose actions, not elements. A method like search("shoes") is better than returning a raw WebElement.
  • Avoid giant page classes. Split repeated sections, such as navigation menus, into component objects.
  • Never use fixed sleeps like Thread.sleep inside pages; use explicit waits on clear conditions.
  • Prefer stable locators such as IDs or dedicated test attributes over long XPath chains that break with layout changes.

A quick health check: if changing one button's locator forces edits in more than one file, the pattern is not being applied consistently yet.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course