首页 > 解决方案 > 使用测试对象的全局导入访问 TestCafe `t.testRun` 数据

问题描述

我正在尝试向我们发送的请求添加一些跟踪标头(作为我们自动化测试的一部分),以帮助调试。我希望使用t.testRun.test.id,以便我们可以将所有请求链接回测试......但是我不断得到TypeError: Cannot read property 'test' of undefined.

我正在使用t. 除了将测试对象 ( t) 传递给每个函数之外,还有什么方法可以访问 testRun 对象吗?

要重现,只需执行

import { t } from 'testcafe';

fixture('<name>')
  .page('<page>')
  .beforeEach(async () => {
    console.log(await t.testRun.test.id);
  };

test('<name>', async () => {/*...*/});

标签: javascripttestingautomated-testse2e-testingtestcafe

解决方案


您只能从从or钩子t获得的对象访问测试运行信息:testbeforeEach/afterEach

  .beforeEach(async t => {
    console.log(await t.testRun.test.id);
  });

全局导入的t对象仅包含可用的操作,如 click、typeText 等。

请注意,您使用非公开的未记录 API 需要您自担风险。


推荐阅读