Skip to content

Commit ce33ec2

Browse files
committed
cherry-pick(#22191): chore: allow reusing browser between the tests
1 parent ed7a560 commit ce33ec2

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

packages/playwright-core/src/remote/playwrightConnection.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,5 @@ const defaultLaunchOptions: LaunchOptions = {
282282

283283
const optionsThatAllowBrowserReuse: (keyof LaunchOptions)[] = [
284284
'headless',
285+
'tracesDir',
285286
];
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { test as baseTest, expect } from './playwright-test-fixtures';
18+
import { RunServer } from '../config/remoteServer';
19+
import type { PlaywrightServer } from '../config/remoteServer';
20+
21+
const test = baseTest.extend<{ runServer: () => Promise<PlaywrightServer> }>({
22+
runServer: async ({ childProcess }, use) => {
23+
let server: PlaywrightServer | undefined;
24+
await use(async () => {
25+
const runServer = new RunServer();
26+
await runServer._start(childProcess);
27+
server = runServer;
28+
return server;
29+
});
30+
if (server) {
31+
await server.close();
32+
// Give any connected browsers a chance to disconnect to avoid
33+
// poisoning next test with quasy-alive browsers.
34+
await new Promise(f => setTimeout(f, 1000));
35+
}
36+
},
37+
});
38+
39+
test('should reuse browser', async ({ runInlineTest, runServer }) => {
40+
const server = await runServer();
41+
const result = await runInlineTest({
42+
'src/a.test.ts': `
43+
import { test, expect } from '@playwright/test';
44+
test('a', async ({ browser }) => {
45+
console.log('%%' + process.env.TEST_WORKER_INDEX + ':' + browser._guid);
46+
});
47+
`,
48+
'src/b.test.ts': `
49+
import { test, expect } from '@playwright/test';
50+
test('b', async ({ browser }) => {
51+
console.log('%%' + process.env.TEST_WORKER_INDEX + ':' + browser._guid);
52+
});
53+
`,
54+
}, { workers: 2 }, { PW_TEST_REUSE_CONTEXT: '1', PW_TEST_CONNECT_WS_ENDPOINT: server.wsEndpoint() });
55+
56+
expect(result.exitCode).toBe(0);
57+
expect(result.passed).toBe(2);
58+
expect(result.outputLines).toHaveLength(2);
59+
const [workerIndex1, guid1] = result.outputLines[0].split(':');
60+
const [workerIndex2, guid2] = result.outputLines[1].split(':');
61+
expect(guid2).toBe(guid1);
62+
expect(workerIndex2).not.toBe(workerIndex1);
63+
});

0 commit comments

Comments
 (0)