TestCafe beforeEach 钩子 - 如何执行一个函数并声明一个变量
TestCafe beforeEach hook - how to execute a function and declare a variable
我正在使用 TestCafe 并寻找一种解决方案来在 beforeEach 挂钩中做两件事:
1.执行一个功能(每次测试前登录)
2. 创建独特的测试数据
我可以分别实现,但不能同时实现:
这适用于登录用户:
fixture('My Component')
.beforeEach(login(user, password)
)
这可以为每个测试用例创建新的测试数据:
fixture(`My Component`)
.beforeEach(async t => {
randomLastName = faker.name.lastName();
})
但我还没有找到在一个钩子中同时实现两者的解决方案。而且,我从文档中了解到,使用两个 beforeEach 挂钩将导致第一个被覆盖。
我目前的实现是在 beforeEach 挂钩中执行登录并在每个测试用例中创建测试数据,这比我想要的更冗长,例如,每个测试用例包含
test('My Test', async (t) => {
let randomLastName = faker.name.lastName();
// ...
}
不胜感激!
一个解决方案是在每次测试执行前使用Test Context准备任何类型的数据上下文
fixture('My Component')
.beforeEach(async (t) => {
// login method
login(user, password);
// inject test data in the test context
t.ctx.inputData = {
randomLastName: faker.name.lastName()
};
});
test('My Test', async (t) => {
// read test data from test context
const inputData = t.ctx.inputData;
const randomLastName = inputData.randomLastName;
// ...
}
我正在使用 TestCafe 并寻找一种解决方案来在 beforeEach 挂钩中做两件事: 1.执行一个功能(每次测试前登录) 2. 创建独特的测试数据
我可以分别实现,但不能同时实现:
这适用于登录用户:
fixture('My Component')
.beforeEach(login(user, password)
)
这可以为每个测试用例创建新的测试数据:
fixture(`My Component`)
.beforeEach(async t => {
randomLastName = faker.name.lastName();
})
但我还没有找到在一个钩子中同时实现两者的解决方案。而且,我从文档中了解到,使用两个 beforeEach 挂钩将导致第一个被覆盖。
我目前的实现是在 beforeEach 挂钩中执行登录并在每个测试用例中创建测试数据,这比我想要的更冗长,例如,每个测试用例包含
test('My Test', async (t) => {
let randomLastName = faker.name.lastName();
// ...
}
不胜感激!
一个解决方案是在每次测试执行前使用Test Context准备任何类型的数据上下文
fixture('My Component')
.beforeEach(async (t) => {
// login method
login(user, password);
// inject test data in the test context
t.ctx.inputData = {
randomLastName: faker.name.lastName()
};
});
test('My Test', async (t) => {
// read test data from test context
const inputData = t.ctx.inputData;
const randomLastName = inputData.randomLastName;
// ...
}