Playwright + JS · Lesson 7 of 35
Module 7: Classes & Objects for Automation
Try it yourself
Implement NavigationComponent class:
The editor has a worked example. Read it, tweak it, then press Mark as complete.
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();
}
}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.
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.