Understanding the distinction between PageFactory and Page Object Model (POM) is crucial in automated testing. Here’s a brief comparison.
Page Object Model (POM)
- POM is a design pattern creating an Object Repository for web UI elements, aiming to reduce code duplication and improve test maintenance.
- Each web page has a corresponding Page Class containing methods that perform operations on WebElements.
- POM separates operations and flows in the UI from verification, making the code cleaner and more understandable.
- It supports the reuse of page methods and gives methods realistic names for easy mapping with UI operations.
PageFactory in Selenium
- PageFactory is an inbuilt POM concept in Selenium WebDriver, used for initializing Page objects or instantiating the Page object itself without using “FindElement/s”.
- It uses annotations
@FindBy
to find WebElement, with attributes like tagName, partialLinkText, name, linkText, id, css, className, xpath.
Why Not to Use PageFactory?
- PageFactory, while a common Page Object implementation in Java, is not recommended by Selenium contributors due to various limitations.
- It does not store the result when the driver locates the element, causing it to relocate the element for each operation, leading to inefficiency and performance issues.
- The
@CacheLookup
annotation in PageFactory, intended for caching elements, can lead toStaleElementException
If the DOM changes, it makes the element stale. - PageFactory is considered a “cumbersome and problematic implementation” with no significant benefits over direct runtime code element finding.
Code Examples
Page Object Model Example
public class LoginPage { private WebDriver driver; // Constructor public LoginPage(WebDriver driver) { this.driver = driver; } // Locators private By username = By.id("username"); private By password = By.id("password"); private By loginButton = By.id("login"); // Methods public void enterUsername(String user) { driver.findElement(username).sendKeys(user); } public void enterPassword(String pass) { driver.findElement(password).sendKeys(pass); } public void clickLogin() { driver.findElement(loginButton).click(); } }
PageFactory Example (Not Recommended)
public class LoginPage { @FindBy(id = "username") WebElement username; @FindBy(id = "password") WebElement password; @FindBy(id = "login") WebElement loginButton; // Constructor public LoginPage(WebDriver driver) { PageFactory.initElements(driver, this); } // Methods public void enterUsername(String user) { username.sendKeys(user); } public void enterPassword(String pass) { password.sendKeys(pass); } public void clickLogin() { loginButton.click(); } }
Conclusion
While PageFactory offers a structured approach, its inefficiencies and limitations make it less favorable than the direct implementation of the Page Object Model. POM provides a more efficient, understandable, and maintainable framework for web UI testing in Selenium.
Feel free to share this post with your network to spread knowledge about efficient practices in Selenium automation testing!
For further reading and detailed explanations, please refer to the articles from Guru99, UltimateQA, and Titus Fortner’s blog.