Page Object Model in Selenium Java
When every test has its own By.id("email"), one change on the login page means editing 40 tests. The Page Object Model (POM) fixes this: each page gets one class that knows its locators and actions. Tests only call those methods.
The rules
- One class per page (or per big component, like a header).
- Locators are private fields inside the page class.
- Public methods describe what a user does:
logIn(), notclickButton3(). - Page classes do not contain assertions. Tests assert.
BasePage: shared helpers
package pages;
public abstract class BasePage {
protected final WebDriver driver;
protected final WebDriverWait wait;
protected BasePage(WebDriver driver) {
this.driver = driver;
this.wait = new WebDriverWait(driver, Duration.ofSeconds(10));
}
protected void click(By locator) {
wait.until(ExpectedConditions.elementToBeClickable(locator)).click();
}
protected void type(By locator, String text) {
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
el.clear();
el.sendKeys(text);
}
protected String textOf(By locator) {
return wait.until(ExpectedConditions.visibilityOfElementLocated(locator)).getText();
}
}
LoginPage
package pages;
public class LoginPage extends BasePage {
private static final String URL = "https://testqualityhub.com/practice/login/";
private final By email = By.id("email");
private final By password = By.id("password");
private final By submit = By.id("submit");
private final By error = By.cssSelector("[data-testid='login-error']");
public LoginPage(WebDriver driver) {
super(driver);
}
public LoginPage open() {
driver.get(URL);
return this;
}
public AccountPage logInAs(String user, String pass) {
type(email, user);
type(password, pass);
click(submit);
return new AccountPage(driver);
}
public String errorMessage() {
return textOf(error);
}
}
AccountPage
package pages;
public class AccountPage extends BasePage {
private final By name = By.cssSelector("[data-testid='account-name']");
private final By logout = By.cssSelector("[data-testid='logout']");
public AccountPage(WebDriver driver) {
super(driver);
}
public String userName() {
return textOf(name);
}
public LoginPage logOut() {
click(logout);
return new LoginPage(driver);
}
}
The test, now short and readable
public class LoginTest extends BaseTest {
@Test(groups = "smoke")
public void validUserCanLogIn() {
AccountPage account = new LoginPage(driver)
.open()
.logInAs("demo@testqualityhub.com", "Demo@1234");
Assert.assertEquals(account.userName(), "Demo Tester");
}
}
BaseTest holds the @BeforeMethod and @AfterMethod that start and quit the browser, so no test repeats them.
Why methods return the next page
logInAs() returns an AccountPage because that is where the user lands. This is called a fluent page object. The code reads like the user journey, and the IDE only suggests actions that make sense on the current page.
Interview tip: "What is POM and why use it?" is asked in almost every automation interview. Answer with the maintenance problem first, then the rules above.
All 9 lessons in Selenium with Java
- What Is Selenium WebDriver?
- Set Up Java, Maven and Selenium
- Write Your First Selenium Test
- Selenium Locators: ID, CSS, XPath and More
- Clicks, Typing, Dropdowns, Alerts, Frames and Windows
- Waits in Selenium: Implicit, Explicit and Fluent
- TestNG for Selenium: Annotations, Assertions and Data
- Page Object Model in Selenium Java
- Selenium Framework Structure, Reports and CI