Playwright + JS · Lesson 7 of 35

0/35 done

Module 7: Classes & Objects for Automation

7. Classes & Object-Oriented Patterns for Page Objects

15 minlesson

Try it yourself

Implement NavigationComponent class:

  1. Constructor accepts page.
  2. Method getHomeLink() returns page.getByRole("link", { name: "Home" }).

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

Classes for Page Object Architecture

javascript
export class BasePage {
  constructor(page) {
    this.page = page;
  }

  async navigate(path) {
    await this.page.goto(path);
  }
}

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

Object-Oriented Programming in JavaScript powers the Page Object Model. In this lesson, we focus purely on the OOP concepts needed for automation: classes, constructors, methods, and class inheritance using extends and super. Let's build our first Page Object class!

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

Why do we call super(page) in child classes?

super(page) invokes the parent BasePage constructor, initializing this.page so child classes have access to browser methods.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

JavaScript
Skip