Manuals / Selenium WebDriver / Chapter 5

B · Stability · intermediate · Week 3–5 · Chapter 5 of 12

Page Object Model

POM keeps Selenium suites maintainable. Encapsulate locators and intent methods — tests read like scenarios.

Path progress
35%

Step 1 of 4

LoginPage class

Private locators, public methods: open(), login(user, pass), getErrorMessage().

LoginPage classDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →LoginPage classdrag · tap →Try thisdrag · tap →Follow the dashed drag · tap →

Example

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.username = (By.ID, "username")
        self.password = (By.ID, "password")
        self.submit = (By.CSS_SELECTOR, "button[type='submit']")
    def login(self, user, pwd):
        self.driver.find_element(*self.username).send_keys(user)
        self.driver.find_element(*self.password).send_keys(pwd)
        self.driver.find_element(*self.submit).click()

Do this now

Extract login from script into pages/LoginPage. Test calls loginPage.login("tomsmith", "SuperSecretPassword!").

Was this step clear?
Chapter learning outcomes
  • Page class structure
  • BasePage patterns (minimal)
  • Test layer thin

Clear these before you leave

Side quest

Sauce Demo POM

Optional: port login + inventory to Sauce Demo with POM.