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

问题描述 投票:2回答:1

我正在尝试做类似于这个post on TestCafe的事情

我在helper.js文件中生成随机电子邮件。我想用这个随机电子邮件登录test.js文件。

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

var randomemail = 'test+' + Math.floor(Math.random() * 10000) + '@gmail.com'

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

.typeText(page.emailInput, randomemail)

我没试过好几件事。我怎样才能在test.js文件中使用生成的电子邮件?

automated-tests e2e-testing testcafe
1个回答
3
投票

两种选择:

1)使用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)在夹具外面声明一个变量

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);
})
© www.soinside.com 2019 - 2024. All rights reserved.