Search code examples
javascripttestingautomated-testse2e-testingtestcafe

Access the TestCafe `t.testRun` data, using a global import of the test object


I'm trying to add some trace headers to requests we send (as part of our automated tests), to help with debugging. My hope was to use t.testRun.test.id, so that we can link all requests back to the test... however I keep getting TypeError: Cannot read property 'test' of undefined.

I'm using the global import of t. Other than passing the test object (t) to every function, is there a way I can access the testRun object?

To reproduce, just do

import { t } from 'testcafe';

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

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

Solution

  • You can get access to the test run info only from the t object obtained from the test or beforeEach/afterEach hooks:

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

    The globally imported t object contains only available actions like click, typeText, etc.

    Note that you use a non-public undocumented API at your own risk.