2019-02-02 01:55:12 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2022-06-22 13:25:44 +00:00
|
|
|
import {IncomingMessage} from 'http';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
|
|
|
import expect from 'expect';
|
|
|
|
|
2023-06-21 19:41:09 +00:00
|
|
|
import {getTestState, launch} from './mocha-utils.js';
|
2023-04-25 13:02:25 +00:00
|
|
|
import {waitEvent} from './utils.js';
|
2019-02-25 20:51:06 +00:00
|
|
|
|
2023-06-16 13:27:31 +00:00
|
|
|
// TODO: rename this test suite to launch/connect test suite as it actually
|
|
|
|
// works across browsers.
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Chromium-Specific Launcher tests', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Puppeteer.launch |browserURL| option', function () {
|
|
|
|
it('should be able to connect using browserUrl, with and without trailing slash', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {close, puppeteer} = await launch({
|
|
|
|
args: ['--remote-debugging-port=21222'],
|
|
|
|
});
|
2023-06-12 08:44:18 +00:00
|
|
|
try {
|
|
|
|
const browserURL = 'http://127.0.0.1:21222';
|
2019-02-25 20:51:06 +00:00
|
|
|
|
2023-06-12 08:44:18 +00:00
|
|
|
const browser1 = await puppeteer.connect({browserURL});
|
|
|
|
const page1 = await browser1.newPage();
|
|
|
|
expect(
|
|
|
|
await page1.evaluate(() => {
|
|
|
|
return 7 * 8;
|
|
|
|
})
|
|
|
|
).toBe(56);
|
|
|
|
browser1.disconnect();
|
2019-02-25 20:51:06 +00:00
|
|
|
|
2023-06-12 08:44:18 +00:00
|
|
|
const browser2 = await puppeteer.connect({
|
|
|
|
browserURL: browserURL + '/',
|
|
|
|
});
|
|
|
|
const page2 = await browser2.newPage();
|
|
|
|
expect(
|
|
|
|
await page2.evaluate(() => {
|
|
|
|
return 8 * 7;
|
|
|
|
})
|
|
|
|
).toBe(56);
|
|
|
|
browser2.disconnect();
|
|
|
|
} finally {
|
|
|
|
await close();
|
|
|
|
}
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw when using both browserWSEndpoint and browserURL', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {browser, close, puppeteer} = await launch({
|
|
|
|
args: ['--remote-debugging-port=21222'],
|
|
|
|
});
|
2023-06-12 08:44:18 +00:00
|
|
|
try {
|
|
|
|
const browserURL = 'http://127.0.0.1:21222';
|
2019-02-25 20:51:06 +00:00
|
|
|
|
2023-06-12 08:44:18 +00:00
|
|
|
let error!: Error;
|
|
|
|
await puppeteer
|
|
|
|
.connect({
|
|
|
|
browserURL,
|
|
|
|
browserWSEndpoint: browser.wsEndpoint(),
|
|
|
|
})
|
|
|
|
.catch(error_ => {
|
|
|
|
return (error = error_);
|
|
|
|
});
|
|
|
|
expect(error.message).toContain(
|
|
|
|
'Exactly one of browserWSEndpoint, browserURL or transport'
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
await close();
|
|
|
|
}
|
2019-02-25 20:51:06 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw when trying to connect to non-existing browser', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {close, puppeteer} = await launch({
|
|
|
|
args: ['--remote-debugging-port=21222'],
|
|
|
|
});
|
2023-06-12 08:44:18 +00:00
|
|
|
try {
|
|
|
|
const browserURL = 'http://127.0.0.1:32333';
|
2019-02-25 20:51:06 +00:00
|
|
|
|
2023-06-12 08:44:18 +00:00
|
|
|
let error!: Error;
|
|
|
|
await puppeteer.connect({browserURL}).catch(error_ => {
|
|
|
|
return (error = error_);
|
|
|
|
});
|
|
|
|
expect(error.message).toContain(
|
|
|
|
'Failed to fetch browser webSocket URL from'
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
await close();
|
|
|
|
}
|
2019-02-02 01:55:12 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2019-07-23 04:30:49 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Puppeteer.launch |pipe| option', function () {
|
|
|
|
it('should support the pipe option', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {browser, close} = await launch({pipe: true}, {createPage: false});
|
2023-06-12 08:44:18 +00:00
|
|
|
try {
|
|
|
|
expect(await browser.pages()).toHaveLength(1);
|
|
|
|
expect(browser.wsEndpoint()).toBe('');
|
|
|
|
const page = await browser.newPage();
|
|
|
|
expect(await page.evaluate('11 * 11')).toBe(121);
|
|
|
|
await page.close();
|
|
|
|
} finally {
|
|
|
|
await close();
|
|
|
|
}
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should support the pipe argument', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {defaultBrowserOptions} = await getTestState({skipLaunch: true});
|
2020-04-09 05:56:25 +00:00
|
|
|
const options = Object.assign({}, defaultBrowserOptions);
|
|
|
|
options.args = ['--remote-debugging-pipe'].concat(options.args || []);
|
2023-06-12 08:44:18 +00:00
|
|
|
const {browser, close} = await launch(options);
|
|
|
|
try {
|
|
|
|
expect(browser.wsEndpoint()).toBe('');
|
|
|
|
const page = await browser.newPage();
|
|
|
|
expect(await page.evaluate('11 * 11')).toBe(121);
|
|
|
|
await page.close();
|
|
|
|
} finally {
|
|
|
|
await close();
|
|
|
|
}
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fire "disconnected" when closing with pipe', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {browser, close} = await launch({pipe: true});
|
2023-06-12 08:44:18 +00:00
|
|
|
try {
|
|
|
|
const disconnectedEventPromise = waitEvent(browser, 'disconnected');
|
|
|
|
// Emulate user exiting browser.
|
|
|
|
browser.process()!.kill();
|
|
|
|
await disconnectedEventPromise;
|
|
|
|
} finally {
|
|
|
|
await close();
|
|
|
|
}
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2019-02-25 20:51:06 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Chromium-Specific Page Tests', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('Page.setRequestInterception should work with intervention headers', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {server, page} = await getTestState();
|
2019-02-25 20:51:06 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/intervention', (_req, res) => {
|
|
|
|
return res.end(`
|
2019-02-25 20:51:06 +00:00
|
|
|
<script>
|
|
|
|
document.write('<script src="${server.CROSS_PROCESS_PREFIX}/intervention.js">' + '</scr' + 'ipt>');
|
|
|
|
</script>
|
2022-06-15 10:09:22 +00:00
|
|
|
`);
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
server.setRedirect('/intervention.js', '/redirect.js');
|
2022-06-15 10:09:22 +00:00
|
|
|
let serverRequest: IncomingMessage | undefined;
|
2020-04-09 05:56:25 +00:00
|
|
|
server.setRoute('/redirect.js', (req, res) => {
|
|
|
|
serverRequest = req;
|
|
|
|
res.end('console.log(1);');
|
2019-02-02 01:55:12 +00:00
|
|
|
});
|
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.setRequestInterception(true);
|
2022-06-22 13:25:44 +00:00
|
|
|
page.on('request', request => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return request.continue();
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.goto(server.PREFIX + '/intervention');
|
|
|
|
// Check for feature URL substring rather than https://www.chromestatus.com to
|
|
|
|
// make it work with Edgium.
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(serverRequest!.headers['intervention']).toContain(
|
2020-05-07 10:54:55 +00:00
|
|
|
'feature/5718547946799104'
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
|
|
|
});
|