1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test as baseTest, expect } from './playwright-test-fixtures';
import { RunServer } from '../config/remoteServer';
import type { PlaywrightServer } from '../config/remoteServer';
const test = baseTest.extend<{ runServer: () => Promise<PlaywrightServer> }>({
runServer: async ({ childProcess }, use) => {
let server: PlaywrightServer | undefined;
await use(async () => {
const runServer = new RunServer();
await runServer.start(childProcess, 'extension');
server = runServer;
return server;
});
if (server) {
await server.close();
// Give any connected browsers a chance to disconnect to avoid
// poisoning next test with quasy-alive browsers.
await new Promise(f => setTimeout(f, 1000));
}
},
});
test('should reuse browser', async ({ runInlineTest, runServer }) => {
const server = await runServer();
const result = await runInlineTest({
'src/a.test.ts': `
import { test, expect } from '@playwright/test';
test('a', async ({ browser }) => {
console.log('%%' + process.env.TEST_WORKER_INDEX + ':' + browser._guid);
});
`,
'src/b.test.ts': `
import { test, expect } from '@playwright/test';
test('b', async ({ browser }) => {
console.log('%%' + process.env.TEST_WORKER_INDEX + ':' + browser._guid);
});
`,
}, { workers: 2 }, { PW_TEST_REUSE_CONTEXT: '1', PW_TEST_CONNECT_WS_ENDPOINT: server.wsEndpoint() });
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(2);
expect(result.outputLines).toHaveLength(2);
const [workerIndex1, guid1] = result.outputLines[0].split(':');
const [workerIndex2, guid2] = result.outputLines[1].split(':');
expect(guid2).toBe(guid1);
expect(workerIndex2).not.toBe(workerIndex1);
});
test('should reuse browser with special characters in the launch options', async ({ runInlineTest, runServer }) => {
const server = await runServer();
const result = await runInlineTest({
'playwright.config.js': `
module.exports = {
use: {
launchOptions: {
env: {
RANDOM_TEST_SPECIAL: 'Привет',
}
}
}
}
`,
'src/a.test.ts': `
import { test, expect } from '@playwright/test';
test('a', async ({ browser }) => {
console.log('%%' + process.env.TEST_WORKER_INDEX + ':' + browser._guid);
});
`,
'src/b.test.ts': `
import { test, expect } from '@playwright/test';
test('b', async ({ browser }) => {
console.log('%%' + process.env.TEST_WORKER_INDEX + ':' + browser._guid);
});
`,
}, { workers: 2 }, { PW_TEST_REUSE_CONTEXT: '1', PW_TEST_CONNECT_WS_ENDPOINT: server.wsEndpoint() });
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(2);
expect(result.outputLines).toHaveLength(2);
const [workerIndex1, guid1] = result.outputLines[0].split(':');
const [workerIndex2, guid2] = result.outputLines[1].split(':');
expect(guid2).toBe(guid1);
expect(workerIndex2).not.toBe(workerIndex1);
});
test('should produce correct test steps', async ({ runInlineTest, runServer }) => {
const server = await runServer();
const result = await runInlineTest({
'reporter.ts': `
class Reporter {
onStepBegin(test, result, step) {
console.log('%% onStepBegin [' + step.category + '] ' + step.title);
}
onStepEnd(test, result, step) {
console.log('%% onStepEnd [' + step.category + '] ' + step.title);
}
}
module.exports = Reporter;
`,
'src/a.test.ts': `
import { test, expect } from '@playwright/test';
test('a', async ({ page }) => {
await page.goto('about:blank');
await page.evaluate(() => console.log('hello'));
});
`,
}, { reporter: './reporter.ts,list' }, { PW_TEST_REUSE_CONTEXT: '1', PW_TEST_CONNECT_WS_ENDPOINT: server.wsEndpoint() });
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
expect(result.outputLines).toEqual([
'onStepBegin [hook] Before Hooks',
'onStepBegin [fixture] Fixture "browser"',
'onStepBegin [pw:api] connect',
'onStepEnd [pw:api] connect',
'onStepEnd [fixture] Fixture "browser"',
'onStepBegin [fixture] Fixture "context"',
'onStepEnd [fixture] Fixture "context"',
'onStepBegin [fixture] Fixture "page"',
'onStepBegin [pw:api] Create page',
'onStepEnd [pw:api] Create page',
'onStepEnd [fixture] Fixture "page"',
'onStepEnd [hook] Before Hooks',
'onStepBegin [pw:api] Navigate to "about:blank"',
'onStepEnd [pw:api] Navigate to "about:blank"',
'onStepBegin [pw:api] Evaluate',
'onStepEnd [pw:api] Evaluate',
'onStepBegin [hook] After Hooks',
'onStepBegin [fixture] Fixture "page"',
'onStepEnd [fixture] Fixture "page"',
'onStepBegin [fixture] Fixture "context"',
'onStepEnd [fixture] Fixture "context"',
'onStepEnd [hook] After Hooks'
]);
});