Page Factory Model in Selenium Java Automation
In Selenium automation, code readability, maintainability, and reusability are crucial—especially for large test suites. To achieve this, the Page Object Model (POM) is a popular design pattern used to separate the logic of test scripts from web elements. However, to make POM even more efficient, Page Factory was introduced in Selenium. It enhances the POM by providing a concise way to initialize web elements and improves test performance through lazy loading. This blog will explain what Page Factory is, how to use it in Selenium with Java, and its benefits in automation testing.
✅ What is Page Factory in Selenium?
Page Factory is an extension of the Page Object Model in Selenium. It provides an annotation-based approach to define and initialize web elements in a class, making the code cleaner and more readable.
It uses @FindBy annotations to locate elements and PageFactory.initElements() to initialize them. This approach avoids writing driver.findElement() repeatedly and supports lazy loading—meaning elements are only initialized when they are actually used.
🛠️ How to Implement Page Factory
Let’s break it down with a simple example.
1. Create a Page Class (Page Object)
java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class LoginPage {
WebDriver driver;
// Use @FindBy to locate elements
@FindBy(id = "username")
WebElement usernameField;
@FindBy(id = "password")
WebElement passwordField;
@FindBy(id = "loginBtn")
WebElement loginButton;
// Constructor to initialize elements
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
// Actions
public void login(String username, String password) {
usernameField.sendKeys(username);
passwordField.sendKeys(password);
loginButton.click();
}
}
2. Write Your Test Case
java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class LoginTest {
@Test
public void testLogin() {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");
// Create object of LoginPage
LoginPage loginPage = new LoginPage(driver);
loginPage.login("testuser", "password123");
driver.quit();
}
}
🔍 Common Annotations Used in Page Factory
@FindBy(id = "value")
@FindBy(name = "value")
@FindBy(xpath = "xpathExpression")
@FindBy(css = "cssSelector")
You can also use @FindAll and @FindBys for more complex locators.
🎯 Benefits of Using Page Factory
✅ Cleaner Code: Reduces boilerplate code for locating elements.
✅ Reusability: Element definitions are reusable across multiple tests.
✅ Lazy Initialization: Elements are only loaded when they are used, improving efficiency.
✅ Improved Maintenance: Changes in UI need to be updated in only one place.
✅ Enhanced Readability: Test scripts become more readable and easier to manage.
⚠️ Best Practices
Do not overuse Page Factory; it's great for static pages but may not work well with dynamic content unless handled carefully.
Use @CacheLookup for static elements that don’t change frequently.
Keep your page methods small and focused (e.g., one method for login, one for registration, etc.)
🧪 Conclusion
The Page Factory model in Selenium Java is an effective way to organize test automation code, especially for projects that involve multiple pages and interactions. It builds upon the Page Object Model and makes the code more modular, scalable, and maintainable. By leveraging @FindBy annotations and PageFactory.initElements(), you can write cleaner tests and increase automation efficiency.
Learn Selenium with Java Training
Read More: Generating Cucumber Reports for Selenium Java Tests
Read More: Implementing Hooks in Selenium Java Cucumber Tests
Read More: Data Tables in Cucumber for Selenium Java AutomationVisit Our IHUB Talent Institute Hyderabad
Get Direction
Comments
Post a Comment