我正在尝试做类似于这个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
文件中使用生成的电子邮件?
两种选择:
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);
})