Skip to content Skip to sidebar Skip to footer

How Do I Pass Variable Data Between Tests

I am trying to do something similar to this post on TestCafe I am generating a random email in my helper.js file. I would like to use this random email to log in the test.js file.

Solution 1:

Two options:

1) Use the Fixture Context Object (ctx)

fixture(`RANDOM EMAIL TESTS`)
    .before(async ctx => {
        /** Do this to initialize the random email and if you only want one random email 
         *  for the entire fixture */

        ctx.randomEmail = `test+${Math.floor(Math.random() * 1000)}@gmail.com`;
    })
    .beforeEach(async t => {
        // Do this if you want to update the email between each test

        t.fixtureCtx.randomEmail = `test+${Math.floor(Math.random() * 1000)}@gmail.com`;
    })

test('Display First Email', async t => {
    console.log(t.fixtureCtx.randomEmail);
})

test('Display Second Email', async t => {
    console.log(t.fixtureCtx.randomEmail);
})

2) Declare a variable outside of the fixture

let randomEmail = '';

fixture(`RANDOM EMAIL TESTS`)
    .beforeEach(async t => {
        // Do this if you want to update the email between each test

        randomEmail = `test+${Math.floor(Math.random() * 1000)}@gmail.com`;
    })

test('Display First Email', async t => {
    console.log(randomEmail);
})

test('Display Second Email', async t => {
    console.log(randomEmail);
})

Post a Comment for "How Do I Pass Variable Data Between Tests"