0
0
Selenium Javatesting~5 mins

PageFactory initialization in Selenium Java

Choose your learning style9 modes available
Introduction

PageFactory initialization helps create and set up web elements in a simple way. It makes tests easier to read and write.

When you want to organize web elements in a separate class for better code structure.
When you need to initialize all web elements at once before using them in tests.
When you want to reduce repetitive code for finding elements on a page.
When you want to improve readability and maintainability of your Selenium tests.
Syntax
Selenium Java
public class PageName {
    @FindBy(id = "locatorValue")
    private WebElement elementName;

    public PageName(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }
}

The @FindBy annotation tells Selenium how to find the element.

PageFactory.initElements(driver, this) initializes all elements annotated with @FindBy.

Examples
This example shows a login page with username, password fields, and a login button initialized using PageFactory.
Selenium Java
public class LoginPage {
    @FindBy(id = "username")
    private WebElement usernameField;

    @FindBy(id = "password")
    private WebElement passwordField;

    @FindBy(id = "loginBtn")
    private WebElement loginButton;

    public LoginPage(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }
}
This example uses a CSS selector to find the profile link element on the home page.
Selenium Java
public class HomePage {
    @FindBy(css = ".profile-link")
    private WebElement profileLink;

    public HomePage(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }
}
Sample Program

This test script opens a browser, navigates to a login page, initializes the page elements using PageFactory, performs a login, prints a message, and closes the browser.

Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPage {
    @FindBy(id = "username")
    private WebElement usernameField;

    @FindBy(id = "password")
    private WebElement passwordField;

    @FindBy(id = "loginBtn")
    private WebElement loginButton;

    public LoginPage(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }

    public void login(String username, String password) {
        usernameField.sendKeys(username);
        passwordField.sendKeys(password);
        loginButton.click();
    }
}

class TestLogin {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com/login");

        LoginPage loginPage = new LoginPage(driver);
        loginPage.login("user1", "pass123");

        System.out.println("Login attempted");
        driver.quit();
    }
}
OutputSuccess
Important Notes

Always call PageFactory.initElements(driver, this) in the page class constructor.

Use meaningful names for web elements to keep tests clear.

Make sure locators are unique and stable to avoid flaky tests.

Summary

PageFactory initialization simplifies element setup in Selenium tests.

Use @FindBy annotations and call initElements in constructor.

This approach improves code readability and maintenance.