如何在测试之间传递变量数据 [英] How do I pass variable data between tests
本文介绍了如何在测试之间传递变量数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I am trying to do something similar to this post on TestCafe
我正在 helper.js
文件中生成随机电子邮件.我想使用此随机电子邮件登录 test.js
文件.
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.
这就是我在helper.js中创建电子邮件的方式
This is how I am creating my email in the helper.js
var randomemail = 'test+' + Math.floor(Math.random() * 10000) + '@gmail.com'
这就是我要在我的 test.js
文件
.typeText(page.emailInput, randomemail)
我尝试了几件事却没有运气.我该如何在 test.js
文件中使用生成的电子邮件?
I have tried several things without luck. How could I go about using the generated email in my test.js
file?
推荐答案
两个选项:
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);
})
这篇关于如何在测试之间传递变量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文