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?
- Code separation – Keep test and locators separately, makes our code clean, easy to understand, and maintain
- 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
- 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 specifically for that page i.e. opening the page, log in, logout, etc…
- Import the page class in your test and replace selectors with page_object selectors and methods
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 –
Check out the video below to learn more about Page Object Model in Selenium Python –
To learn more about Selenium Python, check out the full tutorial series here for free!
📧 Subscribe to my mailing list to get access to more content like this
👍 Follow automationbro on Twitter for the latest updates
...
I love coffees! And, if this post helped you out and you would like to support my work, you can do that by clicking on the button below and buying me a cup of coffee -
You can also support me by liking and sharing this content.
Thanks for reading!
Top comments (0)