![Page Factory pattern](https://qamind.com/wp-content/uploads/2023/02/Page-Factory.png)
The Page Factory pattern is a design pattern commonly used in Selenium automation testing to create object-oriented page object models (POMs) for web pages. This pattern aims to make test automation code more maintainable, reusable, and scalable. In this blog post, we will discuss the Page Factory pattern and its benefits.
What is the Page Factory pattern?
The Page Factory pattern is an extension of the POM design pattern that uses the PageFactory class in Selenium. The PageFactory class provides a simple way to initialize web elements in a page object using the @FindBy annotation. This annotation is used to locate and initialize web elements on a web page. The Page Factory pattern makes the code more readable, reusable, and maintainable by encapsulating the elements of a web page into a separate class.
Implementing the Page Factory pattern
Here is an example of implementing the Page Factory pattern in Selenium:
- Create a page object class that represents the web page to be tested. In this example, we’ll create a LoginPage class.
![Page Factory pattern](https://qamind.com/wp-content/uploads/2023/02/image.png)
- In the test class, initialize the page object using the PageFactory class.
![](https://qamind.com/wp-content/uploads/2023/02/image-1.png)
In the example above, the LoginPage class represents the web page to be tested. We define 4 properties and initialize them by using the FindBy annotation:
- The web driver will be used to perform actions on the browser
- The username element field
- The password field
- The login button which we will perform click on
In the constructor, we initialize the driver and all the elements for that page using the Page Factory’s built-in method called initElements(). The beneficial thing here is that page Factory uses the AjaxElementLocatorFactory to lazy load the elements only when needed which is not the case when using POM. Last but not least, we create a login method that will simply fill out both fields and click on the login button.
In the test itself, we define the driver and the Login page. In the BeforeMethod hook(part of the TestNG annotation family), we initialize the Chrome driver, the URL to navigate to, and create an instance of the Login Page class. In the AfterMethod hook, we’re stopping the driver and closing the browser. The test method simply calls the login methods from the Login Page.
Advantages of using the Page Factory pattern
- Improves code maintainability: The Page Factory pattern separates the object creation and initialization from the test logic. This makes the code more modular and easier to maintain.
- Increases code reusability: The Page Factory pattern allows the creation of reusable page objects that can be shared across different test cases.
- Improves code scalability: The Page Factory pattern makes it easy to add new web elements to a page object without affecting the existing code.
- Enhances code readability: The Page Factory pattern makes the code more readable by encapsulating the web elements of a page into a separate class.
POM vs Page Factory
POM | Page Factory |
Initialization of elements even if not needed at a particular time | Lazy initialization |
By keyword locator | FindBy keyword locator |
All elements are initialized one by one | Initializes all elements using the built-in initElements() method |
Conclusion
I hope this post gave you the starting ground for your test automation journey. Of course, learning and fully incorporating it within your project can be challenging but once you manage to understand the core and logic behind it, the Page Factory pattern can be a powerful design pattern that can make your tests easier to use, write and understand. It is a widely used pattern in the test automation world but to understand it fully, you need a solid knowledge of the POM design.