forked from puppeteer/puppeteer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chromiumonly.spec.ts
173 lines (158 loc) · 5.92 KB
/
chromiumonly.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
/**
* Copyright 2019 Google Inc. All rights reserved.
*
* 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 {IncomingMessage} from 'http';
import expect from 'expect';
import {
getTestState,
setupTestBrowserHooks,
setupTestPageAndContextHooks,
} from './mocha-utils.js';
describe('Chromium-Specific Launcher tests', function () {
describe('Puppeteer.launch |browserURL| option', function () {
it('should be able to connect using browserUrl, with and without trailing slash', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const originalBrowser = await puppeteer.launch(
Object.assign({}, defaultBrowserOptions, {
args: ['--remote-debugging-port=21222'],
})
);
const browserURL = 'http://127.0.0.1:21222';
const browser1 = await puppeteer.connect({browserURL});
const page1 = await browser1.newPage();
expect(
await page1.evaluate(() => {
return 7 * 8;
})
).toBe(56);
browser1.disconnect();
const browser2 = await puppeteer.connect({
browserURL: browserURL + '/',
});
const page2 = await browser2.newPage();
expect(
await page2.evaluate(() => {
return 8 * 7;
})
).toBe(56);
browser2.disconnect();
await originalBrowser.close();
});
it('should throw when using both browserWSEndpoint and browserURL', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const originalBrowser = await puppeteer.launch(
Object.assign({}, defaultBrowserOptions, {
args: ['--remote-debugging-port=21222'],
})
);
const browserURL = 'http://127.0.0.1:21222';
let error!: Error;
await puppeteer
.connect({
browserURL,
browserWSEndpoint: originalBrowser.wsEndpoint(),
})
.catch(error_ => {
return (error = error_);
});
expect(error.message).toContain(
'Exactly one of browserWSEndpoint, browserURL or transport'
);
await originalBrowser.close();
});
it('should throw when trying to connect to non-existing browser', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const originalBrowser = await puppeteer.launch(
Object.assign({}, defaultBrowserOptions, {
args: ['--remote-debugging-port=21222'],
})
);
const browserURL = 'http://127.0.0.1:32333';
let error!: Error;
await puppeteer.connect({browserURL}).catch(error_ => {
return (error = error_);
});
expect(error.message).toContain(
'Failed to fetch browser webSocket URL from'
);
await originalBrowser.close();
});
});
describe('Puppeteer.launch |pipe| option', function () {
it('should support the pipe option', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const options = Object.assign({pipe: true}, defaultBrowserOptions);
const browser = await puppeteer.launch(options);
expect((await browser.pages()).length).toBe(1);
expect(browser.wsEndpoint()).toBe('');
const page = await browser.newPage();
expect(await page.evaluate('11 * 11')).toBe(121);
await page.close();
await browser.close();
});
it('should support the pipe argument', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const options = Object.assign({}, defaultBrowserOptions);
options.args = ['--remote-debugging-pipe'].concat(options.args || []);
const browser = await puppeteer.launch(options);
expect(browser.wsEndpoint()).toBe('');
const page = await browser.newPage();
expect(await page.evaluate('11 * 11')).toBe(121);
await page.close();
await browser.close();
});
it('should fire "disconnected" when closing with pipe', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const options = Object.assign({pipe: true}, defaultBrowserOptions);
const browser = await puppeteer.launch(options);
const disconnectedEventPromise = new Promise(resolve => {
return browser.once('disconnected', resolve);
});
// Emulate user exiting browser.
browser.process()!.kill();
await disconnectedEventPromise;
});
});
});
describe('Chromium-Specific Page Tests', function () {
setupTestBrowserHooks();
setupTestPageAndContextHooks();
it('Page.setRequestInterception should work with intervention headers', async () => {
const {server, page} = getTestState();
server.setRoute('/intervention', (_req, res) => {
return res.end(`
<script>
document.write('<script src="${server.CROSS_PROCESS_PREFIX}/intervention.js">' + '</scr' + 'ipt>');
</script>
`);
});
server.setRedirect('/intervention.js', '/redirect.js');
let serverRequest: IncomingMessage | undefined;
server.setRoute('/redirect.js', (req, res) => {
serverRequest = req;
res.end('console.log(1);');
});
await page.setRequestInterception(true);
page.on('request', request => {
return request.continue();
});
await page.goto(server.PREFIX + '/intervention');
// Check for feature URL substring rather than https://www.chromestatus.com to
// make it work with Edgium.
expect(serverRequest!.headers['intervention']).toContain(
'feature/5718547946799104'
);
});
});