3

我正在尝试在 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文件中使用生成的电子邮件?

4

1 回答 1

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);
})

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);
})
于 2018-11-08T21:42:27.667 回答