Playwright + Java · Lesson 26 of 38
Module 26: Page Object Model
Your task
Create a simple LoginPage constructor:
Write it in the editor, then press Check answer (Ctrl + Enter).
// 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();
}
}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.
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.
___