diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index 6cfcf420a..7a9e0349c 100644 --- a/lib/helper/Playwright.js +++ b/lib/helper/Playwright.js @@ -554,9 +554,10 @@ class Playwright extends Helper { return } - // Start browser if not manually started and not already running - // Browser should start in singleton mode (restart: false) or when restart strategy is enabled - if (!this.options.manualStart && !this.isRunning) { + // Start browser if not manually started and not already running. + // With the browser restart strategy, _before() starts a browser for each test, + // and a browser started here would never be closed (#5784) + if (!this.options.manualStart && !this.isRunning && !restartsBrowser()) { this.debugSection('Session', 'Starting singleton browser session') return this._startBrowser() } diff --git a/test/unit/helper/Playwright_restart_test.js b/test/unit/helper/Playwright_restart_test.js new file mode 100644 index 000000000..0d951b82f --- /dev/null +++ b/test/unit/helper/Playwright_restart_test.js @@ -0,0 +1,29 @@ +import { expect } from 'chai' +import Playwright from '../../../lib/helper/Playwright.js' + +describe('Playwright restart strategy', () => { + function countBrowserStarts(restart) { + const helper = new Playwright({ browser: 'chromium', url: 'http://localhost', restart }) + let starts = 0 + helper._startBrowser = async () => { + starts++ + } + return { helper, starts: () => starts } + } + + it('should not start a browser before a suite when each test starts its own browser (#5784)', async () => { + for (const restart of [true, 'browser']) { + const { helper, starts } = countBrowserStarts(restart) + await helper._beforeSuite() + expect(starts(), `restart: ${restart}`).to.equal(0) + } + }) + + it('should start the browser before a suite when tests share it', async () => { + for (const restart of [false, 'context', 'session']) { + const { helper, starts } = countBrowserStarts(restart) + await helper._beforeSuite() + expect(starts(), `restart: ${restart}`).to.equal(1) + } + }) +})