Page Object Model Design Pattern in Selenium Java
When it comes to test automation, maintaining clean, readable, and reusable code is essential for long-term success. As UI tests grow in size and complexity, managing locators and test flows can quickly become chaotic. That’s where the Page Object Model (POM) design pattern comes in. Widely used in Selenium-based test automation, POM promotes modularity, reduces code duplication, and improves maintainability.
In this blog, we’ll explore what the Page Object Model is, why it's beneficial, and how to implement it effectively using Selenium with Java.
What is Page Object Model (POM)?
Page Object Model is a design pattern that creates an object repository for storing all web elements of a particular web page. In POM, each web page of the application is represented as a class in Java. The elements and actions (methods) on that page are encapsulated in that class.
So, instead of writing Selenium code directly in your test scripts, you use methods from Page Object classes to interact with the application.
Why Use Page Object Model?
Here are some benefits of using POM in Selenium automation:
- Improved Readability: Test scripts become cleaner and easier to understand.
- Better Maintainability: UI changes require updates only in one place—the relevant page class.
- Reusability: Common actions and elements can be reused across different test cases.
- Separation of Concerns: Test logic and page structure are separated, leading to more modular code.
Basic Structure of POM in Selenium Java
Here’s a breakdown of how POM is typically structured:
- Page Classes – Contain locators and methods for interacting with a specific page.
- Test Classes – Contain test logic using page classes.
- Base Class (optional) – Contains common setup like browser initialization and teardown.
Example: Login Page with POM
1. LoginPage.java
java
public class LoginPage {
WebDriver driver;
// Constructor
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
// Locators
@FindBy(id = "username")
WebElement usernameField;
@FindBy(id = "password")
WebElement passwordField;
@FindBy(id = "loginBtn")
WebElement loginButton;
// Actions
public void enterUsername(String username) {
usernameField.sendKeys(username);
}
public void enterPassword(String password) {
passwordField.sendKeys(password);
}
public void clickLogin() {
loginButton.click();
}
public void loginAs(String username, String password) {
enterUsername(username);
enterPassword(password);
clickLogin();
}
}
2. LoginTest.java
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.loginAs("testuser", "securepassword");
// Assertions go here
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}
Best Practices for POM
- Keep locators private and actions public in page classes.
- Use PageFactory for cleaner initialization of WebElements.
- Avoid including assertions in page classes—keep them in test classes.
- Modularize components (e.g., header, footer) as separate classes when reused across pages.
Conclusion
The Page Object Model is a powerful design pattern that brings clarity, reusability, and maintainability to your Selenium test automation projects. Especially in larger applications, using POM with Java ensures your test suite remains scalable and easier to manage over time. By adopting POM, you not only write better code but also create a more efficient and robust testing framework.
Learn Selenium with Java Course in Hyderabad
Read More: Using Actions Class in Selenium Java for Mouse and Keyboard Events
Visit Quality Thought Training Institute Hyderabad
Get Direction
Comments
Post a Comment