0

I am writing a test in playwrite and have a before each. I need a value that is generated in the beforeEach function to be available in the test function itself.

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
  });
});
Matt
  • 487
  • 1
  • 6
  • 16

1 Answers1

0

You can achieve this by defining the variable outside of the beforeEach function and then assigning it within the beforeEach function. This will make it accessible to all the tests within the same scope. Example:

// 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);
  });
});

Hope it helps.

InvstIA
  • 84
  • 4
  • Thanks I will have a go with that. Wasn't sure how that would go with multiple tests and playwrite running a parallel. – Matt Apr 05 '23 at 09:05