Creating Page Object Model (POM) with Python

In test automation, keeping test scripts clean, maintainable, and reusable is crucial for long-term project success. That’s where the Page Object Model (POM) comes in. It’s a design pattern that improves the structure of your Selenium test code by separating page-specific operations and elements from test logic. When combined with Python and Selenium WebDriver, POM becomes a powerful strategy for building scalable automation frameworks.

In this blog, we’ll break down what POM is, why it’s important, and how to implement it using Python.


πŸ“˜ What is the Page Object Model?

The Page Object Model (POM) is a design pattern in Selenium automation that encapsulates the elements and interactions of a web page into a separate Python class. Each page of the application is represented by a Page Object class containing:

Web element locators

Page-specific methods (e.g., login, fill form)

This helps reduce code duplication and improves readability and maintainability.


✅ Benefits of Using POM

Separation of concerns: Keeps test logic and UI structure separate.

Reusability: Common actions (like login) can be reused across multiple test cases.

Maintainability: If the UI changes, only the Page Object needs updating.

Scalability: Makes it easy to scale test cases across large projects.


πŸ§ͺ Basic Folder Structure

Here's a recommended folder structure for a POM framework in Python:

cpp

Copy

Edit

project/

├── pages/

│   ├── login_page.py

│   └── dashboard_page.py

├── tests/

│   └── test_login.py

├── utils/

│   └── base_test.py

└── conftest.py (if using pytest)

🧱 Creating a Page Object Class

Here’s an example of a Page Object for a login page using Selenium in Python:


python

Copy

Edit

# pages/login_page.py


from selenium.webdriver.common.by import By


class LoginPage:

    def __init__(self, driver):

        self.driver = driver

        self.username_input = (By.ID, "username")

        self.password_input = (By.ID, "password")

        self.login_button = (By.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()


πŸ§ͺ Writing the Test Script

python



# tests/test_login.py


import time

from selenium import webdriver

from pages.login_page import LoginPage


def test_valid_login():

    driver = webdriver.Chrome()

    driver.get("https://example.com/login")

    

    login = LoginPage(driver)

    login.enter_username("testuser")

    login.enter_password("securepassword")

    login.click_login()


    time.sleep(2)

    assert "Dashboard" in driver.title

    driver.quit()

πŸ”§ Advanced Tips

Use base classes for common driver operations.

Integrate with pytest for powerful test management.

Use environment config files to manage test environments and credentials.

Add waits using WebDriverWait to handle dynamic content.


🏁 Conclusion

The Page Object Model is a best practice in Selenium test automation, and using it with Python ensures your tests are readable, maintainable, and scalable. By organizing your code using POM, you make your automation framework more robust and future-proof—critical qualities for any professional QA or test engineer. 

Learn Selenium with Pyhton Training Hyderabad

Read More:  Logging in Selenium Python Scripts
Read More:  Generating Test Reports in Selenium Python
Read More:  Automating Captcha with Selenium Python (Overview and Limits)on

Visit IHUB Talent Institute Hyderabad
Get Direction

Comments

Popular posts from this blog

How to Use Tosca's Test Configuration Parameters

Installing Java and Eclipse IDE for Selenium Automation

How Flutter Works Behind the Scenes