Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

tests\\dummy.test.ts'
random test'

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading