Mar, 2025
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.
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.
Step 2: Create a Page Class
Below is an example of a LoginPage class that encapsulates UI elements and actions.
Step 3: Create a Test Script Using the Page Class
Below is a test script that uses LoginPage in a test case.
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.
Step 2: Create a Page Class
Below is an example of a LoginPage class that encapsulates UI elements and actions.
Step 3: Create a Test Script Using the Page Class
Below is a test script that uses LoginPage in a test case.
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. |
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?