Manuals / Selenium WebDriver / Ch 5

B · StabilityIntermediate65 min read

5. Page Object Model

Selenium WebDriver · 48 pages source format

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

What you'll learn

  • Page class structure
  • BasePage patterns (minimal)
  • Test layer thin

LoginPage class

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

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!").

Clear?

Secure Area flow

LoginPage + SecureAreaPage. Assert flash message after login.

Do this now

Two-page flow with POM. Two tests using same LoginPage.

Clear?

Base driver fixture

pytest fixture or @BeforeEach starts driver, yields, quits. DRY without hiding failures.

Do this now

conftest.py with driver fixture. All tests use it.

Clear?

ARCHITECTURE.md

pages/, tests/, conftest.py, config — document for onboarding.

Do this now

Write ARCHITECTURE.md with folder tree and naming rules.

Clear?

Checklist