Playwright + TS · Lesson 6 of 36

0/36 done

Module 6: Classes & OOP for Automation

6. Classes, Constructors & Access Modifiers for POM

14 minlesson

Your task

Complete the CartPage class inheriting from BasePage:

  1. Call super(baseUrl) in the constructor.
  2. Implement getCartUrl to return this.getUrl("/cart").

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

Classes for Page Object Models

typescript
export class BasePage {
  // protected: accessible within this class and subclasses
  protected readonly baseUrl: string;

  constructor(baseUrl: string) {
    this.baseUrl = baseUrl;
  }

  public getUrl(path: string): string {
    return `${this.baseUrl}${path}`;
  }
}

// Inheritance with extends & super:
export class LoginPage extends BasePage {
  // private: only accessible inside LoginPage
  private readonly loginPath: string = "/login";

  constructor(baseUrl: string) {
    super(baseUrl); // calls constructor of BasePage
  }

  public getLoginUrl(): string {
    return this.getUrl(this.loginPath);
  }
}

Key Takeaways & Walkthrough Summary

Object-Oriented Programming is essential for building scalable automation frameworks, especially the Page Object Model. In this lesson, we focus purely on the OOP concepts needed for Playwright: classes, constructors, methods, access modifiers, and inheritance. We keep it practical and directly applicable to automation. Let's build our first class!

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

Common Doubts & Lesson Q&A

Why mark locators as readonly in POM classes?

readonly prevents accidental reassignment of the locator object during test execution, ensuring test reliability.

Ready? Solve the task in the editor

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

TypeScript
Skip