Bananasaurus
Bananasaurus

Reputation: 21

Issue with Playwright running multiple workers and executing hooks multiple times

I'm experiencing an unexpected behavior with Playwright when running tests with multiple workers. According to the Playwright documentation, the beforeAll hook should be executed ONCE PER WORKER. However, I'm observing that the hook is being executed multiple times, resulting in duplicated log messages.

I have a simple test setup with the following structure:

test.beforeAll( async({browser}) => {
    console.log("should be before all tests")
});

test.beforeEach( async()=>{
    console.log('should be before Each test')
})

test.afterAll(async() => {
    console.log('should be after all tests')
});

test('test case',async () => {
});

test('test case 2',async () => {
});

Expected behavior per worker should be: one console.log before, two console.logs (once per test) and one console.log after.

Received behavior:

Running 8 tests using 4 workers
[chromium] › loginAndNavigate.spec.ts:65:5 › test case 2
should be before all tests
should be before Each test
should be after all tests
[chromium] › loginAndNavigate.spec.ts:60:5 › test case 1
should be before all tests
should be before Each test
should be after all tests
[firefox] › loginAndNavigate.spec.ts:60:5 › test case 1
should be before all tests
should be before Each test
should be after all tests
[firefox] › loginAndNavigate.spec.ts:65:5 › test case 2
should be before all tests
should be before Each test
should be after all tests
[webkit] › loginAndNavigate.spec.ts:60:5 › test case 1
should be before all tests
should be before Each test
should be after all tests
[webkit] › loginAndNavigate.spec.ts:65:5 › test case 2
should be before all tests
should be before Each test
should be after all tests
[Mobile Safari] › loginAndNavigate.spec.ts:60:5 › test case 1
should be before all tests
should be before Each test
should be after all tests
[Mobile Safari] › loginAndNavigate.spec.ts:65:5 › test case 2
should be before all tests
should be before Each test
should be after all tests

As you can see, each message is being printed multiple times.

Any insights or suggestions on what might be causing this issue would be greatly appreciated.

I've already checked my Playwright configuration(where the number of workers is set to 4). I'm using the latest version of Playwright

Upvotes: 2

Views: 4787

Answers (2)

Vishal Aggarwal
Vishal Aggarwal

Reputation: 4225

Serial Mode

You may run tests in serial mode , if you need to run beforeAll just once:

// Annotate entire file as serial.
test.describe.configure({ mode: 'serial' });

test.beforeAll( async({browser}) => {
    console.log("should be before all tests")
});

test.beforeEach( async()=>{
    console.log('should be before Each test')
})

test.afterAll(async() => {
    console.log('should be after all tests')
});

test('test case',async () => {
});

test('test case 2',async () => {
});

https://playwright.dev/docs/test-parallel#serial-mode

Upvotes: 0

candre
candre

Reputation: 1568

I beleive this is expected behavour. As you have set number of workers to 4, beforeAll will potentially run up to 4 times per browser. The beforeAll hook is also done before each new browser.

To demonstrate, in the run below, I have changed to max 2 workers and are running 4 tests in 2 different browsers:

Running 8 tests using 2 workers

  ✓  1 [chrome web] › multipleworkers.spec.ts:19:5 › test case 3 @1 (1ms)
  ✓  2 [chrome web] › multipleworkers.spec.ts:15:5 › test case @1 (1ms)
should be before all tests
should be before all tests
should be before Each test
should be before Each test
  ✓  3 [chrome web] › multipleworkers.spec.ts:17:5 › test case 2 @1 (0ms)
  ✓  4 [chrome web] › multipleworkers.spec.ts:21:5 › test case 4 @1 (0ms)
should be before Each test
should be after all tests
should be before Each test
should be after all tests
  ✓  5 [firefox web] › multipleworkers.spec.ts:19:5 › test case 3 @1 (1ms)
  ✓  6 [firefox web] › multipleworkers.spec.ts:15:5 › test case @1 (1ms)
should be before all tests
should be before Each test
  ✓  7 [firefox web] › multipleworkers.spec.ts:17:5 › test case 2 @1 (0ms)
should be before all tests
should be before Each test
  ✓  8 [firefox web] › multipleworkers.spec.ts:21:5 › test case 4 @1 (0ms)
should be before Each test
should be after all tests
should be before Each test
should be after all tests

Here, beforeAll are run 4 times; 2 workers and 2 different browsers while beforeEach are run 8 times.

Upvotes: 0

Related Questions