What is the Page Object Model (POM)?
Options
- Each page is a class with locators and actions (Correct option)
- A way to store photos that tests will compare
- A Maven plugin that builds the pages of the app
- A database schema that describes the pages
Answer
A design pattern where each page (or component) is a class holding its locators and actions, so tests read like user steps and a UI change is fixed in one place
Why
Without POM, locators are scattered across tests; when a button's id changes you edit dozens of tests.
With POM, LoginPage has methods like loginAs(user, pass); tests call them and never mention locators. If the UI changes, you fix the page class once.
Tests become readable: loginPage.loginAs("ana", "secret"); assertTrue(dashboard.isVisible());
public class LoginPage {
private final WebDriver driver;
public LoginPage(WebDriver d) { this.driver = d; }
public DashboardPage loginAs(String user, String pass) {
driver.findElement(By.id("user")).sendKeys(user);
driver.findElement(By.id("pass")).sendKeys(pass);
driver.findElement(By.id("login")).click();
return new DashboardPage(driver);
}
}Remember
POM = one class per page; locators live there, not in tests.