</>

Technology

Selenium

Difficulty

Intermediate

Interview Question

What is Page Object Model or POM?

POM is a Selenium design pattern where each web page has a dedicated class containing its locators and interaction methods.

Answer

Page Object Model (POM) is a design pattern in Selenium that creates a framework for maintaining and organizing automation scripts.

Core concept:

  • For each page of the application, a separate Java class is created
  • The class contains all web elements (locators) belonging to that page
  • The class contains all methods (actions) that can be performed on that page
  • Test scripts are maintained in separate files and call these page object methods

Why use POM:

  • Separates test logic from page interaction logic
  • Locator changes require editing only one class — not every test
  • Methods are reusable across multiple test classes
  • Makes tests readable like plain English

Example structure:

CODE
src/
├── pages/
│   ├── LoginPage.java      ← Web elements + methods for Login page
│   ├── DashboardPage.java  ← Web elements + methods for Dashboard
│   └── SearchPage.java
└── tests/
    ├── LoginTest.java      ← Calls LoginPage methods
    └── SearchTest.java

LoginPage.java:

Java
public class LoginPage {
    private By username = By.id("username");
    private By password = By.id("password");
    private By loginBtn = By.id("loginBtn");

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

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

Follow AutomateQA

Related Topics