4. Page Object Model Pattern
By: Ali Pala
Tags: Selenium, Test Automation, Page Object Model, Python, Selenium Webdriver

Hello folks! We have prepared our environment so that building a test automation framework. At this stage, I am going to start discussing Page Object Model Pattern in Selenium. I highly recommend you to read the previous posts beforehand.

You can find what should you expect in this post below;

What is Page Object Model(POM)? Why we should POM? Implementation. Advantages of using POM.

1. What is Page Object Model(POM)?

Page Object Model is a design pattern that is used mostly in Web UI testing for enhancing maintenance of the test code and reducing duplication of it. Each page in the web application has to be separate class different than the others. These are the classes that contain Web Elements(Locators) of the web page, the classes include the methods which perform operations on those Web Elements and the classes are exist so that to execute corresponding page tests.

2. Why we should POM?

As I mentioned above, Selenium test projects generally are hard to be maintained. Also, there is a significant amount of duplicated line of codes. Duplicated code causes unnecessary functionality and many crazy similar page object locators. Another reason is that changing the page locators frequently. If some locator will change, you might have to adjust the locator for the new implementation. This is a considerable waste of time for the QA team. You cannot see the exact advantage of POM if you have a small script to test the login behavior of the application. Script maintenance looks easy. However, as time goes further, the test suite will grow. As you add more and more lines to your code, things become tough. Think about this. If you consumed your time to maintain your code at every change, how would you increase your test coverage?

3. Implementation

Okay, we talked about theory enough. It’s time to write some codes. I will be using PyCharm IDE to develop Page Object Model-based project. Create a directory and give a name whatever you want for the project on your local. Here is the very first window once you start to create a new project in PyCharm. First, you need to specify both the location and the project name. Then, select a project interpreter and click click button.

Creating a new POM project in PyCharm

The next step is creating separate folders for the corresponding elements. For example, Web Elements will be located under Locators folder, common actions will be in Base class, Chrome, Firefox or Explorer driver will be in Drivers, Web Page methods will be located in Pages, Testcases will be in Tests folder. You can also create a TestSuite folder so that executing your tests as a suite. Here is the project folder structure below.

The folder structure in a POM pattern

We can create python files under folders that are related actions for our Application Under Test. For creating a python file, right-click Locators folder, click “New” and create python file.

After deciding associated files, your project structure will look like this.

Python project structure

I will only talk about Login Page to be able to give a clear idea of what are we doing with Page Object Model pattern. Assume that, you will test the login functionality of a web application and need to give username and password on Login Page and click login button to enter the system.

We need to send the username and password details into related text boxes by using send_keys() function. Then, click login button to enter the Home Page then we should verify we are whether the correct page. We should consider creating 3 python classes to achieve a test based on POM; locators.py for web elements, LoginPage.py for page methods, and test_login.py for the test. We also need to create a Base class to call the Webdriver object.

base.py

base.py

locators.py

Some codes in locators.py

LoginPage.py

LoginPage.py

test_login.py

test_login.py

That’s all! You may wonder about how to gather login details. Here is the importance of Data Driven Testing. I encapsulated login details in login_details.xlsx file and used pandas library in order to fetch the username and password as easily as possible with a minimum number of codes.

4. Advantages

If the UI design of the page changes, you don’t have to update the test class implementation, rather it is enough to update the page object(locator) POM provides us creating a non-fragile test by means of reducing duplicated codes. POM makes our test code as clean as possible. Therefore, the test codes are easy to understand even if after a very long time later. POM design implementation separates the page objects and tests by achieving an abstraction.

Let’s continue with the next blog. See you there!