0
0
Testing Fundamentalstesting~6 mins

Page Object Model concept in Testing Fundamentals - Full Explanation

Choose your learning style9 modes available
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.
Explanation
Separation of Concerns
Page Object Model (POM) separates the code that interacts with the web page from the test code. It creates a class or object for each page that contains methods to interact with page elements. This keeps tests clean and focused on behavior, not page details.
POM keeps page details separate from test logic to improve clarity and maintenance.
Page Classes
Each web page or part of a page is represented by a class or object. This class holds locators for elements like buttons and fields, and methods to perform actions like clicking or typing. Tests use these classes instead of raw selectors.
Page classes encapsulate element locators and actions for a specific page.
Reusability and Maintainability
If the page changes, only the page class needs updating, not every test. This reduces duplicated code and makes tests easier to maintain. It also allows reusing page classes across multiple tests.
POM improves test maintainability by centralizing page changes in one place.
Improved Readability
Tests become easier to read because they use meaningful method names like loginPage.enterUsername() instead of raw selectors. This makes tests more understandable for anyone reading them.
POM makes tests more readable by using clear, descriptive methods.
Real World Analogy

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.

Separation of Concerns → Chefs focusing on cooking steps, not ingredient storage details
Page Classes → Recipe book pages describing how to prepare each dish
Reusability and Maintainability → Updating the recipe book when kitchen layout changes, not the cooking steps
Improved Readability → Clear recipe instructions that anyone can follow easily
Diagram
Diagram
┌───────────────┐       uses       ┌───────────────┐
│   Test Code   │──────────────────▶│  Page Object  │
└───────────────┘                   └───────────────┘
                                      │       ▲
                                      │       │
                           contains   │       │  encapsulates
                                      ▼       │
                              ┌───────────────┐
                              │ Page Elements │
                              └───────────────┘
Diagram showing test code using page objects which encapsulate page elements.
Key Facts
Page Object ModelA design pattern that separates web page structure from test code by using page classes.
Page ClassA class representing a web page with element locators and interaction methods.
LocatorA way to find elements on a web page, like by ID, class, or XPath.
Test MaintainabilityThe ease with which test code can be updated and kept working over time.
ReusabilityUsing the same code components in multiple tests without duplication.
Code Example
Testing Fundamentals
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()
OutputSuccess
Common Confusions
Believing Page Object Model means writing tests inside page classes.
Believing Page Object Model means writing tests inside page classes. Page classes only contain page details and actions; test logic stays separate in test files.
Thinking POM eliminates the need to understand selectors.
Thinking POM eliminates the need to understand selectors. Selectors are still needed but are centralized inside page classes for easier management.
Summary
Page Object Model organizes test code by separating page details into page classes.
It improves test readability and makes maintenance easier by centralizing page changes.
Tests interact with page classes through clear methods, not raw selectors.