Mar,  2025

Page Object Model and Page Factory Using Selenium Java
SHARE THIS

Introduction

Test automation has become integral to modern software development, enabling rapid releases while enhancing software quality. One of the primary approaches to efficiently automating web applications is the page object model (POM).
POM is a design pattern that increases test maintenance, re-purpose, and readability. This blog will explore the advantages and implementation of the page object model (POM) in automation frameworks.

What is a Page Object Model?

The design pattern known as the Page Object Model, also referred to as POM, is employed in automated testing. In POM, each page of an application is represented as a class, and each class contains elements and actions of a respective web page.

Figure 1 Page Object Model
The test scripts are written and maintained in a separate class using the respective web page elements class. This makes the scripts more maintainable than any other design pattern.

Key Principles of the Page Object Model:

  1. Encapsulation – The web elements and actions are encapsulated within the page class.
  2. Isolation – Test scripts do not directly interact with the UI elements; they use the methods from the page classes.
  3. Reusability – Multiple test cases can make use of the same actions and components.

Benefits of the Page Object Model:

  1. Improved Maintainability – Instead of modifying multiple test scripts for any change in the UI, only corresponding page classes are updated.
  2. Better Code Readability – Because test scripts concentrate on test logic rather than user interface interaction, they are
    easier to read.
  3. Code Reusability – Common functions like login, form submission, and navigation can be reused across different
    tests.
  4. Scalability – It becomes easier to scale the test suite by adding new pages without affecting existing test cases.
  5. Reduced Code Duplication – Since page methods are centralized, test scripts do not have redundant code.

Project Structure for Page Object Model:

Below is an example of the project structure.
Figure 2 Page Object Model Project Structure

Implementation of Page Object Model (POM) in Selenium:

Let’s go through an example of the implementation of Page Factory with Selenium POM WebDriver and Java as a programming language.

Step 1: Create a TestBase Class
Below is an example of a base class that contains the base functions like WebDriver initialization, etc.

Figure 3 Implementation of Page Object Model (POM) in Selenium

Explanation of the above code snippet:

  • We have created a base class with the initialization of the WebDriver using WebDriver Manager.
  • Code Line 26: We have used implicit wait after the initialization of the WebDriver so that it will apply to all elements and remain effective as long as the WebDriver instance is active.
  • Method navigatetoSite: We are setting up the driver and navigating to the base URL.

Step 2: Create a Page Class
Below is an example of a LoginPage class that encapsulates UI elements and actions.

Figure 4 POM Page Class

Explanation of the above code snippet:

  • We have created a login page class inheriting the base class.
  • Code Lines 8 to 10: In POM, we find elements using the By We have used private keywords so that the locators of this class should not be accessible to other classes.
  • Code Lines 14 to 22: We have simply added the actions against the locators/ elements.
  • Code Line 24: We are using an assertion to validate the page URL after performing login. If, for some reason, the login is unsuccessful, it will look for the page URL and will fail.

Step 3: Create a Test Script Using the Page Class
Below is a test script that uses LoginPage in a test case.

Figure 5 POM Test Script Using the Page Class

Explanation of the above code snippet:

  • We have created a login test class inheriting the base class.
  • Code Lines 8 to 11: The setup method will launch the driver and will navigate to the base URL. We have added an annotation @BeforeTest, which means that the method will execute before every method that has @Test
  • Code Lines 13 to 19: This is our actual test case. We are importing the actions from the Login Page class. @Test annotation is used to define it as a test.
  • Code Lines 21 to 24: We are simply closing the browser after the execution of our test cases. @AftreClass annotation is used, which means that this method will be executed after all the methods/tests in the class are executed.

What is Page Factory?

Page Factory is a form of implementing the Page Object Model (POM) design pattern, which is typically considered an improved implementation of POM within Selenium.

In the page factory, the web elements are initialized using @FindBy annotation. This facilitates the page creation process, enhancing code eligibility and making it more sustainable.

Implementation of Page Factory in Selenium POM:

Let’s go through an example of the implementation of Page Factory with Selenium POM WebDriver and Java as a programming language.

