Greg Finzer
Greg Finzer

Reputation: 7054

How to get the current test name for Playwright in TypeScript?

Here is what I have implemented for NUnit when using Playwright. I would like to do the same thing for TypeScript with Playwright.

public string GetTestClassMethod()
{
    return $"{TestContext.CurrentContext.Test.ClassName}.{TestContext.CurrentContext.Test.MethodName}";
}

Upvotes: 4

Views: 7980

Answers (1)

Infern0
Infern0

Reputation: 2814

you can extract some meta info about the file and actual test using typescript.

This info can be fetched from TestInfo class.

Here is an example:

test('random test', async ({ page }, testInfo) => {
    await page.goto('https://playwright.dev/');

    console.log(testInfo.title);
    console.log(testInfo.titlePath);
});

output:

random test
tests\\dummy.test.ts

Here is official documentation for this functionality: https://playwright.dev/docs/api/class-testinfo

Another way to fetch such info is trough reporter class, with its hooks: More info: https://playwright.dev/docs/api/class-reporter

Upvotes: 6

Related Questions