Selenium + Java · Lesson 35 of 44

0/44 done

Module 35: Page Object Model

35. Enterprise Page Object Model Design & PageFactory

18 minlesson

Your task

Implement simple LoginPage with By locators:

  1. Store private final WebDriver driver.
  2. Store By usernameLocator = By.id("user").
  3. Provide method enterUsername(String user) that calls driver.findElement(usernameLocator).sendKeys(user).

Write it in the editor, then press Check answer (Ctrl + Enter).

Enterprise POM with By Locators (Industry Standard)

java
public class LoginPage {
    private final WebDriver driver;
    private final By usernameInput = By.id("username");
    private final By passwordInput = By.id("password");
    private final By submitBtn = By.id("login-btn");

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

    public void login(String user, String pass) {
        driver.findElement(usernameInput).sendKeys(user);
        driver.findElement(passwordInput).sendKeys(pass);
        driver.findElement(submitBtn).click();
    }
}

Key Takeaways & Walkthrough Summary

Welcome to Phase 5! The Page Object Model is the cornerstone design pattern of professional test automation. In this lesson, you will architect a BasePage, encapsulate WebElements, and compare classical By locators with Selenium's PageFactory and FindBy annotations. Let's design scalable page objects!

Part of the Selenium + Java course (Selenium with Java). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Is PageFactory still recommended in modern Selenium?

Many modern enterprise teams prefer standard By locators over PageFactory (@FindBy) because PageFactory hides initialization timing and complicates dynamic waiting.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip