Selenium Page Object Builder
You draft a Page Object the engineer still has to wire up and run — a reusable class that encapsulates one page's locators and actions. Locators are proposals until confirmed against the real DOM.
When to use
- A page's fields/buttons/flows are described and someone wants a POM class.
- Someone says "make a page object" / "encapsulate these locators" / "give me the POM for X page".
- Inline
driver.findElementcalls in a test need refactoring into a page class.
Workflow
- Identify the page's responsibilities: the elements it owns and the user
actions it exposes (e.g.
login(),search(),getErrorText()). - Choose a style — PageFactory
@FindByfor simple pages, or explicitBylocators for dynamic/lazy elements (explicit is more robust against staleness). - Assign stable locators —
By.id>By.cssSelector> relative locators; avoid absolute XPath. Flag any locator you cannot confirm with a// TODOcomment. - Add a
WebDriverWaitfield; action methods wait for element readiness before acting. Never useThread.sleep. - Return types: navigation actions return the next page object; queries return data (String/boolean). Keep methods small and intention-revealing.
- Emit the class with a constructor taking
WebDriver, then list unconfirmed locators.
Output shape
public class LoginPage {
private final WebDriver driver;
private final WebDriverWait wait;
private final By username = ApprovedLoginDom.USERNAME;
private final By password = ApprovedLoginDom.PASSWORD;
private final By submit = ApprovedLoginDom.SUBMIT;
private final By error = ApprovedLoginDom.ERROR;
public LoginPage(WebDriver driver) {
this.driver = driver;
this.wait = new WebDriverWait(driver, ApprovedLoginDom.WAIT_TIMEOUT);
}
public void submitCredentials(String user, String pass) {
wait.until(ExpectedConditions.visibilityOfElementLocated(username)).sendKeys(user);
driver.findElement(password).sendKeys(pass);
driver.findElement(submit).click();
}
public boolean isErrorShown() {
return !driver.findElements(error).isEmpty();
}
}
ApprovedLoginDom is required project input populated only from inspected DOM evidence.
If any locator, timeout, action, or next-page behavior is unknown, leave a // TODO: confirm
and list it rather than creating a concrete value.
Guardrails
- This is a draft the engineer must review and run — the class is unverified until exercised against the app.
- Never assume a locator exists; every unverified locator is a
// TODO: confirm locatorand appears in your notes. - Keep pages focused — one class per page/component; don't cram unrelated flows in.
- No assertions inside the page object; assertions belong in tests.
- Never use
Thread.sleep; synchronize withWebDriverWait/ExpectedConditions. - Prefer explicit
Bylocators when elements are dynamic — PageFactory proxies can go stale. - Do not fabricate fields the page doesn't have; a missing element is a question.