Page Object Model and Page Factory are the names of two popular tools in Selenium, a popular test automation framework. In this article, we are going to dive into the depth of these two important pillars in the field of testing.
The Page Object Model, widely known as POM, is a design pattern that is widely used in test automation that creates an Object Repository for web UI elements. It holds tremendous popularity due to its advantages like reducing code duplication and improving test maintenance.
In this model, each web page of an application is considered as a class file that will identify the WebElements of that particular web page and also contain Page methods that perform operations on those WebElements. These methods are given names according to the tasks they are performing. For example, if a loader is waiting for the payment gateway to appear, that POM method name becomes like waitForPaymentScreenDisplay().
Selenium WebDriver is popular for providing an extremely simple experience in UI test automation. You can do that just by performing operations on some specific elements.
For example, in a Login script, the primary task is finding the login credentials and filling in those values. You may think that it is such a small task. Then why is everyone bothered about struggling so much to automate them? Well, it is a very small script. That’s why it looks so simple. In real-world applications, there are numerous components and functions in an application. Hence, testing each of them minutely becomes a tremendously cumbersome task. With time, you will keep on adding new features to your application, and eventually, the script will get longer and the test suite will keep growing.
You know that the modern world prefers dynamic coding to make things easy. That means it is quite common that some important elements have multiple scripts using a single element. So, if you follow an ordinary approach for maintaining the scripts, for every small to big change in the element, you have to manually update all the scripts. This process is normally very time-consuming and prone to errors.
To get rid of such petty problems, the world has adopted the method of using class files. In this approach, a separate class file is created that finds web elements, fills them, or verifies them. The best part is that you can reuse that class file in all the scripts using that element. As a result, in the future, if there is any change in the web element, making changes in the class file will be enough rather than making changes in multiple different scripts. This amazing approach that makes the code more readable, maintainable, and reusable, is called by the name “Page Object Model”.
Page Object Model has gained immense popularity due to its several benefits that serve you in multiple ways. Let’s take a look at them to get a clearer idea.
POM is all about bringing ease to operations. Hence, implementing it is also a smooth step-by-step process that we have discussed below.
As mentioned earlier, the Page Object Model framework means all Web Elements of the AUT and the methods that operate on these Web Elements are maintained inside a class file. The basic structure of this POM framework is called Simple POM. Basic tasks are separated as part of test methods. Let’s try to understand it with an example.
Steps to be followed -
This was the step breakdown. However, we need to find out how POM serves the process here. So, here we are dealing with the following two pages -
According to the page structure, we create two Page Object Models in Selenium classes. So, let’s check them out.
It is quite common that during the updation process or locator changes, the code becomes harder to maintain. That’s where POM saves you from getting trapped with such issues in the code. Let’s understand the complete system with a nice example.
Use Case 1: You need to follow the following steps to proceed with this step.
From the step breakdown, it is evident that you have to use the “login text fields” and the “login button” multiple times. Let’s start with an overview of the code when the POM design technique is not being used.
Such non-POM processes become extremely inefficient. Suppose in a codebase for a “Login” function, if the locator for the login button changes, you have to make corrections to the Login button locator by yourself at each portion it is used. On the other hand, if the POM approach is used here, making the changes to the Login button locator in only the page class for the login page will be enough to do the job. Then, there will be no existence of inefficient approaches like making the changes by yourself at every place where you use or refer to the login locator.
Page Factory in Selenium is a class or better to say an inbuilt well-optimized Page Object Model framework concept for Selenium WebDriver. Its efficient use cases are the initialization of Page objects or instantiating the Page object itself. It provides you with another amazing benefit i.e. it allows you to initialize Page class elements without using “FindElement/s.”
Separation of Page Object Repository and Test Methods is a crucial concept that is used in this framework/class. The class Page Factory in Selenium allows you to execute the following tasks on different elements with ease -
Also, @FindBy annotation can be used with different location strategies to find web elements and perform actions on them. Some of the most popular locators are mentioned below.
Implementation of Page Factory in Selenium Project
Similar to POM, Page Factory also has a smooth implementation process. For a clearer understanding, let’s check out the same example in Page Factory.
As the common use of Page Factory, we saw that the initElement() method allows you to initialize Page Objects of the Page Objects class. But, it is never possible that all the required WebElements on the page (or AUT) are located and stored in one shot. And, that’s where the concept of lazy loading named AjaxElementLocatorFactory comes into the picture. As previously mentioned, it is a very useful concept when the elements are used in any operation. It is used to assign a timeout for WebElements to the object page class. That means it gives the condition to observe a specific wait time starting from the moment when an operation is performed on an element. It ensures that if the element is not found in the given time interval, Test Case execution will throw a ‘NoSuchElementException’ exception.
We looked at various sides of the two amazing tools that do revolutionary things using Selenium. You need to use both of them but according to the specific requirements for them. And, to understand that requirement, you must know about the following differences between them.
Both Page Object Model and Page Factory have their own advantages and disadvantages. They are tremendously useful according to the specific set of requirements you have from them. All the discussion above lets you figure out how you can bring them to use as per your need.
However, while Selenium has been serving as a great test automation framework for a long, the present world is now more keen to experience absolutely codeless testing. So, as you get to know about Selenium from the article “An Introduction To Codeless Selenium Test Automation”, you can check out here how no-code testing is totally changing the game.
When the discussion is about the no-code movement, the most amazing codeless testing tool Preflight is a must-mention here. To get amazed by its awesome features and functionalities, just go through this article “10 Amazing Software Testing Benefits From Preflight In 2022”.
To experience the most efficient form of Software Testing, Book A Demo now. For more information, we are always available with our amazing website. And, if the informative tech articles excite you as they excite us, this blog page is what you must check out.