Playwright + Python · Lesson 6 of 37

0/37 done

Module 6: Object-Oriented Programming

6. OOP Architecture: Classes, Inheritance & Base Page Models

16 minlesson

Your task

Create a TestUser class:

  1. __init__(self, username: str, role: str = "STANDARD").
  2. Property or method is_admin() that returns True if self.role == "ADMIN", else False.

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

OOP for Page Object Models

python
class BasePage:
    def __init__(self, driver):
        self.driver = driver

    def navigate(self, url: str):
        self.driver.get(url)

class LoginPage(BasePage):
    def __init__(self, driver):
        super().__init__(driver)
        self.username_input = "#username"
        self.password_input = "#password"

    def login(self, username, password):
        self.navigate("https://targetqa.com/login")
        print(f"Typing {username} into {self.username_input}")

Key Takeaways & Walkthrough Summary

Object-Oriented Programming is the foundation of the Page Object Model in Python. In this lesson, you will master classes, __init__ constructors, instance methods, and class inheritance with super() to build clean BasePage abstractions. Let's design scalable classes!

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

What is 'self' in Python class methods?

self represents the specific instance of the class, allowing access to instance attributes and methods.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Python
Skip