Page Object Model (POM) is a design pattern where each web page is represented as a separate Java class. The class stores all the page''s locators and the methods to interact with them.
Benefits:
- ✓Reusable code — one class for all tests using that page
- ✓Easy maintenance — locator changes in one place only
- ✓Readable tests — test code reads like plain English
- ✓Separation of concerns — test logic vs page interaction
Page class structure:
Java
public class LoginPage {
private WebDriver driver;
private By usernameField = By.id("username");
private By passwordField = By.id("password");
private By loginButton = By.id("loginBtn");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void login(String username, String password) {
driver.findElement(usernameField).sendKeys(username);
driver.findElement(passwordField).sendKeys(password);
driver.findElement(loginButton).click();
}
}
Test class using POM:
Java
@Test
public void testLogin() {
LoginPage loginPage = new LoginPage(driver);
loginPage.login("admin", "pass123");
Assert.assertEquals(driver.getTitle(), "Dashboard");
}
