Page Object Model and Page Factory in Selenium
Tags: software, page, factory, selenium
Published: 2024-07-02 02:38:20

We implement the test automation using [Selenium](https://testgrid.io/blog/selenium-what-is-it/) to ease the process of website testing. But what if test automation scripts are not written in a structured way? It would make the testing process inefficient and ambiguous. To maintain the efficient performance and project structure of the automation scripts, it is necessary to have different pages/scripts for each task. To ease the access of distributing codes across different files and maintain a clean project structure, the Page object model and Page Factory come to the rescue.

In this article, we will walk you through some of the core concepts of the Page object model and Page Factory in Selenium with the help of appropriate examples.

### What is the Page Object Model in Selenium?
Page Object Model (POM) is a design pattern in Selenium that is used to create an object repository to store all web elements. It helps improve the code reusability and test case maintenance.

In this design pattern, each web page in the web application will have a corresponding Page Class in the automation script. This Page class will identify the various WebElements of that web page and also includes methods to perform testing on those WebElements. 

Note: The name methods inside a Page class should be given to represent the task they are performing. For example, if a loader is waiting for the order to be confirmed, the POM method name can be waitForOrderConfirmation().

### Why do We Need Page Object Mode?

The below-mentioned points depict the need for a Page Object Model in Selenium.

The below-mentioned steps depict the need for a Page Object Model in Selenium.

**Code Duplication:** Without proper management of locators, automation test projects can become unwieldy due to duplicated code or excessive repetition of locator usage.
**Time Efficiency:** Maintaining scripts becomes cumbersome when multiple scripts rely on the same page element. Any change in that element necessitates updates across all affected scripts, consuming valuable time and risking errors.
**Code Maintenance:** POM mitigates code maintenance challenges by centralizing element identification and interaction in separate class files. When a web element changes, updating its locator in one class file propagates the change across all associated scripts, ensuring code remains reusable, readable, and maintainable.
**Adaptability to UI Changes:** When UI elements are restructured or relocated, existing automation tests may fail due to outdated locators. Manual updates to numerous scripts can be time-intensive. However, with POM, locating and updating element locators is centralized, streamlining the process and allowing testers to focus on enhancing test coverage rather than manual adjustments.
By implementing POM, automation frameworks gain flexibility, efficiency, and resilience to UI modifications, ultimately enhancing the effectiveness of test automation efforts.

### Advantages of Page Object Model in Selenium

The advantages of using the Page object model in Selenium are:

**Reusability:** The same Page object class can be used in several test cases, which reduces code duplication and improves code reuse. This saves time and effort when writing new tests because the same Page object class may be used several times.
**Easy Maintenance:** The page object model improves the UI test management by organizing code logically. It helps identify which page or screen needs modification when UI elements or actions change.
**Readability of Scripts:** POM makes the test scripts more readable and understandable by separating the script files for each screen and using the logical names for methods.
**Increases test coverage:** POM allows testers to create additional tests with less effort. This increases test coverage and helps to uncover more faults, resulting in software with greater quality.
**Functional encapsulation**: Using POM, all probable testing operations on a page may be described and included within the same class built for each page. This enables a clear definition and defines the scope of each page’s operation.

### What is Page Factory in Selenium?

Page Factory is an in-built Selenium design pattern for web automation testing that simplifies the creation of Page Objects. It reduces the amount of boilerplate code required to build Page Objects, making the automation code easier to maintain and read.

In Page Factory, testers utilize the @FindBy annotation alongside the initElements method to initialize web elements.

The @FindBy annotation accepts various attributes such as tagName, partialLinkText, name, linkText, id, CSS, className, and XPath, enabling testers to locate and interact with elements on the web page precisely.

### Advantages of Page Factory in Selenium

Using the Page Factory along with the Page Object Model in Selenium brings a lot of advantages. Listed below are a few advantages of Page Factory:

**Easy Initialization:** PageFactory simplifies web element initialization by allowing the use of annotations like @FindBy directly within the page object class. These annotations specify locators (e.g., id, name, XPath), and PageFactory automatically initializes elements upon instantiation of the page object.
**Lazy Initialization:** PageFactory employs lazy initialization, meaning elements are initialized only when accessed or interacted with in the test code. This optimizes performance by avoiding unnecessary element lookup and initialization when elements are not needed.
**Improved Code Readability:** Separating web element initialization from test code enhances code readability. With Page Factory, it’s clearer to understand code intent and interactions with the web page.
**Enhanced Test Performance:** Page Factory can boost test performance by reducing the overhead of locating web elements. Initializing the Page Object once per test, rather than per test method, minimizes redundant operations, improving overall efficiency.

### FAQs
1. What is the difference between the Page object model and the Page factory in Selenium?
The Page Object Model is a code design pattern that creates an object repository for web items that can be accessed via a web page. It uses the By annotation to describe page objects, and each object must be initialized. POM also supports cache storage. Page Factory, on the other hand, is a class that only implements the POM.

2. Why do we use POM in Selenium?
POM is a Selenium design pattern that builds an object repository to hold all web elements. It reduces code duplication and enhances test case management.

3. Can we use POM without Page Factory in Selenium?
Yes, you can use Page Object Model (POM) in Selenium without using Page Factory. 

**Source:** _This blog was originally posted on [TestGrid](https://testgrid.io/blog/page-object-model-and-page-factory-in-selenium/)._