如何在测试之间传递可变数据

Rob*_*t K 3 javascript testing automated-tests e2e-testing testcafe

我正在尝试做一些类似于TestCafe上的这篇文章

我在我的helper.js文件中生成了一封随机电子邮件。我想使用这个随机电子邮件来登录test.js文件。

这就是我在 helper.js 中创建电子邮件的方式

var randomemail = 'test+' + Math.floor(Math.random() * 10000) + '@gmail.com'
Run Code Online (Sandbox Code Playgroud)

这就是我想在我的test.js文件中使用它的方式

.typeText(page.emailInput, randomemail)
Run Code Online (Sandbox Code Playgroud)

我已经尝试了几件事但没有运气。我怎样才能在我的test.js文件中使用生成的电子邮件?

dap*_*985 5

两种选择:

1)使用夹具上下文对象(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);
})
Run Code Online (Sandbox Code Playgroud)

2)在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);
})
Run Code Online (Sandbox Code Playgroud)