What is a Page Object Model in Selenium? Benefits, Examples & Best Practices
Selenium is an extremely popular framework for web test automation. Selenium interacts directly with a browser through WebDriver, which is why it is one of the most popular choice of QA teams worldwide. However, there is one catch here, as projects evolve, the nature of test scripts evolves as well. Having hundreds of locators and actions for multiple tests becomes a nightmare in milliseconds.
And this is the point where the Page Object Model in Selenium (POM) comes into the picture. The design pattern organizes the test automation code and maintains clean, scalable, and manageable automation framework code. POM made element locators centralized into separate page classes instead of sprinkling them throughout your tests making them easy to update. QA leaders who are creating long-term automation strategies may find POM to be the difference between brittle scripts and a maintainable test suite.
What is the Page Object Model (POM) in Selenium?
So, what is POM? In simple terms, it is a pattern that isolates the test logic from the locators and actions on the page. As per the Selenium Page Object Design Pattern, we will have a class for each web page of the application and the elements on that page will be defined as variables of that class. The interactions, such as clicking buttons or typing in a textbox, are defined as methods.
This helps to isolate the page functionality so if your page layout changes, you only need to change the appropriate page class and not hundreds of test scripts. This is the reason that POM is considered to be the main pillar in developing scalable Selenium automation frameworks.
The POM framework in Selenium promotes modularity and reusability, which is quite contrary to ad-hoc scripts. It serves as a template, creating a unified standard of writing automation code across teams. For organizations with a distributed QA and DevOps team, this standardization is crucial to decrease onboarding time and enhance collaboration.
Key Features of Page Object Model
The POM framework in Selenium comes with several distinct features that make it a preferred choice:
- Reusability of code – Locators and methods are written once and reused across tests.
- Readability & maintainability – Page classes clearly represent UI functionality, making scripts easier to read.
- Easy debugging – Centralized locators mean debugging is faster when elements change.
- Enhanced test coverage – By abstracting page behavior, testers can focus on building richer test scenarios.
- Framework integration – Works smoothly with TestNG, JUnit, and even BDD frameworks like Cucumber.
Page Object Model Structure in Selenium Projects
A well-structured POM framework has three key components:
- Page classes (Object Repository): These represent application pages. Each class contains locators (using By or @FindBy) and methods for interactions.
- Test classes: These call the methods from page classes to execute the test cases.
- Folder hierarchy: A typical Selenium project might look like this:
src/test/java/
├── pages/
│ ├── LoginPage.java
│ └── DashboardPage.java
├── tests/
│ ├── LoginTest.java
└── utils/
└── BaseTest.java
This folder hierarchy enforces a clear separation of concerns. Developers know exactly where to update locators, and testers know where to add or modify test cases.
Benefits of Using Page Object Model
This is the reason why POM is the preferred choice of most QA teams:
- Reduced maintenance overhead: Centralized locators help in less maintenance when there are UI changes.
- Less code duplication: Prevent duplicate locator logic at multiple places.
- Better Readability: The structure of the tests is easily understood by both developers and testers.
- Scalability with ease: New test cases can be imported with ease.
- Framework Compatibility: Integrates with ease to JUnit, TestNG or any other framework.
- Automated testing promotes team collaboration: As business analysts, developers and testers can rally behind the same design for automation.
How to Create Page Object Model in Selenium (Step by Step)
Let’s walk through the process:
- Create a class for each page – Example: LoginPage.java.
- Define locators – Use By or @FindBy with PageFactory in Selenium.
- Write methods – Define actions such as login(username, password).
- Call methods in test classes – The test script should never directly use locators.
Setting up dependencies
If you’re using Maven, add Selenium WebDriver dependencies in pom.xml like this:
org.seleniumhq.selenium selenium-java 4.20.0
This is how you add Selenium WebDriver dependencies in pom.xml to get started.
Want to see how Page Object Model-based tests can fit into a full CI/CD flow? Check out ACCELQ’s guide on CI/CD Pipeline with Jenkins: Build, Test & Deploy
Example of Page Object Model in Selenium WebDriver (Login Page)
Here’s a simple example of Page Object Model in Selenium Java:
Page Class (LoginPage.java)
public class LoginPage {
WebDriver driver;
@FindBy(id="username")
WebElement username;
@FindBy(id="password")
WebElement password;
@FindBy(id="loginBtn")
WebElement loginBtn;
public 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();
}
}
Test Class (LoginTest.java)
public class LoginTest {
WebDriver driver;
@BeforeMethod
public void setup() {
driver = new ChromeDriver();
driver.get("https://example.com/login");
}
@Test
public void testValidLogin() {
LoginPage loginPage = new LoginPage(driver);
loginPage.login("admin", "password123");
// Add assertion for successful login
}
}
Flow explained: The LoginPage class encapsulates all login-related functionality, while LoginTest simply uses that functionality.
Page Object Model Best Practices
Follow these Page Object Model best practices to ensure success:
- One class per page to keep things organized.
- Avoid embedding business logic inside page classes.
- Use clear, descriptive method names like clickLoginButton().
- Use PageFactory class to manage locators with @FindBy.
- Combine POM with TestNG/JUnit for structure and reporting.
- Keep methods short and intuitive to avoid bloated classes.
- Maintain consistency in naming conventions across all page objects.
Common Mistakes to Avoid
Some pitfalls to watch out for:
- Mixing test logic with page object classes – defeats the purpose of abstraction.
- Overcomplicating methods – keep page methods simple.
- Not updating page objects when the UI changes – leads to failing tests.
- Skipping reviews – page objects should be peer-reviewed like application code.
Page Object Model vs Other Frameworks
Let’s compare POM with other test automation frameworks:
Page Object Model vs Data Driven framework in Selenium
- POM: Focuses on separating locators and actions.
- Data Driven: Focuses on separating test data from scripts.
- Use case: Combine both for data-driven POM frameworks where data drives execution.
Page Object Model vs Keyword Driven framework
- Keyword Driven: Relies on predefined keywords for test execution.
- POM: Relies on page classes and object repositories.
- Use case: POM is simpler for UI-heavy projects; Keyword Driven is better for non-technical testers.
Hybrid frameworks
Many organizations use a hybrid approach, combining POM with data-driven and keyword-driven frameworks to maximize flexibility. This approach is common in enterprise testing where multiple application types and testing roles exist.
Conclusion
The Page Object Model in Selenium is more than just a design choice, it’s a necessity for building scalable, maintainable, and collaborative automation frameworks. By abstracting locators and actions into page classes, teams reduce duplication, simplify debugging, and accelerate test development.
ACCELQ, with its AI-powered, codeless automation, takes the essence of POM even further by abstracting test logic into reusable assets without writing code. Whether you’re starting small or managing large enterprise suites, implementing POM is the first step to smarter Selenium automation.
🤖 Meet Your Personal AI AUTOPILOT!
Testing at Hyperspeed Starts Here!
Try Now
Prashanth Punnam
Sr. Technical Content Writer
With over 8 years of experience transforming complex technical concepts into engaging and accessible content. Skilled in creating high-impact articles, user manuals, whitepapers, and case studies, he builds brand authority and captivates diverse audiences while ensuring technical accuracy and clarity.
You Might Also Like:
Top 9 Principles of Automation Testing
Top 9 Principles of Automation Testing
Migration From TestProject to ACCELQ
Migration From TestProject to ACCELQ
Tips to create the right Data Driven Test Strategy

