Latest post from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

In real-time automation projects, test execution is never limited to a single browser or a single environment . Applications must be validated across multiple browsers (Chrome, Firefox, Edge) and multiple environments such as QA, UAT, and Staging before going live. A well-designed Selenium + Java + Cucumber automation framework should allow testers to switch browsers and environments easily without changing test scripts . This blog explains how to manage test execution efficiently across different browsers and environments using best practices followed in real projects. Why Multi-Browser and Multi-Environment Testing Is Important Different users use different browsers QA, UAT, and Staging environments have different configurations Bugs may appear only in specific environments or browsers Same test cases must be validated everywhere before production release Common Challenges Testers Face Hardcoded browser names and URLs Maintaining separate test scripts for each environment Browse...

Understanding Page Factory and Its Annotations in Selenium

๐Ÿงฉ 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.

This tells Selenium to locate all elements declared in this class using the specified annotations.

๐Ÿงพ 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

Popular posts from this blog

Ensuring Thread Safety in Parallel Test Execution with Selenium, Cucumber, and Java