Logic Building Bootcamp · Lesson 5 of 5

OOP Essentials — Classes, Inheritance, Magic Methods, Decorators

Real SDET code lives in classes — page objects, API clients, base test cases. Here are the essentials you'll actually use, with no heavy theory.

By Shahriyar · Updated

The idea, in one line

A class is a blueprint. From it you make objects that carry their own data and their own actions. Master four small pieces and you can read almost any test framework.

The four pieces

See it work

▸ try it
import functools

# Decorator that retries a flaky step
def retry(times):
    def outer(fn):
        @functools.wraps(fn)     # keep fn's real name/docstring
        def inner(*args, **kwargs):
            for attempt in range(times):
                try:
                    return fn(*args, **kwargs)
                except AssertionError:
                    if attempt == times - 1:
                        raise
        return inner
    return outer

class BasePage:
    def __init__(self, url):
        self.url = url
    def __repr__(self):          # useful in a failed assert
        return f"{type(self).__name__}({self.url!r})"

class LoginPage(BasePage):
    def __init__(self, url, user):
        super().__init__(url)    # run parent setup
        self.user = user

    @retry(times=3)
    def submit(self):
        assert self.user, "no user set"
        return "ok"

print(LoginPage("/login", "amy"))   # LoginPage('/login')

Read it top to bottom: BasePage holds a url and prints itself nicely, LoginPage reuses that setup with super().__init__ and adds a user, and @retry wraps submit so a flaky assert gets a few more tries before it fails.

Advanced — why each piece earns its place

Grounded in the official Python docs (classes) and functools

All lessons in Logic Building Bootcamp

  1. Pattern 1 — Counting how often things appear
  2. Pattern 2 — Two Pointers
  3. Pattern 3 — Sliding Window
  4. Pattern 4 — Index Math & Matrix Walks
  5. OOP Essentials — Classes, Inheritance, Magic Methods, Decorators