Search code examples
playwrightplaywright-typescript

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}";
}

Solution

  • 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