【问题标题】:How to get the current test name for Playwright in TypeScript?如何在 TypeScript 中获取 Playwright 的当前测试名称?
【发布时间】:2022-12-25 23:21:33
【问题描述】:

这是我在使用 Playwright 时为 NUnit 实现的内容。我想用 Playwright 为 TypeScript 做同样的事情。

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

【问题讨论】:

    标签: playwright playwright-typescript


    【解决方案1】:

    您可以使用打字稿提取有关文件和实际测试的一些元信息。

    此信息可以从TestInfo 类中获取。

    这是一个例子:

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

    输出:

    random test
    tests\dummy.test.ts
    

    这是此功能的官方文档: https://playwright.dev/docs/api/class-testinfo

    另一种获取此类信息的方法是通过 reporter 类,它有钩子: 更多信息:https://playwright.dev/docs/api/class-reporter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-04
      • 2012-01-12
      • 1970-01-01
      相关资源
      最近更新 更多