</>

Technology

Selenium

Difficulty

Intermediate

Interview Question

What is Page Object Model or POM?

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