Playwright + Java · Lesson 6 of 38
Module 6: Object-Oriented Programming
Try it yourself
Create a TestUser class:
The editor has a worked example. Read it, tweak it, then press Mark as complete.
// Base Page with encapsulated driver/page reference:
public abstract class BasePage {
protected String pageUrl;
public BasePage(String pageUrl) {
this.pageUrl = pageUrl;
}
public void open() {
System.out.println("Navigating to: " + this.pageUrl);
}
}
// Child Page inheriting common capabilities:
public class LoginPage extends BasePage {
private String usernameSelector = "#username";
public LoginPage() {
super("https://targetqa.com/login");
}
public void login(String user, String pass) {
open();
System.out.println("Typing credentials into " + usernameSelector);
}
}Object-Oriented Programming is the foundational architecture of the Page Object Model. In this lesson, we explore classes, encapsulation, inheritance with extends and super, and polymorphism tailored specifically for test automation. Let's design scalable classes!
Part of the Playwright + Java course (Playwright with Java). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
Encapsulation ensures tests interact through business actions (e.g. login()) rather than directly manipulating raw locator strings.
Finished reading?
Review the worked example in the editor, then mark the lesson complete.