playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Bug]: When fullyParallel=false and workers=1, and there are multiple test cases across multiple test files, test.beforeAll etc hooks get executed ONLY before and after the execution of test cases in the FIRST test file.

Open Debo251 opened this issue 7 months ago • 1 comments

Version

1.50

Steps to reproduce

Create a basic playwright framework:

  1. playwright.config.js: // playwright.config.js const { defineConfig } = require('@playwright/test');

module.exports = defineConfig({

//globalSetup: require.resolve('./global-setup'), //globalTeardown: require.resolve('./global-teardown'), testDir: 'tests', timeout: 30000, reporter: 'html', use: { // Set the browser to Chrome and headed mode channel: 'chrome', browserName: 'chromium', // Use 'chromium' for Chrome headless: false, // Set to false to run in headed mode }, fullyParallel:false, workers: 1, });

  1. Create a simple setupFixture: const { test: base } = require('@playwright/test'); let page, browser, context; const test = base.extend({ page: async ({}, use) => { browser = await base.chromium.launch({ headless: false }); context = await browser.newContext(); page = await context.newPage(); await use(page); await page.close(); await context.close(); await browser.close(); } });

test.beforeAll(async ({page},testInfo) => {

console.log("HERE IN BEFORE ALL ", testInfo.title);
console.log(testInfo.workerIndex);

});

test.beforeEach(async ({page},testInfo) => { console.log("HERE IN BEFORE EACH ", testInfo.title); console.log(testInfo.workerIndex); });

test.afterEach(async ({page},testInfo) => { console.log("HERE IN AFTER EACH ", testInfo.title); console.log(testInfo.workerIndex);

});

test.afterAll(async ({page},testInfo) => { console.log("HERE IN AFTER ALL ", testInfo.title); console.log(testInfo.workerIndex); });

module.exports = { test };

  1. Create 2 test files (.spec.js files) & in each test file write one test cases each. Here is a sample test case: const {test} = require('../setupFixture'); const { expect } = require('@playwright/test');

test('Verify Facebook title', async ({ page }) => { await page.goto('https://www.facebook.com'); const title = await page.title(); expect(title).toBe('Facebook – log in or sign up');

});

Expected behavior

On running the command 'npx playwright test'

test.beforeAll should get executed before execution of the test cases across all test files test.beforeEach should get executed before execution of every test case irrespective of which test file it is in test.afterEach should get executed before execution of every test case irrespective of which test file it is in test.afterAll should get executed after execution of the all test cases across all test files

Actual behavior

test.beforeAll gets executed before running test cases of first test file only test.beforeEach gets executed before every test case of first test file only test.afterAll gets executed after running test cases of first test file only test.afterEach gets executed after every test case of first test file only

The hooks don't run before or after execution of test cases in the second test file at all

Additional context

No response

Environment

System:
    OS: Windows 11 10.0.22631
    CPU: (14) x64 Intel(R) Core(TM) Ultra 7 165U
    Memory: 16.01 GB / 31.46 GB
  Binaries:
    Node: 22.14.0 - C:\Program Files\nodejs\node.EXE
    npm: 11.1.0 - C:\Program Files\nodejs\npm.CMD
  IDEs:
    VSCode: 1.100.2 - C:\Users\ddas175\AppData\Local\Programs\Microsoft VS Code\bin\code.CMD
  npmPackages:
    @playwright/test: ^1.50.0 => 1.50.0

Debo251 avatar Jun 11 '25 12:06 Debo251

@Debo251 You should not call beforeAll() and other hooks in the helper file, but instead do that in each test file. Here is a longer explanation. Please let me know whether this helps.

dgozman avatar Jun 11 '25 12:06 dgozman

Closing as there is no action item for Playwright here. If you still encounter problems, please file a new issue by filling in the "Bug Report" template and link to this one.

dgozman avatar Jun 16 '25 08:06 dgozman