Created
July 13, 2023 11:58
-
-
Save Xotabu4/4c0006d0470405693ca85dfc1aa21a67 to your computer and use it in GitHub Desktop.
Playwright typescript 5.x @step decorator for pageobject methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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