Playwright + Java · Lesson 26 of 38

0/38 done

Module 26: Page Object Model

26. Enterprise Page Object Model Architecture in Java

18 minlesson

Your task

Create a simple LoginPage constructor:

  1. Accept Page page.
  2. Initialize Locator emailInput with page.getByLabel("Email").
  3. Expose getEmailInput() returning the locator.

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

Enterprise Page Object Architecture

java
// BasePage.java
public abstract class BasePage {
    protected final Page page;

    public BasePage(Page page) {
        this.page = page;
    }

    public void navigate(String path) {
        page.navigate(path);
    }
}

// LoginPage.java
public class LoginPage extends BasePage {
    private final Locator usernameInput;
    private final Locator passwordInput;
    private final Locator submitBtn;

    public LoginPage(Page page) {
        super(page);
        this.usernameInput = page.getByLabel("Username");
        this.passwordInput = page.getByLabel("Password");
        this.submitBtn = page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Log In"));
    }

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

Key Takeaways & Walkthrough Summary

Welcome to Phase 4! We kick off advanced architecture with the Page Object Model. In this lesson, you will architect a robust BasePage class, encapsulate Playwright Locators inside strongly typed Java classes, and create reusable component objects. Let's design scalable page objects!

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

Common Doubts & Lesson Q&A

Should Playwright Page Objects use Selenium's @FindBy PageFactory?

No! Playwright Java does not use PageFactory. Page.locator() and getBy*() methods are lazy by default, making PageFactory obsolete and unnecessarily complex.

Ready? Solve the task in the editor

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

Java
Skip