Latest post from this blog
Understanding Page Factory and Its Annotations in Selenium
- Get link
- X
- Other Apps
๐งฉ Understanding Page Factory and Its Annotations in Selenium
When working with Page Object Model (POM) in Selenium, managing web elements efficiently becomes crucial. Writing multiple `driver.findElement()` statements across test cases not only makes the code lengthy but also difficult to maintain.
That’s where Page Factory comes in — it helps you initialize and manage page elements in a cleaner, more readable way.
๐ What is Page Factory in Selenium:
Page Factory is an extension of the Page Object Model (POM) that provides an easier and optimized way to initialize web elements.
It helps in:
* Reducing code duplication
* Improving readability
* Lazy loading of web elements (elements are loaded only when they are used)
⚙️ Syntax to Initialize Page Factory:
You initialize Page Factory using the following syntax inside your Page Object class:
PageFactory.initElements(driver, this);
* driver → the active WebDriver instance.
* this → refers to the current Page Object class.
๐งพ Example: Without and With Page Factory:
๐ฅ Without Page Factory:
public class LoginPage {
WebDriver driver;
By username = By.id("user");
By password = By.id("pass");
By loginButton = By.id("loginBtn");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void login(String user, String pass) {
driver.findElement(username).sendKeys(user);
driver.findElement(password).sendKeys(pass);
driver.findElement(loginButton).click();
}
}
๐ฉ With Page Factory:
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;
// Constructor
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
// Using Page Factory annotations
@FindBy(id = "user")
WebElement username;
@FindBy(id = "pass")
WebElement password;
@FindBy(id = "loginBtn")
WebElement loginButton;
// Method
public void login(String user, String pass) {
username.sendKeys(user);
password.sendKeys(pass);
loginButton.click();
}
}
✅ Cleaner
✅ Easier to maintain
✅ No need to write `findElement` again and again
๐ท️ Common Page Factory Annotations:
Selenium Page Factory provides several annotations to locate elements. Let’s look at the most commonly used ones:
1️⃣ @FindBy:
Used to locate elements using one or more attributes.
Example:
@FindBy(@id = "username")
WebElement username;
@FindBy(name = "password")
WebElement password;
@FindBy(xpath = "//button[text()='Login']")
WebElement loginButton;
You can use other locators like:
* id
* name
* className
* xpath
* css
* linkText
* partialLinkText
* tagName
2️⃣ @FindBys:
Used when you want to apply **multiple locators in AND condition** (all conditions must match).
Example:
@FindBys({
@FindBy(className = "form-control"),
@FindBy(name = "email")
})
WebElement emailField;
Here, Selenium first finds elements with class `form-control` and then filters those with name `email`.
3️⃣ @FindAll:
Used when you want to apply multiple locators in OR condition (if any one matches, it’s valid).
Example:
@FindAll({
@FindBy(id = "submitBtn"),
@FindBy(xpath = "//button[text()='Submit']")
})
WebElement submitButton;
Selenium will match either locator — whichever is found first.
⚡ How Page Factory Improves Performance:
Lazy Initialization: Elements are not located until they are actually used in the code.
Reduced Boilerplate Code: Eliminates repetitive `findElement` lines.
Better Maintenance: Changes in locator are done only in one place — the Page Object class.
๐ง Key Points:
1. Always initialize Page Factory in the constructor of the Page class.
2. Avoid mixing `findElement` and Page Factory within the same class.
3. Page Factory improves readability but doesn’t automatically wait for elements — use `WebDriverWait` if needed.
4. Works best when integrated with TestNG or Cucumber frameworks.
✅ Example Test Case Using Page Factory:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class LoginTest {
@Test
public void verifyLogin() {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");
LoginPage login = new LoginPage(driver);
login.login("admin", "admin123");
driver.quit();
}
}
๐งฉ Conclusion
Page Factory in Selenium is a powerful and elegant way to handle web elements in your automation framework. It helps in writing clean, readable, and maintainable code while supporting lazy initialization for better performance.
If you’re already using the Page Object Model, integrating Page Factory is the next logical step to make your framework more scalable and robust
ers"*
* *"Using @FindBy, @FindBys, and @FindAll in Selenium Page Factory"*
---
Would you like me to add a **diagram showing how PageFactory.initElements() links the WebDriver and elements** for better visual clarity in your blog?
Comments
Post a Comment