SDET Unicorns

Selenium Python Page Object Model

POM

Table of Content

In this post, we will learn what is Page Object Model and how to implement the Page Object Model with Selenium Python. Page Object pattern has been around for many years and is usually considered as one of the best practices to follow in the test automation industry.

What is Page Object Model (POM)?

POM is a design pattern that makes it easier for us to maintain our code and reduce code duplication. It allows you to abstract any page related information away from your actual tests.

So to put it simply, all the selectors that are within your tests, we can extract that away and put it in a separate file which we can then reuse whenever we want.

Advantages of Page Object Model?

  1. Code separation – Keep test and locators separately, makes our code clean, easy to understand, and maintain
  2. Single source of the repository – all the locators are stored in one place. Tests become short and optimized as we can reuse locators and page object methods
  3. Readability – improves significantly and makes it easy for new team members to start writing tests by following the existing structure

POM in Selenium Python

These are the steps we’ll take to implement POM in Selenium Python –

  • Create a new class under a new page_objects directory
  • Find all the selectors in your test and move them under the page class
  • Identify any common methods you can create specific for that page i.e. opening the page, login, logout etc…
  • Import the page class in your test and replace selectors with page_object selectors and methods
    def test_home_page(self):
        # open home page
        self.open_page()
        
        # assert page title
        self.assert_title("Practice E-Commerce Site – Automation Bro")

        # assert logo
        self.assert_element(HomePage.logo_icon)

        # click on the get started button and assert the url
        self.click(HomePage.get_started_btn)
        get_started_url = self.get_current_url()
        self.assert_equal(get_started_url, "https://practice.automationbro.com/#get-started")
        self.assert_true("get-started" in get_started_url)

        # get the text of the header and assert the value
        self.assert_text("Think different. Make different.", HomePage.heading_text)

        # Exercise: scroll to bottom and assert the copyright text
        self.scroll_to_bottom()
        self.assert_text("Copyright © 2020 Automation Bro", HomePage.copyright_text)

In the code sample above, we have replaced the following selectors – logo_icon, get_started_btn, heading_text and the copyright_text as well as added the open_page page object method.

Now, your page object class might look something like this –

class HomePage(BaseCase):
    logo_icon = ".custom-logo-link"
    get_started_btn = "#get-started"
    heading_text = "h1"
    copyright_text = ".tg-site-footer-section-1"

    def open_page(self):
        self.open("https://practice.automationbro.com")

Check out the video below to learn how to implement Page Object Model in Selenium Python –


To learn more about Selenium Python, check out the full tutorial series here for free!

I hope this post helped you out, let me know if you have any questions in the comments below!

Happy testing! 

My Resources

Thrive Suite: The all-in-one WordPress theme I used to create my blog.
Jasper AI: My favorite AI writing tool.
Surfer SEO:  A tool to help you rank your content on the first page of Google.

Write Content Faster

5/5

Write blog posts that rank for affiliate terms!

Join our mailing list

Unlock the Secrets to Becoming a SDET Pro in the Industry!

Stay ahead of the curve! Join our mailing list for exclusive insights, tips, and the latest industry updates delivered straight to your inbox

Table of Content

Related Post

Functional vs Regression Testing

Functional Testing vs Regression Testing: Main Differences

Testing has increasingly become a requirement in the software development lifecycle in recent years. The realization that development is no longer a focal point for stakeholders but saving costs by catching defects early in their application makes it necessary to learn different testing types.

Read More »