Selenium · Browser automation foundations
How do you design a page object model that stays maintainable?
Short answer
A page object encapsulates one page or component's locators and user actions behind intention-revealing methods. Tests express behaviour and assertions, while page objects own DOM details and navigation.
The value of a page object is a single place to change when the UI changes. If a locator appears in ten tests, one markup change breaks ten tests. If it lives behind a method such as signIn(email, password), only the page object changes.
public class LoginPage {
private final WebDriver driver;
private final By email = By.id("email");
private final By password = By.id("password");
private final By submit = By.cssSelector("[data-testid='sign-in']");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public DashboardPage signIn(String user, String pass) {
driver.findElement(email).sendKeys(user);
driver.findElement(password).sendKeys(pass);
driver.findElement(submit).click();
return new DashboardPage(driver);
}
}- One page object per page or meaningful component, not per test
- Expose user intent such as signIn or addToCart, not individual clicks
- Return the next page object to make navigation explicit
- Keep assertions in tests so the page object does not decide correctness
- Avoid a god object that models the entire application in one class
- Share common header or navigation through composition rather than inheritance where possible
Page objects should not hide waits that the test needs to reason about. They can encapsulate synchronisation for their own actions, but they should not swallow failures or retry business logic. A page object that silently waits forever turns a clear assertion failure into a timeout.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Should page objects contain assertions?
- 2How do you model a shared header?
- 3What is the difference between a page object and a component object?
- 4When is a page object overkill?
Say it in the interview
A page object owns the locators and user actions for one page or component, behind methods that express intent. Tests call signIn and assert on the result, so a markup change updates one place instead of many tests. I return the next page object to make navigation explicit and keep assertions in the test. I avoid god objects and do not let page objects hide failures behind endless waits.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Selenium mock interview and get scored feedback on depth, clarity and confidence.