Playwright + TS · Lesson 6 of 36
Module 6: Classes & OOP for Automation
Your task
Complete the CartPage class inheriting from BasePage:
super(baseUrl) in the constructor.getCartUrl to return this.getUrl("/cart").Write it in the editor, then press Check answer (Ctrl + Enter).
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);
}
}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.
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.
___