Playwright + Python · Lesson 25 of 37
Module 25: Page Object Model
Your task
Create a LoginPage class:
Write it in the editor, then press Check answer (Ctrl + Enter).
class BasePage:
def __init__(self, page):
self.page = page
def goto(self, path: str):
self.page.goto(path)
class LoginPage(BasePage):
def __init__(self, page):
super().__init__(page)
self.username_input = page.get_by_label("Username")
self.password_input = page.get_by_label("Password")
self.login_btn = page.get_by_role("button", name="Log In")
def login(self, username, password):
self.username_input.fill(username)
self.password_input.fill(password)
self.login_btn.click()Welcome to Phase 4! We begin advanced architecture with the Page Object Model. In this lesson, you will architect a BasePage class, encapsulate Playwright locators inside strongly typed Python classes, and create reusable component objects. Let's design scalable page objects!
Part of the Playwright + Python course (Playwright with Python). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
In Playwright, locators are lazy queries and do not execute until called, making it completely safe and clean to initialize them in __init__.
Ready? Solve the task in the editor
Use Check answer to mark it complete. Hints are there if you need them.
___