gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:05:32 26 4
gpt4 key购买 nike

我正在尝试做类似的事情 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) 使用夹具上下文对象 (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);
})

关于javascript - 如何在测试之间传递可变数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51488794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com