Skip to content

Commit c238bfe

Browse files
authored
fix(playwright): start no browser in _beforeSuite with restart: browser (#5784) (#5785)
With the browser restart strategy, _before() starts a browser for each test. The browser that _beforeSuite() started was replaced by the one of the first test and never closed, so each test file left one browser running.
1 parent 0a4912c commit c238bfe

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎lib/helper/Playwright.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,10 @@ class Playwright extends Helper {
554554
return
555555
}
556556

557-
// Start browser if not manually started and not already running
558-
// Browser should start in singleton mode (restart: false) or when restart strategy is enabled
559-
if (!this.options.manualStart && !this.isRunning) {
557+
// Start browser if not manually started and not already running.
558+
// With the browser restart strategy, _before() starts a browser for each test,
559+
// and a browser started here would never be closed (#5784)
560+
if (!this.options.manualStart && !this.isRunning && !restartsBrowser()) {
560561
this.debugSection('Session', 'Starting singleton browser session')
561562
return this._startBrowser()
562563
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { expect } from 'chai'
2+
import Playwright from '../../../lib/helper/Playwright.js'
3+
4+
describe('Playwright restart strategy', () => {
5+
function countBrowserStarts(restart) {
6+
const helper = new Playwright({ browser: 'chromium', url: 'http://localhost', restart })
7+
let starts = 0
8+
helper._startBrowser = async () => {
9+
starts++
10+
}
11+
return { helper, starts: () => starts }
12+
}
13+
14+
it('should not start a browser before a suite when each test starts its own browser (#5784)', async () => {
15+
for (const restart of [true, 'browser']) {
16+
const { helper, starts } = countBrowserStarts(restart)
17+
await helper._beforeSuite()
18+
expect(starts(), `restart: ${restart}`).to.equal(0)
19+
}
20+
})
21+
22+
it('should start the browser before a suite when tests share it', async () => {
23+
for (const restart of [false, 'context', 'session']) {
24+
const { helper, starts } = countBrowserStarts(restart)
25+
await helper._beforeSuite()
26+
expect(starts(), `restart: ${restart}`).to.equal(1)
27+
}
28+
})
29+
})

0 commit comments

Comments
 (0)