Playwright + Python · Lesson 25 of 37

0/37 done

Module 25: Page Object Model

25. Scalable Page Object Model Architecture in Python

18 minlesson

Your task

Create a LoginPage class:

  1. __init__(self, page) with self.page = page.
  2. self.email_input = page.get_by_label("Email").
  3. Method fill_email(self, email) calling self.email_input.fill(email).

Write it in the editor, then press Check answer (Ctrl + Enter).

Enterprise Page Object Model in Python

python
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()

Key Takeaways & Walkthrough Summary

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.

Common Doubts & Lesson Q&A

Should locators be initialized in __init__ or as methods?

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.

Python
Skip