0
0
Selenium Javatesting~15 mins

PageFactory initialization in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify login page elements are initialized using PageFactory
Preconditions (2)
Step 1: Open browser and navigate to the login page URL
Step 2: Initialize the LoginPage class using PageFactory
Step 3: Verify that the username input field is displayed
Step 4: Verify that the password input field is displayed
Step 5: Verify that the login button is displayed
✅ Expected Result: All login page elements (username, password, login button) are properly initialized and visible on the page
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Assert username input field is displayed
Assert password input field is displayed
Assert login button is displayed
Best Practices:
Use PageFactory.initElements to initialize web elements
Use @FindBy annotations for element locators
Use explicit waits if necessary before assertions
Keep locators simple and reliable (e.g., by id or name)
Automated Solution
Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class LoginPage {
    private WebDriver driver;

    @FindBy(id = "username")
    private WebElement usernameInput;

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

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

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

    public boolean isUsernameDisplayed() {
        return usernameInput.isDisplayed();
    }

    public boolean isPasswordDisplayed() {
        return passwordInput.isDisplayed();
    }

    public boolean isLoginButtonDisplayed() {
        return loginButton.isDisplayed();
    }
}

public class LoginPageTest {
    private WebDriver driver;
    private LoginPage loginPage;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("https://example.com/login");
        loginPage = new LoginPage(driver);
    }

    @Test
    public void testLoginPageElementsAreInitialized() {
        assertTrue(loginPage.isUsernameDisplayed(), "Username input should be displayed");
        assertTrue(loginPage.isPasswordDisplayed(), "Password input should be displayed");
        assertTrue(loginPage.isLoginButtonDisplayed(), "Login button should be displayed");
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This code defines a LoginPage class representing the login page. It uses @FindBy annotations to locate elements by their id. The constructor calls PageFactory.initElements(driver, this) to initialize these elements.

The test class LoginPageTest opens the browser, navigates to the login page URL, and creates a LoginPage object. It then asserts that the username input, password input, and login button are all displayed on the page.

Using PageFactory helps keep element locators organized and initialization clean. Assertions verify the elements are present and visible, matching the manual test case steps.

Common Mistakes - 3 Pitfalls
Not calling PageFactory.initElements in the page class constructor
Using complex or brittle XPath locators instead of simple locators like id or name
Not verifying element visibility before interacting or asserting
Bonus Challenge

Now add data-driven testing with 3 different login URLs to verify PageFactory initialization on each

Show Hint