Playwright + JS · Lesson 24 of 35

0/35 done

Module 24: Page Object Model in JavaScript

24. Scalable Page Object Model in ES6

20 minlesson

Try it yourself

Implement CartComponent class:

  1. Constructor initializes this.itemCount = 0.
  2. Method addItem() increments this.itemCount.
  3. Method getItemCount() returns this.itemCount.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

Enterprise Page Object Model in JavaScript

javascript
// 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();
  }
}

Key Takeaways & Walkthrough Summary

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.

Common Doubts & Lesson Q&A

Where should assertions live in the Page Object Model?

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.

JavaScript
Skip