Decorators

What is a decorator?

A decorator is just a function that runs once when the code is loaded.
It receives whatever it decorates (class, method, property…) and can replace or augment it.  Decorators let us inject cross‑cutting behaviour without touching the core logic.

Class vs method decorator

Class decorator

At load time we create Proxy, stash a reference to the original class, and hand Proxy back to the runtime. When someone later does new MobiscrollDatePicker(), the proxy decides whether to instantiate the desktop or mobile implementation.

Method decorator

Receives the original method and metadata about what you're decorating (context.name). Returns a new function that wraps the call in  test.step, giving us pretty test reports.

Method decorator

 

Take‑aways

  • Decorators execute at load time, not at call time.

  • A class decorator can replace the whole class, a method decorator can only replace that one method.

  • In our codebase, @Desktop + @Mobile dynamically pick the right implementation, while @step adds nice Playwright reporting 

Context request vs global request

Good for requests that need to share session data with the browser context
It's not tied to any particular page but inherits the context's configuration, such as user agent and any cookies or local storage data already set within the context. Updates cookies

1

context.request

Best for requests specifically associated with the activity or data of a particular page or headers specific to that page.
Updates cookies

2

page.request

Isolated instance, a standalone request outside of Playwright's page or context scope
Does not use or updates
updates cookies from the browser context

3

request fixture

Usage

Workers

These processes are OS processes, running independently, orchestrated by the test runner.

All workers have identical environments and each starts its own browser.

Thank You!

Questions?

Typescript decorators

By TenantCloud

Typescript decorators

  • 18