Step 1: Create a TestBase Class
Step 1 remains the same for the implementation of POM and page factory.

Figure 6 Test Base Class Project Object Model

Step 2: Create a Page Class
Below is an example of a LoginPage class that encapsulates UI elements and actions.

Explanation of the above code snippet:

  • We have created a base class with the initialization of the WebDriver using WebDriver Manager.
  • Code Line 26: We have used implicit wait after the initialization of the WebDriver so that it will apply to all elements and remain effective as long as the WebDriver instance is active.
  • Method navigatetoSite: We are setting up the driver and navigating to the base URL.
Figure 7 Page Class

Explanation of the above code snippet:

  • We have created a login page class inheriting the base class.
  • Code Lines 10 to 17: In the page factory, we find elements using the @FindBy keyword. We have used private keywords so that the locators/ elements of this class should not be accessible to other classes.
  • Code Lines 26 to 36: We have simply added the actions against the locators/ elements.

Step 3: Create a Test Script Using the Page Class
Below is a test script that uses LoginPage in a test case.

Figure 8 Test Script Using the Page Class

Explanation of the above code snippet:

  • We have created a login test class inheriting the base class.
  • Code Lines 12 to 16: The setup method will launch the driver and will navigate to the base URL. We are also creating an object for the login page to access the login page methods. We have added an annotation @BeforeTest, which means the method will execute before every method with @Test
  • Code Lines 18 to 23: This is our actual test case. We are importing the actions from the Login Page class. @Test annotation is used to define it as a test.
  • Code Lines 25 to 28: We are simply closing the browser after the execution of our test cases. @AftreTestannotation is used, which means that this method will be executed after the test has been executed.

Key Points From the Above Two Examples:

  • The major difference in both implementations was the way we found the elements/ locators.
  • The concept/ structure remains the same, i.e., a separate class for the test scripts and page actions is created.
  • You might have noticed that in one of my examples, I’ve used @AfterTest in one and @AftreClass in the other of my examples for the teardown method. The reason was to give you an idea of what different annotations we have and can be used to our advantage.
  • Similarly, we have @BeforeTest, @BeforeMethod, @AftreMethod, @BeforeClass, etc, which are different assertions available in TestNG. I will come up with a blog specifically related to the annotations with detailed information and practical examples.

How Page Object Model Differs from Page Factory

Page Object Model Page Factory
Uses By to find web elements. Uses @FindBy annotation to find web elements.
Code complexity is moderate, and it doesn’t provide lazy initialization. Code complexity is slightly complex as compared to POM due to annotations and it provides lazy initialization.
Simple reading since the techniques are explicit and descriptive. For beginners, the annotations might impact on code readability.
Page Object Model is a design pattern. Page Factory is a class applying the Page Object Model design pattern.
In the Page Object Model, the object is individually initialized for every page. All page objects in Page Factory are initialized using the initElements () method.

Best Practices for Using Page Object Model (POM)

  1. Keep Methods Short and Focused – Each method should perform a single action.
  2. Organize Page Classes Logically – Group related pages in separate packages.
  3. Avoid Business Logic in Page Classes – Page classes should only contain UI-related actions (like clicking and typing).
  4. Implement a Base Class – All the common functionalities/ WebDriver initialization, like wait conditions and navigation, can be stored in a base class.
  5. Use Stable and Dynamic Locators – Use locators that are less prone to changes in UI.
  6. Minimize Dependencies – Keeping the page objects independent and focused on their specific functionalities.

Conclusion

    A page object model or POM is a design pattern that lets QA testers apply scalable, maintainable, and reusable automated frameworks. Another great advantage is its ability to isolate user interface elements from test codes, which ensures stable test scripts against changes in the user interface. The page Object Model (POM) proves to be a powerful model that enhances the efficiency of your test automation strategy no matter which test automation tool you are working with.
Picture of Talib Hussain

Talib Hussain

Talib Hussain works as a Senior SQA Automation Engineer at TenX

Global Presence

TenX drives innovation with AI consulting, blending data analytics, software engineering, and cloud services.

Ready to discuss your project?