A practical example of using the Page Object Model (POM) in Selenium with Python.
This example demonstrates how to structure your code using POM to create a clean and maintainable automation framework.
Practical Example: Using Page Objects in Selenium with Python
1. Project Structure
First, let’s establish a directory layout for our Selenium framework:/selenium-framework |-- /src | |-- /pageobjects | | |-- login_page.py | | |-- home_page.py | |-- /utils | | |-- browser_factory.py | | |-- config_utils.py |-- /tests | |-- test_login.py |-- requirements.txt |-- .gitignore |-- README.md
2. Page Object Model (POM)
The Page Object Model encapsulates the details of the web pages and the actions that can be performed on them. Each page is represented as a class.Example: login_page.py
pythonfrom selenium.webdriver.common.by import By from selenium.webdriver.remote.webdriver import WebDriver class LoginPage: def __init__(self, driver: WebDriver): self.driver = driver self.username_field = (By.ID, "username") self.password_field = (By.ID, "password") self.login_button = (By.ID, "loginButton") def enter_username(self, username: str): self.driver.find_element(*self.username_field).send_keys(username) def enter_password(self, password: str): self.driver.find_element(*self.password_field).send_keys(password) def click_login(self): self.driver.find_element(*self.login_button).click()
Example: home_page.py
pythonfrom selenium.webdriver.common.by import By from selenium.webdriver.remote.webdriver import WebDriver class HomePage: def __init__(self, driver: WebDriver): self.driver = driver self.welcome_message = (By.TAG_NAME, "h1") def get_welcome_message(self): return self.driver.find_element(*self.welcome_message).text
3. Utilities
Utility classes help manage common functionalities across your tests, such as browser setup and configuration management.Example: browser_factory.py
pythonfrom selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager def start_browser(browser: str): if browser.lower() == "chrome": driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install())) else: raise ValueError("Browser not supported.") driver.maximize_window() return driver
Example: config_utils.py
pythonimport configparser class ConfigUtils: def __init__(self): self.config = configparser.ConfigParser() self.config.read('config.properties') def get_property(self, section: str, key: str) -> str: return self.config.get(section, key)
4. Test Scripts
Now we can write our test scripts using the Page Object Model.Example: test_login.py
pythonimport pytest from selenium import webdriver from src.pageobjects.login_page import LoginPage from src.pageobjects.home_page import HomePage from src.utils.browser_factory import start_browser from src.utils.config_utils import ConfigUtils @pytest.fixture(scope="module") def setup(): config = ConfigUtils() driver = start_browser(config.get_property('Settings', 'browser')) driver.get(config.get_property('Settings', 'url')) yield driver driver.quit() def test_login(setup): driver = setup login_page = LoginPage(driver) login_page.enter_username("testuser") login_page.enter_password("password") login_page.click_login() home_page = HomePage(driver) assert home_page.get_welcome_message() == "Welcome, testuser!"
5. Configuration Management
Example: config.properties
[Settings] browser = chrome url = https://example.com
6. Dependency Management
Example: requirements.txt
selenium==4.23.1 webdriver-manager==3.8.6 pytest==7.3.1
pip install -r requirements.txt
7. Running Tests
To run the tests, use the following command:pytest --html=reports/html/report.html
Conclusion
Using the Page Object Model in Selenium with Python provides a structured approach to organizing your test automation code. By encapsulating page elements and actions within dedicated classes, you enhance code maintainability, readability, and reusability.
This practical example illustrates how to implement POM effectively, ensuring that your automation framework is scalable and easy to manage.