forked from microsoft/playwright
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbrowsertype-launch-selenium.spec.ts
202 lines (170 loc) · 8.72 KB
/
browsertype-launch-selenium.spec.ts
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* 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 { playwrightTest as test, expect } from './config/browserTest';
import type { TestInfo } from '@playwright/test';
import path from 'path';
import fs from 'fs';
import { start } from '../packages/playwright-core/lib/outofprocess';
const chromeDriver = process.env.PWTEST_CHROMEDRIVER;
const brokenDriver = path.join(__dirname, 'assets', 'selenium-grid', 'broken-selenium-driver.js');
const standalone_3_141_59 = path.join(__dirname, 'assets', 'selenium-grid', 'selenium-server-standalone-3.141.59.jar');
const selenium_4_0_0_rc1 = path.join(__dirname, 'assets', 'selenium-grid', 'selenium-server-4.0.0-rc-1.jar');
function writeSeleniumConfig(testInfo: TestInfo, port: number) {
const template = path.join(__dirname, 'assets', 'selenium-grid', `selenium-config-standalone.json`);
const content = fs.readFileSync(template, 'utf8').replace(/4444/g, String(port));
const file = testInfo.outputPath(`selenium-config-standalone.json`);
fs.writeFileSync(file, content, 'utf8');
return file;
}
test.skip(({ mode }) => mode !== 'default', 'Using test hooks');
test.skip(!chromeDriver);
test.slow();
test('selenium grid 3.141.59 standalone chromium', async ({ browserName, childProcess, waitForPort, browserType }, testInfo) => {
test.skip(browserName !== 'chromium');
const port = testInfo.workerIndex + 15123;
const grid = childProcess({
command: ['java', `-Dwebdriver.chrome.driver=${chromeDriver}`, '-jar', standalone_3_141_59, '-config', writeSeleniumConfig(testInfo, port)],
cwd: __dirname,
});
await waitForPort(port);
const __testHookSeleniumRemoteURL = `http://localhost:${port}/wd/hub`;
const browser = await browserType.launch({ __testHookSeleniumRemoteURL } as any);
const page = await browser.newPage();
await page.setContent('<title>Hello world</title><div>Get Started</div>');
await page.click('text=Get Started');
await expect(page).toHaveTitle('Hello world');
await browser.close();
expect(grid.output).toContain('Starting ChromeDriver');
expect(grid.output).toContain('Started new session');
await grid.waitForOutput('Removing session');
});
test('selenium grid 3.141.59 hub + node chromium', async ({ browserName, childProcess, waitForPort, browserType }, testInfo) => {
test.skip(browserName !== 'chromium');
const port = testInfo.workerIndex + 15123;
const hub = childProcess({
command: ['java', '-jar', standalone_3_141_59, '-role', 'hub', '-port', String(port)],
cwd: __dirname,
});
await waitForPort(port);
const node = childProcess({
command: ['java', `-Dwebdriver.chrome.driver=${chromeDriver}`, '-jar', standalone_3_141_59, '-role', 'node', '-host', '127.0.0.1', '-hub', `http://localhost:${port}/grid/register`],
cwd: __dirname,
});
await Promise.all([
node.waitForOutput('The node is registered to the hub and ready to use'),
hub.waitForOutput('Registered a node'),
]);
const __testHookSeleniumRemoteURL = `http://localhost:${port}/wd/hub`;
const browser = await browserType.launch({ __testHookSeleniumRemoteURL } as any);
const page = await browser.newPage();
await page.setContent('<title>Hello world</title><div>Get Started</div>');
await page.click('text=Get Started');
await expect(page).toHaveTitle('Hello world');
await browser.close();
expect(hub.output).toContain('Got a request to create a new session');
expect(node.output).toContain('Starting ChromeDriver');
expect(node.output).toContain('Started new session');
await node.waitForOutput('Removing session');
});
test('selenium grid 4.0.0-rc-1 standalone chromium', async ({ browserName, childProcess, waitForPort, browserType }, testInfo) => {
test.skip(browserName !== 'chromium');
const port = testInfo.workerIndex + 15123;
const grid = childProcess({
command: ['java', `-Dwebdriver.chrome.driver=${chromeDriver}`, '-jar', selenium_4_0_0_rc1, 'standalone', '--config', writeSeleniumConfig(testInfo, port)],
cwd: __dirname,
});
await waitForPort(port);
const __testHookSeleniumRemoteURL = `http://localhost:${port}/wd/hub`;
const browser = await browserType.launch({ __testHookSeleniumRemoteURL } as any);
const page = await browser.newPage();
await page.setContent('<title>Hello world</title><div>Get Started</div>');
await page.click('text=Get Started');
await expect(page).toHaveTitle('Hello world');
await browser.close();
expect(grid.output).toContain('Starting ChromeDriver');
expect(grid.output).toContain('Session created');
await grid.waitForOutput('Deleted session');
});
test('selenium grid 4.0.0-rc-1 hub + node chromium', async ({ browserName, childProcess, waitForPort, browserType }, testInfo) => {
test.skip(browserName !== 'chromium');
const port = testInfo.workerIndex + 15123;
const hub = childProcess({
command: ['java', '-jar', selenium_4_0_0_rc1, 'hub', '--port', String(port)],
cwd: __dirname,
});
await waitForPort(port);
const __testHookSeleniumRemoteURL = `http://localhost:${port}/wd/hub`;
const node = childProcess({
command: ['java', `-Dwebdriver.chrome.driver=${chromeDriver}`, '-jar', selenium_4_0_0_rc1, 'node', '--grid-url', `http://localhost:${port}`, '--port', String(port + 1)],
cwd: __dirname,
});
await Promise.all([
node.waitForOutput('Node has been added'),
hub.waitForOutput('from DOWN to UP'),
]);
const browser = await browserType.launch({ __testHookSeleniumRemoteURL } as any);
const page = await browser.newPage();
await page.setContent('<title>Hello world</title><div>Get Started</div>');
await page.click('text=Get Started');
await expect(page).toHaveTitle('Hello world');
await browser.close();
expect(hub.output).toContain('Session request received by the distributor');
expect(node.output).toContain('Starting ChromeDriver');
await hub.waitForOutput('Deleted session');
});
test('selenium grid 4.0.0-rc-1 standalone chromium broken driver', async ({ browserName, childProcess, waitForPort, browserType }, testInfo) => {
test.skip(browserName !== 'chromium');
const port = testInfo.workerIndex + 15123;
const grid = childProcess({
command: ['java', `-Dwebdriver.chrome.driver=${brokenDriver}`, '-jar', selenium_4_0_0_rc1, 'standalone', '--config', writeSeleniumConfig(testInfo, port)],
cwd: __dirname,
});
await waitForPort(port);
const __testHookSeleniumRemoteURL = `http://localhost:${port}/wd/hub`;
const error = await browserType.launch({ __testHookSeleniumRemoteURL } as any).catch(e => e);
expect(error.message).toContain(`Error connecting to Selenium at http://localhost:${port}/wd/hub/session: Could not start a new session`);
expect(grid.output).not.toContain('Starting ChromeDriver');
});
test('selenium grid 3.141.59 standalone non-chromium', async ({ browserName, browserType }, testInfo) => {
test.skip(browserName === 'chromium');
const __testHookSeleniumRemoteURL = `http://localhost:4444/wd/hub`;
const error = await browserType.launch({ __testHookSeleniumRemoteURL } as any).catch(e => e);
expect(error.message).toContain('Connecting to SELENIUM_REMOTE_URL is only supported by Chromium');
});
test('selenium grid 3.141.59 standalone chromium through run-driver', async ({ browserName, childProcess, waitForPort }, testInfo) => {
test.skip(browserName !== 'chromium');
const port = testInfo.workerIndex + 15123;
const grid = childProcess({
command: ['java', `-Dwebdriver.chrome.driver=${chromeDriver}`, '-jar', standalone_3_141_59, '-config', writeSeleniumConfig(testInfo, port)],
cwd: __dirname,
});
await waitForPort(port);
const { playwright: pw, stop } = await start({
SELENIUM_REMOTE_URL: `http://localhost:${port}/wd/hub`,
});
const browser = await pw.chromium.launch();
const page = await browser.newPage();
await page.setContent('<title>Hello world</title><div>Get Started</div>');
await page.click('text=Get Started');
await expect(page).toHaveTitle('Hello world');
// Note: it is important to stop the driver without explicitly closing the browser.
// It should terminate selenium session in this case.
await stop();
expect(grid.output).toContain('Starting ChromeDriver');
expect(grid.output).toContain('Started new session');
// It is important that selenium session is terminated.
await grid.waitForOutput('Removing session');
});