What is the Page Object Model (POM) and how is it used with Selenium WebDriver?

What is the Page Object Model (POM)?

The Page Object Model (POM) is a design pattern in test automation for enhancing test maintenance and reducing code duplication. POM can be used with tools like Selenium WebDriver to interact with web pages. It encourages the creation of an abstraction layer for the pages or parts of the pages of the application under test.

In POM, each web page in the application is represented by a class. These classes are referred to as "Page Objects," and they encapsulate the properties and behaviors of the web pages they represent. The Page Object class contains elements (or controls) of the web page as well as methods to interact with these elements.

Benefits of POM

  • Reusability: By creating page object classes, the code can be reused across multiple test cases.
  • Readability: Tests become more readable because the technical details are hidden behind the methods in the page objects.
  • Maintainability: When the UI changes, the fix needs to be applied only within the page object class rather than in every test case where the UI elements are accessed.

How POM is Used with Selenium WebDriver

Here's how you can implement the Page Object Model with Selenium WebDriver in a test automation project:

Step 1: Define the Page Object Class

Each page object class will contain the locators to find the web elements and methods to perform actions on those elements.

Here's an example in Python:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

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):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.username_input)).send_keys(username)

    def enter_password(self, password):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.password_input)).send_keys(password)

    def click_login(self):
        WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.login_button)).click()

Step 2: Use the Page Object in Tests

The test script will then use this page object to interact with the web page, which makes the test script cleaner and more maintainable.

Here's how you might use the LoginPage in a test:

from selenium import webdriver
import unittest

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://example.com/login")

    def test_login(self):
        login_page = LoginPage(self.driver)
        login_page.enter_username("user@example.com")
        login_page.enter_password("password123")
        login_page.click_login()

        # Add assertions here to validate the login was successful

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

Step 3: Run the Tests

You would run the tests using a testing framework. In the Python example above, we're using the built-in unittest framework.

python -m unittest test_login.py

The Page Object Model is a widely adopted pattern in Selenium WebDriver tests because it leads to cleaner, more manageable, and scalable test code. It is also applicable in other programming languages and test frameworks with Selenium or similar tools. The principles of encapsulating page details and providing an interface for interacting with those details remain the same regardless of the language or framework.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon