Page Object Model Interview Questions & Answers

Page Object Model is a design pattern in Selenium automation where each web page is represented as a separate class.
The class contains:

  • Web elements of the page
  • Methods to perform actions on those elements

It helps improve:

  • Code reusability
  • Maintainability
  • Readability

POM is used to:

  • Reduce code duplication
  • Separate test logic from page elements
  • Make framework easy to maintain
  • Handle UI changes easily
  • Improve scalability of automation framework

Main components are:

  1. Page Class → Contains locators and methods
  2. Test Class → Contains test cases
  3. Object Repository → Stores web element locators

Advantages:

  • Better code organization
  • Easy maintenance
  • Reusable methods
  • Improved readability
  • Reduced duplication
  • Easy debugging

Disadvantages:

  • Initial setup takes time
  • More classes need to be created
  • Not suitable for very small projects

If a locator changes in UI, we update it only in the Page Class instead of updating in all test cases.

Page Factory is an inbuilt Selenium class used to implement POM easily using annotations like @FindBy.

Example:

@FindBy(id="username")
WebElement userName;
POMPage Factory
Uses normal findElement()Uses @FindBy annotations
Elements initialized manuallyElements initialized automatically
More coding requiredLess coding required
Slower compared to Page FactoryFaster initialization

@FindBy is used to locate web elements in Page Factory.

Example:

@FindBy(xpath="//input[@id='email']")
WebElement email;

It initializes all web elements present in the Page Class.

Example:

PageFactory.initElements(driver, this);

Example:

public class LoginPage {
   WebDriver driver;
   @FindBy(id="username")
   WebElement username;
   @FindBy(id="password")
   WebElement password;
   @FindBy(id="login")
   WebElement loginBtn;
   LoginPage(WebDriver driver){
       this.driver = driver;
       PageFactory.initElements(driver, this);
   }

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

Constructor is used to:

  • Initialize WebDriver
  • Initialize web elements using PageFactory

Page Classes should only contain page elements and methods.
Test data should be stored separately for better maintainability.

Yes.
We can implement POM using normal findElement() methods without using Page Factory.

Page ClassTest Class
Contains locators and methodsContains test cases
Represents webpageExecutes validations
ReusableUses page methods

Common methods like login, click, search, etc. can be reused across multiple test cases.

POM is commonly used in:

  • Hybrid Framework
  • Data-Driven Framework
  • Keyword-Driven Framework
  • Keep locators private
  • Use meaningful method names
  • Avoid hard coding
  • Use reusable methods
  • Maintain separate test and page classes

Object Repository is a place where all web element locators are stored.

Example:

@FindBy(id="login")
WebElement loginBtn;

Dynamic elements can be handled using:

  • Dynamic XPath
  • Explicit waits
  • Relative locators

Example:

@FindBy(xpath="//button[contains(text(),'Login')]")
WebElement loginBtn;

Yes.
POM is commonly integrated with TestNG for test execution, assertions, and reporting.

Because it follows:

  • Separation of concerns
  • Encapsulation
  • Reusability
  • Maintainability

Web elements are hidden inside Page Classes and accessed only through methods.

Example:

public void clickLogin(){
   loginBtn.click();
}

Waits are usually implemented inside page methods using Explicit Wait.

Example:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOf(loginBtn));

Typical structure:

src/test/java

  ├── pages

  ├── tests

  ├── utilities

  ├── base

  └── reports

Create separate Page Classes for each webpage.

Example:

  • LoginPage.java
  • HomePage.java
  • CartPage.java

Base Class contains common setup code like:

  • Browser launch
  • Driver initialization
  • Wait setup
  • Common utilities

Methods that can be used multiple times across tests.

Example:

click()

sendKeys()

selectDropdown()

In large projects:

  • UI changes happen frequently
  • Multiple testers work together
  • Reusable components are needed

POM helps manage all these efficiently.

In real projects:

  • Every page has separate class
  • Test cases call page methods
  • Framework becomes modular and scalable
  • Maintenance becomes easier when UI changes occur