Skip to content

Instantly share code, notes, and snippets.

@Xotabu4
Created July 13, 2023 11:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Xotabu4/4c0006d0470405693ca85dfc1aa21a67 to your computer and use it in GitHub Desktop.
Save Xotabu4/4c0006d0470405693ca85dfc1aa21a67 to your computer and use it in GitHub Desktop.
Playwright typescript 5.x @step decorator for pageobject methods
import { test } from '@playwright/test';
/**
* Decorator that wraps a function with a Playwright test step.
* Used for reporting purposes.
*
* @example
```
import { step } from './step_decorator';
class MyTestClass {
@step
async myTestFunction() {
// Test code goes here
}
}
```
*/
export function step<This, Args extends any[], Return>(
target: (this: This, ...args: Args) => Promise<Return>,
context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Promise<Return>>,
) {
async function replacementMethod(this: This, ...args: Args): Promise<Return> {
// @ts-expect-error error
const name = `${this.constructor.name}.${context.name as string}(${args.map((a) => JSON.stringify(a)).join(',')})`;
return test.step(name, async () => target.call(this, ...args));
}
return replacementMethod;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment