6. Classes, Constructors & Access Modifiers for POM

Classes for Page Object Models

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);
}
}

Task

  • Complete the `CartPage` class inheriting from `BasePage`:
  • 1. Call `super(baseUrl)` in the constructor.
  • 2. Implement `getCartUrl` to return `this.getUrl("/cart")`.
πŸ’‘Common doubts
Did you like the problem?
Lesson 6 of 36
TypeScript
Loading code editor…
Next