Are POM and PageFactory the same?
When I started learning about the Selenium Automation Framework, I used the words "Page Object Model" and "Page Factory" interchangeably. Later, I understood the core difference between them. Let me share my knowledge and will learn together.
Page Object Model (POM)
POM is a design pattern approach. This design pattern is for UI applications and not for APIs. It helps to design the framework in an easily understandable and simple to maintain code. It's neither a framework nor a tool. So, what is it? Creating an individual class for each webpage is a POM design pattern. Each class contains the codes related to the web elements of the pages and their events. In a way, we can call it a page library or an object repository.
There are no hard rules for defining POM. Following are some of the basic rules that helps us understand the purpose of POM.
1. Write the codes that support the page's behavior.
2. Write test cases in the test class, not in the page class.
3. Mention the driver's action in page class. The driver and webpage are tangled.
4. Don't mention any assertion in the page class.
5. When you navigate to another page, the initial page object should return another page object for the new page. It is also called the Page Chaining model.
A page factory is a "Class". It helps to enhance the page object model. The Page Factory must be imported from the package [" org.openqa.selenium.support.PageFactory "]. All web elements are initialized using the " initElements " method. Different annotations are used to handle the web element. Some of the annotations are @FindBy, @FindBys, @FindAll and @CacheLookup.
One of the advantages of using a page factory is the " AjaxElementLocatorFactory " class. It is used to find the web elements only when they are used in any operation. It assigns a timeout for web elements to the object page class.
When we implement PageFactory in POM design pattern. we don't need to handle locating web elements and initializing them. Page Factory will handle it.
[ Reference : PageObject (martinfowler.com) ]