我正在写一个剧本测试,并有一个前每个。我需要一个在beforeEach函数中生成的值,以便在测试函数本身中可用。
test.beforeEach(async ({ page, request }) => {
const user = await createUser(request);
// pass the user variable to each test
});
test.describe('do something', () => {
test('do something 1', async ({ page }) => {
// have access to the variable here
});
});
您可以通过在beforeEach函数之外定义变量,然后在beforeEach函数中分配它来实现这一点。这将使同一范围内的所有测试都可以访问它。例子:
// Define the user variable outside the beforeEach function
let user;
test.beforeEach(async ({ page, request }) => {
// Generate the user and assign it to the user variable
user = await createUser(request);
});
test.describe('do something', () => {
test('do something 1', async ({ page }) => {
// Use the user variable in your test
console.log(user);
});
});
希望有帮助。