Introduction
Testing web applications can get messy when test code mixes with page details. This makes tests hard to read and maintain. The Page Object Model helps organize tests by separating page structure from test logic.
Imagine a restaurant kitchen where chefs have a recipe book. The recipe book tells them exactly how to prepare each dish without needing to know where every ingredient is stored. If the kitchen layout changes, only the recipe book needs updating, not the chefs' cooking steps.
┌───────────────┐ uses ┌───────────────┐
│ Test Code │──────────────────▶│ Page Object │
└───────────────┘ └───────────────┘
│ ▲
│ │
contains │ │ encapsulates
▼ │
┌───────────────┐
│ Page Elements │
└───────────────┘class LoginPage: def __init__(self, driver): self.driver = driver self.username_input = 'id=username' self.password_input = 'id=password' self.login_button = 'id=loginBtn' def enter_username(self, username): self.driver.find_element(self.username_input).send_keys(username) def enter_password(self, password): self.driver.find_element(self.password_input).send_keys(password) def click_login(self): self.driver.find_element(self.login_button).click() # Test code example login_page = LoginPage(driver) login_page.enter_username('user1') login_page.enter_password('pass123') login_page.click_login()
Page Object Model in software testing?login_page.get_title() return if the page title is "Welcome"?
class LoginPage:
def __init__(self, driver):
self.driver = driver
def get_title(self):
return self.driver.titleclass LoginPage:
def __init__(self, driver):
self.driver = driver
def click_login(self):
self.driver.find_element_by_id('loginBtn').click()Pass the test and this pattern moves unknown