๐ Page Object Model (POM)
POM design pattern, PageFactory, @FindBy annotations, and maintainable framework structure.
What is Page Object Model (POM)?
Page Object Model (POM) is a design pattern in test automation where each web page is represented by a corresponding Page Class. The Page Class holds element locators and action methods, separating test scripts from UI layout changes.
Advantages of POM
Key architectural benefits of adopting Page Object Model in enterprise automation frameworks.
| Advantage | Description |
|---|---|
| Code Reusability | Page methods (e.g. login) are written once and reused across multiple test cases. |
| Easy Maintenance | If UI locator changes, update it only in the Page Object class without touching test scripts. |
| Clean Readability | Test classes contain clean business logic instead of low-level `findElement` locators. |
Implementing Page Object Class
Building a LoginPage class using encapsulation.
LoginPage.java
Java
package com.example.pages; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; public class LoginPage { private WebDriver driver; // Locators private By usernameInput = By.id("username"); private By passwordInput = By.id("password"); private By loginButton = By.id("loginBtn"); public LoginPage(WebDriver driver) { this.driver = driver; } // Page Action Methods public void enterUsername(String user) { driver.findElement(usernameInput).sendKeys(user); } public void enterPassword(String pass) { driver.findElement(passwordInput).sendKeys(pass); } public void clickLogin() { driver.findElement(loginButton).click(); } }
Clean Automation Test Class
Executing test scenarios using Page Object classes.
LoginTest.java
Java
package com.example.tests; import com.example.pages.LoginPage; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class LoginTest { @Test public void testValidLogin() { WebDriver driver = new ChromeDriver(); driver.get("https://app.example.com/login"); LoginPage loginPage = new LoginPage(driver); loginPage.enterUsername("admin"); loginPage.enterPassword("secret"); loginPage.clickLogin(); driver.quit(); } }