Search code examples
javascripttestingautomated-testse2e-testingtestcafe

How to share a global variable between test files from a test in TestCafe?


I'm manually setting auth cookies for my login purpose and I would like to share the Auth token across my tests. The very first time I have to perform a login in a test and then I have to save the auth token in a variable and share it across the test files.

Here is the code snippet to explain what and how I'm trying to do:

loginTest.js :

let authToken = null;

fixture`Login test`
  .page(inputData.url)
  .beforeEach(async (t) => {
    const nextMonth = new Date();
    nextMonth.setMonth(nextMonth.getMonth() + 1);
    await t.navigateTo(inputData.url).then(await setCookie('AUTH_COOKIE_ID', authToken, nextMonth));
  });

test
  .before(async () => {
    await loginPage.login(inputData.firstUserEmailId, inputData.firstUserPassword);
    authToken = await getCookie('AUTH_COOKIE_ID');
  })('Verify login test', async (t) => {
    await loginPage.goToPeople(personName);
    await t
      .expect(loginPage.personName.exists)
      .ok();
  });

Now, after the test, I have the actual authToken (not null) and if I have to share the authToken variable across all my tests in all my files then how do I do? with this coding design I can share authToken in the same file (test suite). for example:

I have a file peopleTest.js :

fixture`People test`
  .page(inputData.url)
  .beforeEach(async (t) => {
    const nextMonth = new Date();
    nextMonth.setMonth(nextMonth.getMonth() + 1);
    await t.navigateTo(inputData.url).then(await setCookie('AUTH_COOKIE_ID', loginTest.authToken, nextMonth));
  });

test('Verify people test', async (t) => {
    await loginPage.goToPeople(personName);
    await t
      .expect(loginPage.personName.exists)
      .ok();
  });

In the above test, if I can do loginTest.authToken that would be great.

PS: In case, people are wondering why I'm doing setting cookie instead of using useRole. Just to let you know that the useRole didn't work in my setup as the application sets the cookie manually in my local env so I have to manually set the cookie as a login workaround.


Solution

  • Refer to the Extract Reusable Test Code receipt to find information on how you can share your test code between your test cases.

    Also, you can use the fixture context object if you want to share your object only between tests of a particular fixture: Sharing Variables Between Fixture Hooks and Test Code.

    For instance:

    helper.js

    var authToken = 111;
    
    function setToken(x) {
        authToken = x;
    }
    
    function getToken(x) {
        return authToken;
    }
    
    export { setToken, getToken };
    

    test.js

    import { getToken, setToken } from './helper.js'
    
    fixture("test1")
            .page("http://localhost");
    
    test('test1', async t => {
        console.log('token: ' + getToken());
        setToken(111);
    });
    

    test1.js

    import { getToken } from './helper.js'
    
    fixture("test2")
            .page("http://localhost");
    
    test('test2', async t => {
        console.log('token2: ' + getToken());
    });
    

    See also:

    import

    export