Playwright + JS · Lesson 24 of 35
Module 24: Page Object Model in JavaScript
Try it yourself
Implement CartComponent class:
The editor has a worked example. Read it, tweak it, then press Mark as complete.
// pages/BasePage.js
export class BasePage {
constructor(page) {
this.page = page;
}
async goto(path) {
await this.page.goto(path);
}
}
// pages/LoginPage.js
import { BasePage } from "./BasePage.js";
export class LoginPage extends BasePage {
constructor(page) {
super(page);
this.emailInput = page.getByLabel("Email");
this.passwordInput = page.getByLabel("Password");
this.submitBtn = page.getByRole("button", { name: "Sign In" });
}
async login(email, password) {
await this.emailInput.fill(email);
await this.passwordInput.fill(password);
await this.submitBtn.click();
}
}Welcome to Phase 4! We begin with the Page Object Model, the gold standard design pattern for maintainable automation. In this lesson, you will learn how to build clean ES6 Page Object classes, encapsulate locators, compose reusable Component Objects, and separate user actions from assertions. Let's architect clean page objects!
Part of the Playwright + JS course (Playwright with JavaScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
Keep assertions in your test spec files. Page Objects encapsulate element locators and user interactions.
Finished reading?
Review the worked example in the editor, then mark the lesson complete.