Debug School

rakesh kumar
rakesh kumar

Posted on

Explain page object model in selenium python

What is Page Object Model (POM)?
One way to help write clean and well-tested code is to create a Page Object Model. A Page Object Model (POM) is a set of classes designed to represent one or more web pages. It is a way of using objects to represent elements on the page. This enables you to write code that is simple and easy to understand. It also helps you to keep your tests well-structured and understandable.

Image description

Image description

There are many benefits to creating a POM such as:

Testing Quality – The POM helps you write tests that are easy to understand and maintain. This can help improve the quality of your testing process to improve the readability and reliability of the scripts.
Site Maintenance – It is easier to maintain the site over time. Suppose if something got changed on any page, we could easily find the functions and locators that need to be changed by that page class.
Team Collaboration – The POM can help with collaboration between team members and improve the efficiency of your team significantly.
Overall Productivity – The POM can make it easier for new team members to get up to speed and boost overall team productivity.
Reusing Code – Using POM, we can reuse our functions in different Test Scripts by importing them from Page Class. It doesn’t require writing the same functions in different test cases.
With all of these benefits, it is clear that a POM can be a valuable tool for any organization. By creating one, you can help improve the quality of your software testing process and team collaboration.

What is Page Factory?
Page Factory is a method of implementing a Page Object Model. To support the Page Object pattern. In Java, we use @findBy, here, we will declare all web elements in a dictionary. Dictionary keys become WebElement or class member variables with having all extended WebElement methods.

Page Factory initializes all the web elements declared in Point at a time. All WebElements methods are re-defined to add extra features eg- the click method is extended to have an explicit wait for the element to be clickable.

Page Object Model in Selenium with Python using selenium-page-factory
It is easy implement page factory in Selenium with Python by using the selenium-page-factory package. Install it by using

pip install selenium-page-factory
Enter fullscreen mode Exit fullscreen mode

To use selenium-page-factory every Page in the Page Object Model should have a WebDriver object as a class member as shown below:

class PageClass(PageFactory):

def __init__(self, driver):
self.driver = driver
Enter fullscreen mode Exit fullscreen mode

Extended WebElements Methods in selenium-page-factory

Image description

Image description

The Page Object Model (POM) is a design pattern used in Selenium for creating a structured, maintainable, and reusable codebase for web automation. It abstracts web pages into separate classes, where each class represents a page or a component of a page. This separation helps achieve a clean distinction between test code and page-specific code, enhancing code reusability and maintainability.

Here's an example of implementing the Page Object Model in Selenium using Python. We'll create a simple example with a login page and a home page.

Create a "LoginPage" class representing the Login page:

from import By

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.username_locator = (By.ID, "username")
        self.password_locator = (By.ID, "password")
        self.login_button_locator = (By.ID, "loginButton")

    def enter_username(self, username):

    def enter_password(self, password):

    def click_login_button(self):
Enter fullscreen mode Exit fullscreen mode

Create a "HomePage" class representing the Home page:

from import By

class HomePage:
    def __init__(self, driver):
        self.driver = driver
        self.welcome_message_locator = (By.ID, "welcomeMessage")

    def get_welcome_message(self):
        return self.driver.find_element(*self.welcome_message_locator).text
Enter fullscreen mode Exit fullscreen mode

Write a test script that uses the Page Objects:

from selenium import webdriver
from LoginPage import LoginPage
from HomePage import HomePage

# Initialize the WebDriver
driver = webdriver.Chrome()

# Create instances of Page Objects
login_page = LoginPage(driver)
home_page = HomePage(driver)

# Perform actions using Page Objects

# Check if login was successful
welcome_message = home_page.get_welcome_message()
if "Welcome, your_username!" in welcome_message:
    print("Login successful")
    print("Login failed")

# Close the browser
Enter fullscreen mode Exit fullscreen mode


The test script navigates to the example website and uses the LoginPage Page Object to enter a username and password and click the login button.

After logging in, it uses the HomePage Page Object to extract the welcome message and checks if it matches the expected value.

The script then prints whether the login was successful or not.

By using the Page Object Model, the test script becomes more readable, and changes to the page structure can be easily managed within the corresponding Page Objects. This separation of concerns improves the maintainability of the test automation code.


Top comments (0)