As Selenium automation projects grow, maintaining test scripts becomes increasingly challenging. A simple automation script may work well for a few test cases, but larger applications can have hundreds of pages, elements and workflows.
If element locators and test logic are repeated across multiple scripts, even a small change in the application can require updates in several places.
The Page Object Model (POM) is a popular design pattern used in Selenium automation testing to solve this problem.
POM separates webpage-specific elements and interactions from test execution logic. This makes automation frameworks easier to maintain, reuse and expand.
In this tutorial, we will understand the Page Object Model, its advantages, project structure and implementation using Selenium WebDriver with Java and TestNG.
1. What Is Page Object Model in Selenium?
Page Object Model is a design pattern in which each application page or meaningful page component is represented by a separate Java class.
Each page class generally contains:
Web element locators.
Methods to interact with those elements.
Page-specific operations.
Reusable functionality.
For example, an e-commerce application may contain:
Login Page
Home Page
Product Page
Shopping Cart Page
Checkout Page
Each of these can be represented by a separate Java class.
Instead of writing the same element locators repeatedly in test cases, testers maintain them in their corresponding page classes.
2. Why Is Page Object Model Required?
Consider a login page containing username, password and login button elements.
Without POM, the same locators may be repeated in several test scripts.
If the username field's ID changes, multiple test cases may need modification.
With POM, the locator is maintained in one page class. Updating it there can benefit every test that uses that class.
Advantages of POM
Maintainability: Element locators are organised centrally.
Reusability: Page methods can be reused across test cases.
Readability: Test scripts become easier to understand.
Scalability: New pages and workflows can be added systematically.
Reduced duplication: Common page interactions do not need to be repeated.
Separation of responsibilities: Page interactions are separated from test execution and validation.
3. Typical Selenium POM Framework Structure
A Maven-based Selenium automation project may use the following structure:
SeleniumAutomationFramework
│
├── src/main/java
│ ├── pages
│ │ ├── LoginPage.java
│ │ ├── HomePage.java
│ │ └── ProductPage.java
│ │
│ └── utilities
│ └── DriverFactory.java
│
├── src/test/java
│ ├── tests
│ │ ├── LoginTest.java
│ │ └── ProductTest.java
│ │
│ └── base
│ └── BaseTest.java
│
├── pom.xml
└── testng.xmlThe exact folder structure may vary depending on project requirements.
4. Creating a Login Page Class
Let us create a simple Login Page using Selenium WebDriver.
LoginPage.java
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage {
WebDriver driver;
By username =
By.id("username");
By password =
By.id("password");
By loginButton =
By.id("login");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void enterUsername(String user) {
driver.findElement(username).sendKeys(user);
}
public void enterPassword(String pass) {
driver.findElement(password).sendKeys(pass);
}
public void clickLogin() {
driver.findElement(loginButton).click();
}
public void login(String user, String pass) {
enterUsername(user);
enterPassword(pass);
clickLogin();
}
}This class contains the locators and operations required to interact with the login page.
The constructor receives the WebDriver instance so that the page object uses the browser session managed by the test framework.
5. Creating a Base Test Class
A Base Test class can manage common browser setup and cleanup.
BaseTest.java
package base;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
public class BaseTest {
protected WebDriver driver;
@BeforeMethod
public void setUp() {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://example.com");
}
@AfterMethod(alwaysRun = true)
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}This example creates a fresh browser session before each test method and closes it afterward.
For larger projects, browser creation can be moved into a reusable Driver Factory.
6. Creating a Login Test Using POM
Now we can use the LoginPage class in a TestNG test.
LoginTest.java
package tests;
import base.BaseTest;
import pages.LoginPage;
import org.testng.Assert;
import org.testng.annotations.Test;
public class LoginTest extends BaseTest {
@Test
public void verifySuccessfulLogin() {
LoginPage loginPage =
new LoginPage(driver);
loginPage.login(
"testuser",
"Test@123"
);
Assert.assertTrue(
driver.getCurrentUrl().contains("dashboard")
);
}
}The test method does not need to know how the username field or login button is located.
It simply calls the page method and validates the result.
The URL and element locators are illustrative; they should be replaced with those of the application under test.
7. What Is Page Factory in Selenium?
Page Factory is a Selenium support feature that provides a convenient way to initialise page elements using annotations such as @FindBy.
Example:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class LoginPage {
@FindBy(id = "username")
WebElement username;
@FindBy(id = "password")
WebElement password;
@FindBy(id = "login")
WebElement loginButton;
public LoginPage(WebDriver driver) {
PageFactory.initElements(driver, this);
}
public void login(String user, String pass) {
username.sendKeys(user);
password.sendKeys(pass);
loginButton.click();
}
}Page Factory is optional. A POM framework can also use ordinary By locators and findElement() calls.
Modern Selenium frameworks often prefer explicit locator management because it makes element lookup and wait behaviour easier to control.
8. Difference Between POM and Page Factory
| Feature | Page Object Model | Page Factory |
|---|---|---|
| Type | Design pattern | Selenium support feature |
| Purpose | Organises page-specific code | Initialises page elements |
| Element definition | Commonly uses By locators | Commonly uses @FindBy |
| Requirement | Can be implemented without Page Factory | Used within a page-object design |
| Flexibility | High | Provides a convenient annotation-based approach |
Page Factory does not replace POM. It is one optional way of implementing page objects.
9. Best Practices for Building a POM Framework
Maintain separate page classes for application pages or components.
Keep locators and page interactions inside page classes.
Keep assertions and test scenarios primarily in test classes.
Use meaningful method and class names.
Avoid duplicating browser setup across test classes.
Use explicit waits for dynamic elements.
Maintain reusable utility methods carefully.
Keep test data separate from test logic.
Use Maven for dependency management.
Integrate the framework with Git and CI/CD tools.
10. Common POM Interview Questions
1. What is Page Object Model in Selenium?
POM is a design pattern that represents application pages or components as separate classes containing locators and interaction methods.
2. What are the benefits of POM?
It improves maintainability, reusability, readability and scalability while reducing duplicated code.
3. Is Page Factory mandatory for POM?
No. Page Factory is optional. POM can be implemented using standard Selenium locators and WebDriver methods.
4. Where should assertions be written in a POM framework?
Assertions are generally placed in test classes so that page objects focus on page interactions and the tests focus on validation.
5. Can POM be used with TestNG?
Yes. POM is commonly combined with TestNG to organise page interactions and manage test execution.
6. Can POM be used with Selenium Python?
Yes. The Page Object Model is a design pattern and can be implemented in Python using Selenium and frameworks such as PyTest.
Learn Selenium Automation Framework Development with Palium Skills
Learning Selenium commands is only the first step toward becoming an automation test engineer. Developing a reusable automation framework is an important practical skill.
Palium Skills offers Selenium and Playwright automation testing training covering:
Selenium WebDriver with Java and Python.
Page Object Model and framework development.
TestNG and PyTest.
XPath and CSS Selector.
Data-driven automation testing.
API testing and integration.
Maven, Git and Jenkins.
Real-time automation projects.
Interview preparation and career guidance.
Training modes: Classroom training at South Kolkata and Salt Lake centres, plus instructor-led live online sessions for learners across India.
Contact Palium Skills
Website: www.paliumskills.com
Call: 8420594969 / 7044871915
WhatsApp: 9903130500
Email: info@paliumskills.com
South Kolkata Centre: 1st Floor, Sheeba Bhavan, 1/22 Poddar Nagar (Near South City Mall), Kolkata – 700068.
Salt Lake Centre: 5th Floor, RDB Boulevard, Block GP, Salt Lake Electronic Complex, Kolkata – 700091.
Build practical Selenium automation frameworks with Palium Skills through classroom and live online training.