UI automation · core
Locators and the page object pattern
A stable locator survives a page change. A page object keeps locators and user actions in one class, so a UI change is fixed in one place.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Prefer stable attributes: id, name, a data-testid, or a short relative locator tied to meaning. Avoid absolute XPath from /html/body, index locators like //tr[2]/td[3], and styling classes.
- XPath for a row: //table[@id="orders"]//tr[td[normalize-space()="ORD-1042"]]//button[normalize-space()="Cancel"]. normalize-space() copes with extra spaces and nested tags.
- Standard CSS cannot select by text. contains(text(),"ORD-104") also matches ORD-1041, so match the full value.
- Best long term: ask developers for test attributes such as data-order and data-testid.
- A page object is a class with private By locators, a constructor that takes the WebDriver, and methods named for user actions (loginAs, getErrorMessage).
- A method that moves to another page returns the next page object (HomePage); a failed login returns LoginPage.
- Keep assertions out of page objects: pages describe the UI, tests decide pass or fail. A small "is loaded" check is fine.
- Page Factory (@FindBy) lazy proxies can go stale and hide waits, so many teams use plain By locators with explicit waits. Never use public static WebDriver.
The red pen
Common traps: mistake → fix
Mistake: The browser generated this XPath, so it is good
Fix: generated absolute paths break with any layout change.
Mistake: One method per element (clickButton1) is a page object
Fix: methods should describe user actions.
Mistake: Assertions belong in page objects
Fix: they make pages hard to reuse; keep them in tests.
Easy to hard
Questions interviewers ask, easy to hard
- Write a locator for the Cancel button in the row of order ORD-1042.
- What is a page object and why use one?
- Why do many teams avoid Page Factory now?
- What would you ask the developers to add to make locators stable?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.