2019-01-24 06:04:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 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.
|
|
|
|
*/
|
2020-06-23 05:18:46 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import os from 'os';
|
|
|
|
import path from 'path';
|
|
|
|
import sinon from 'sinon';
|
2020-06-25 10:54:00 +00:00
|
|
|
import { promisify } from 'util';
|
2021-05-03 11:48:31 +00:00
|
|
|
import Protocol from 'devtools-protocol';
|
2020-06-23 05:18:46 +00:00
|
|
|
import {
|
|
|
|
getTestState,
|
2021-05-06 20:30:04 +00:00
|
|
|
itChromeOnly,
|
2020-06-23 05:18:46 +00:00
|
|
|
itFailsFirefox,
|
2021-11-10 12:31:15 +00:00
|
|
|
itFirefoxOnly,
|
2020-06-23 05:18:46 +00:00
|
|
|
itOnlyRegularInstall,
|
2020-07-13 09:22:26 +00:00
|
|
|
} from './mocha-utils'; // eslint-disable-line import/extensions
|
|
|
|
import utils from './utils.js';
|
2020-06-23 05:18:46 +00:00
|
|
|
import expect from 'expect';
|
|
|
|
import rimraf from 'rimraf';
|
2020-07-14 15:57:29 +00:00
|
|
|
import { Page } from '../lib/cjs/puppeteer/common/Page.js';
|
2020-06-23 05:18:46 +00:00
|
|
|
|
2020-06-25 10:54:00 +00:00
|
|
|
const mkdtempAsync = promisify(fs.mkdtemp);
|
|
|
|
const readFileAsync = promisify(fs.readFile);
|
2021-11-10 12:31:15 +00:00
|
|
|
const rmAsync = promisify(rimraf);
|
2020-06-25 10:54:00 +00:00
|
|
|
const statAsync = promisify(fs.stat);
|
2021-11-10 12:31:15 +00:00
|
|
|
const writeFileAsync = promisify(fs.writeFile);
|
|
|
|
|
2019-01-24 06:04:42 +00:00
|
|
|
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
|
2020-08-10 08:23:17 +00:00
|
|
|
const FIREFOX_TIMEOUT = 30 * 1000;
|
2019-01-24 06:04:42 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Launcher specs', function () {
|
2020-08-10 08:23:17 +00:00
|
|
|
if (getTestState().isFirefox) this.timeout(FIREFOX_TIMEOUT);
|
2020-06-12 13:55:51 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Puppeteer', function () {
|
|
|
|
describe('BrowserFetcher', function () {
|
|
|
|
it('should download and extract chrome linux binary', async () => {
|
|
|
|
const { server, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-24 06:04:42 +00:00
|
|
|
const downloadsFolder = await mkdtempAsync(TMP_FOLDER);
|
|
|
|
const browserFetcher = puppeteer.createBrowserFetcher({
|
|
|
|
platform: 'linux',
|
|
|
|
path: downloadsFolder,
|
2020-05-07 10:54:55 +00:00
|
|
|
host: server.PREFIX,
|
2019-01-24 06:04:42 +00:00
|
|
|
});
|
2020-03-10 20:59:03 +00:00
|
|
|
const expectedRevision = '123456';
|
|
|
|
let revisionInfo = browserFetcher.revisionInfo(expectedRevision);
|
2020-05-07 10:54:55 +00:00
|
|
|
server.setRoute(
|
|
|
|
revisionInfo.url.substring(server.PREFIX.length),
|
|
|
|
(req, res) => {
|
|
|
|
server.serveFile(req, res, '/chromium-linux.zip');
|
|
|
|
}
|
|
|
|
);
|
2019-01-24 06:04:42 +00:00
|
|
|
|
|
|
|
expect(revisionInfo.local).toBe(false);
|
|
|
|
expect(browserFetcher.platform()).toBe('linux');
|
2020-03-10 20:59:03 +00:00
|
|
|
expect(browserFetcher.product()).toBe('chrome');
|
|
|
|
expect(!!browserFetcher.host()).toBe(true);
|
2019-01-24 06:04:42 +00:00
|
|
|
expect(await browserFetcher.canDownload('100000')).toBe(false);
|
2020-03-10 20:59:03 +00:00
|
|
|
expect(await browserFetcher.canDownload(expectedRevision)).toBe(true);
|
2019-01-24 06:04:42 +00:00
|
|
|
|
2020-03-10 20:59:03 +00:00
|
|
|
revisionInfo = await browserFetcher.download(expectedRevision);
|
2019-01-24 06:04:42 +00:00
|
|
|
expect(revisionInfo.local).toBe(true);
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await readFileAsync(revisionInfo.executablePath, 'utf8')).toBe(
|
|
|
|
'LINUX BINARY\n'
|
|
|
|
);
|
2020-03-10 20:59:03 +00:00
|
|
|
const expectedPermissions = os.platform() === 'win32' ? 0o666 : 0o755;
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
|
|
|
(await statAsync(revisionInfo.executablePath)).mode & 0o777
|
|
|
|
).toBe(expectedPermissions);
|
|
|
|
expect(await browserFetcher.localRevisions()).toEqual([
|
|
|
|
expectedRevision,
|
|
|
|
]);
|
2020-03-10 20:59:03 +00:00
|
|
|
await browserFetcher.remove(expectedRevision);
|
|
|
|
expect(await browserFetcher.localRevisions()).toEqual([]);
|
|
|
|
await rmAsync(downloadsFolder);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should download and extract firefox linux binary', async () => {
|
|
|
|
const { server, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-03-10 20:59:03 +00:00
|
|
|
const downloadsFolder = await mkdtempAsync(TMP_FOLDER);
|
|
|
|
const browserFetcher = puppeteer.createBrowserFetcher({
|
|
|
|
platform: 'linux',
|
|
|
|
path: downloadsFolder,
|
|
|
|
host: server.PREFIX,
|
|
|
|
product: 'firefox',
|
|
|
|
});
|
2020-05-13 13:48:39 +00:00
|
|
|
const expectedVersion = '75.0a1';
|
2020-03-10 20:59:03 +00:00
|
|
|
let revisionInfo = browserFetcher.revisionInfo(expectedVersion);
|
2020-05-07 10:54:55 +00:00
|
|
|
server.setRoute(
|
|
|
|
revisionInfo.url.substring(server.PREFIX.length),
|
|
|
|
(req, res) => {
|
|
|
|
server.serveFile(
|
|
|
|
req,
|
|
|
|
res,
|
2020-05-13 13:48:39 +00:00
|
|
|
`/firefox-${expectedVersion}.en-US.linux-x86_64.tar.bz2`
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2020-03-10 20:59:03 +00:00
|
|
|
|
|
|
|
expect(revisionInfo.local).toBe(false);
|
|
|
|
expect(browserFetcher.platform()).toBe('linux');
|
|
|
|
expect(browserFetcher.product()).toBe('firefox');
|
|
|
|
expect(await browserFetcher.canDownload('100000')).toBe(false);
|
|
|
|
expect(await browserFetcher.canDownload(expectedVersion)).toBe(true);
|
|
|
|
|
|
|
|
revisionInfo = await browserFetcher.download(expectedVersion);
|
|
|
|
expect(revisionInfo.local).toBe(true);
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await readFileAsync(revisionInfo.executablePath, 'utf8')).toBe(
|
|
|
|
'FIREFOX LINUX BINARY\n'
|
|
|
|
);
|
2020-03-10 20:59:03 +00:00
|
|
|
const expectedPermissions = os.platform() === 'win32' ? 0o666 : 0o755;
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
|
|
|
(await statAsync(revisionInfo.executablePath)).mode & 0o777
|
|
|
|
).toBe(expectedPermissions);
|
|
|
|
expect(await browserFetcher.localRevisions()).toEqual([
|
|
|
|
expectedVersion,
|
|
|
|
]);
|
2020-03-10 20:59:03 +00:00
|
|
|
await browserFetcher.remove(expectedVersion);
|
2019-01-24 06:04:42 +00:00
|
|
|
expect(await browserFetcher.localRevisions()).toEqual([]);
|
|
|
|
await rmAsync(downloadsFolder);
|
|
|
|
});
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Browser.disconnect', function () {
|
|
|
|
it('should reject navigation when browser closes', async () => {
|
|
|
|
const { server, puppeteer, defaultBrowserOptions } = getTestState();
|
2019-01-24 06:04:42 +00:00
|
|
|
server.setRoute('/one-style.css', () => {});
|
|
|
|
const browser = await puppeteer.launch(defaultBrowserOptions);
|
2020-05-07 10:54:55 +00:00
|
|
|
const remote = await puppeteer.connect({
|
|
|
|
browserWSEndpoint: browser.wsEndpoint(),
|
|
|
|
});
|
2019-01-24 06:04:42 +00:00
|
|
|
const page = await remote.newPage();
|
2020-05-07 10:54:55 +00:00
|
|
|
const navigationPromise = page
|
|
|
|
.goto(server.PREFIX + '/one-style.html', { timeout: 60000 })
|
|
|
|
.catch((error_) => error_);
|
2019-01-24 06:04:42 +00:00
|
|
|
await server.waitForRequest('/one-style.css');
|
2019-05-21 14:26:04 +00:00
|
|
|
remote.disconnect();
|
2019-01-24 06:04:42 +00:00
|
|
|
const error = await navigationPromise;
|
2022-06-09 17:00:50 +00:00
|
|
|
expect(
|
|
|
|
[
|
|
|
|
'Navigation failed because browser has disconnected!',
|
|
|
|
'Protocol error (Page.navigate): Target closed.',
|
|
|
|
].includes(error.message)
|
|
|
|
).toBeTruthy();
|
2019-01-24 06:04:42 +00:00
|
|
|
await browser.close();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should reject waitForSelector when browser closes', async () => {
|
|
|
|
const { server, puppeteer, defaultBrowserOptions } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-24 06:04:42 +00:00
|
|
|
server.setRoute('/empty.html', () => {});
|
|
|
|
const browser = await puppeteer.launch(defaultBrowserOptions);
|
2020-05-07 10:54:55 +00:00
|
|
|
const remote = await puppeteer.connect({
|
|
|
|
browserWSEndpoint: browser.wsEndpoint(),
|
|
|
|
});
|
2019-01-24 06:04:42 +00:00
|
|
|
const page = await remote.newPage();
|
2020-05-07 10:54:55 +00:00
|
|
|
const watchdog = page
|
|
|
|
.waitForSelector('div', { timeout: 60000 })
|
|
|
|
.catch((error_) => error_);
|
2019-05-21 14:26:04 +00:00
|
|
|
remote.disconnect();
|
2019-01-24 06:04:42 +00:00
|
|
|
const error = await watchdog;
|
2019-02-20 06:08:09 +00:00
|
|
|
expect(error.message).toContain('Protocol error');
|
2019-01-24 06:04:42 +00:00
|
|
|
await browser.close();
|
|
|
|
});
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Browser.close', function () {
|
|
|
|
it('should terminate network waiters', async () => {
|
|
|
|
const { server, puppeteer, defaultBrowserOptions } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-08-21 17:26:48 +00:00
|
|
|
const browser = await puppeteer.launch(defaultBrowserOptions);
|
2020-05-07 10:54:55 +00:00
|
|
|
const remote = await puppeteer.connect({
|
|
|
|
browserWSEndpoint: browser.wsEndpoint(),
|
|
|
|
});
|
2019-08-21 17:26:48 +00:00
|
|
|
const newPage = await remote.newPage();
|
|
|
|
const results = await Promise.all([
|
2020-05-07 10:54:55 +00:00
|
|
|
newPage.waitForRequest(server.EMPTY_PAGE).catch((error) => error),
|
|
|
|
newPage.waitForResponse(server.EMPTY_PAGE).catch((error) => error),
|
|
|
|
browser.close(),
|
2019-08-21 17:26:48 +00:00
|
|
|
]);
|
|
|
|
for (let i = 0; i < 2; i++) {
|
|
|
|
const message = results[i].message;
|
|
|
|
expect(message).toContain('Target closed');
|
|
|
|
expect(message).not.toContain('Timeout');
|
|
|
|
}
|
2020-04-09 05:56:25 +00:00
|
|
|
await browser.close();
|
2019-08-21 17:26:48 +00:00
|
|
|
});
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Puppeteer.launch', function () {
|
|
|
|
it('should reject all promises when browser is closed', async () => {
|
|
|
|
const { defaultBrowserOptions, puppeteer } = getTestState();
|
2019-01-24 06:04:42 +00:00
|
|
|
const browser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
const neverResolves = page
|
2020-06-23 05:18:46 +00:00
|
|
|
.evaluate(() => new Promise(() => {}))
|
2020-05-07 10:54:55 +00:00
|
|
|
.catch((error_) => (error = error_));
|
2019-01-24 06:04:42 +00:00
|
|
|
await browser.close();
|
|
|
|
await neverResolves;
|
|
|
|
expect(error.message).toContain('Protocol error');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should reject if executable path is invalid', async () => {
|
|
|
|
const { defaultBrowserOptions, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-24 06:04:42 +00:00
|
|
|
let waitError = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
const options = Object.assign({}, defaultBrowserOptions, {
|
|
|
|
executablePath: 'random-invalid-path',
|
|
|
|
});
|
|
|
|
await puppeteer.launch(options).catch((error) => (waitError = error));
|
2019-02-20 06:08:09 +00:00
|
|
|
expect(waitError.message).toContain('Failed to launch');
|
2019-01-24 06:04:42 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('userDataDir option', async () => {
|
|
|
|
const { defaultBrowserOptions, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-24 06:04:42 +00:00
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
2020-05-07 10:54:55 +00:00
|
|
|
const options = Object.assign({ userDataDir }, defaultBrowserOptions);
|
2019-01-24 06:04:42 +00:00
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
// Open a page to make sure its functional.
|
|
|
|
await browser.newPage();
|
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
|
|
|
await browser.close();
|
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
2019-11-26 12:12:25 +00:00
|
|
|
// This might throw. See https://github.com/puppeteer/puppeteer/issues/2778
|
2020-06-23 05:18:46 +00:00
|
|
|
await rmAsync(userDataDir).catch(() => {});
|
2019-01-24 06:04:42 +00:00
|
|
|
});
|
2021-11-10 12:31:15 +00:00
|
|
|
itFirefoxOnly('userDataDir option restores preferences', async () => {
|
|
|
|
const { defaultBrowserOptions, puppeteer } = getTestState();
|
|
|
|
|
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
|
|
|
|
|
|
|
const prefsJSPath = path.join(userDataDir, 'prefs.js');
|
|
|
|
const prefsJSContent = 'user_pref("browser.warnOnQuit", true)';
|
|
|
|
await writeFileAsync(prefsJSPath, prefsJSContent);
|
|
|
|
|
|
|
|
const options = Object.assign({ userDataDir }, defaultBrowserOptions);
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
// Open a page to make sure its functional.
|
|
|
|
await browser.newPage();
|
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
|
|
|
await browser.close();
|
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
|
|
|
|
|
|
|
expect(await readFileAsync(prefsJSPath, 'utf8')).toBe(prefsJSContent);
|
|
|
|
|
|
|
|
// This might throw. See https://github.com/puppeteer/puppeteer/issues/2778
|
|
|
|
await rmAsync(userDataDir).catch(() => {});
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('userDataDir argument', async () => {
|
|
|
|
const { isChrome, puppeteer, defaultBrowserOptions } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-24 06:04:42 +00:00
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
|
|
|
const options = Object.assign({}, defaultBrowserOptions);
|
2020-04-09 05:56:25 +00:00
|
|
|
if (isChrome) {
|
2019-02-20 06:08:09 +00:00
|
|
|
options.args = [
|
|
|
|
...(defaultBrowserOptions.args || []),
|
2020-05-07 10:54:55 +00:00
|
|
|
`--user-data-dir=${userDataDir}`,
|
2019-02-20 06:08:09 +00:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
options.args = [
|
|
|
|
...(defaultBrowserOptions.args || []),
|
2021-11-10 12:31:15 +00:00
|
|
|
'-profile',
|
2019-02-20 06:08:09 +00:00
|
|
|
userDataDir,
|
|
|
|
];
|
|
|
|
}
|
2019-01-24 06:04:42 +00:00
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
|
|
|
await browser.close();
|
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
2019-11-26 12:12:25 +00:00
|
|
|
// This might throw. See https://github.com/puppeteer/puppeteer/issues/2778
|
2022-02-23 19:21:17 +00:00
|
|
|
await rmAsync(userDataDir).catch(() => {});
|
|
|
|
});
|
|
|
|
itChromeOnly('userDataDir argument with non-existent dir', async () => {
|
|
|
|
const { isChrome, puppeteer, defaultBrowserOptions } = getTestState();
|
|
|
|
|
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
|
|
|
await rmAsync(userDataDir);
|
|
|
|
const options = Object.assign({}, defaultBrowserOptions);
|
|
|
|
if (isChrome) {
|
|
|
|
options.args = [
|
|
|
|
...(defaultBrowserOptions.args || []),
|
|
|
|
`--user-data-dir=${userDataDir}`,
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
options.args = [
|
|
|
|
...(defaultBrowserOptions.args || []),
|
|
|
|
'-profile',
|
|
|
|
userDataDir,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
|
|
|
await browser.close();
|
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
|
|
|
// This might throw. See https://github.com/puppeteer/puppeteer/issues/2778
|
2020-06-23 05:18:46 +00:00
|
|
|
await rmAsync(userDataDir).catch(() => {});
|
2019-01-24 06:04:42 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('userDataDir option should restore state', async () => {
|
|
|
|
const { server, puppeteer, defaultBrowserOptions } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-24 06:04:42 +00:00
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
2020-05-07 10:54:55 +00:00
|
|
|
const options = Object.assign({ userDataDir }, defaultBrowserOptions);
|
2019-01-24 06:04:42 +00:00
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.evaluate(() => (localStorage.hey = 'hello'));
|
2019-01-24 06:04:42 +00:00
|
|
|
await browser.close();
|
|
|
|
|
|
|
|
const browser2 = await puppeteer.launch(options);
|
|
|
|
const page2 = await browser2.newPage();
|
|
|
|
await page2.goto(server.EMPTY_PAGE);
|
|
|
|
expect(await page2.evaluate(() => localStorage.hey)).toBe('hello');
|
|
|
|
await browser2.close();
|
2019-11-26 12:12:25 +00:00
|
|
|
// This might throw. See https://github.com/puppeteer/puppeteer/issues/2778
|
2020-06-23 05:18:46 +00:00
|
|
|
await rmAsync(userDataDir).catch(() => {});
|
2019-01-24 06:04:42 +00:00
|
|
|
});
|
2020-06-19 14:39:03 +00:00
|
|
|
// This mysteriously fails on Windows on AppVeyor. See
|
|
|
|
// https://github.com/puppeteer/puppeteer/issues/4111
|
2020-05-07 10:54:55 +00:00
|
|
|
xit('userDataDir option should restore cookies', async () => {
|
2020-06-23 05:18:46 +00:00
|
|
|
const { server, puppeteer, defaultBrowserOptions } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-24 06:04:42 +00:00
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
2020-05-07 10:54:55 +00:00
|
|
|
const options = Object.assign({ userDataDir }, defaultBrowserOptions);
|
2019-01-24 06:04:42 +00:00
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.evaluate(
|
|
|
|
() =>
|
|
|
|
(document.cookie =
|
|
|
|
'doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT')
|
|
|
|
);
|
2019-01-24 06:04:42 +00:00
|
|
|
await browser.close();
|
|
|
|
|
|
|
|
const browser2 = await puppeteer.launch(options);
|
|
|
|
const page2 = await browser2.newPage();
|
|
|
|
await page2.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await page2.evaluate(() => document.cookie)).toBe(
|
|
|
|
'doSomethingOnlyOnce=true'
|
|
|
|
);
|
2019-01-24 06:04:42 +00:00
|
|
|
await browser2.close();
|
2019-11-26 12:12:25 +00:00
|
|
|
// This might throw. See https://github.com/puppeteer/puppeteer/issues/2778
|
2020-06-23 05:18:46 +00:00
|
|
|
await rmAsync(userDataDir).catch(() => {});
|
2019-01-24 06:04:42 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return the default arguments', async () => {
|
|
|
|
const { isChrome, isFirefox, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
if (isChrome) {
|
2019-02-20 06:08:09 +00:00
|
|
|
expect(puppeteer.defaultArgs()).toContain('--no-first-run');
|
|
|
|
expect(puppeteer.defaultArgs()).toContain('--headless');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(puppeteer.defaultArgs({ headless: false })).not.toContain(
|
|
|
|
'--headless'
|
|
|
|
);
|
|
|
|
expect(puppeteer.defaultArgs({ userDataDir: 'foo' })).toContain(
|
2020-10-13 10:59:58 +00:00
|
|
|
`--user-data-dir=${path.resolve('foo')}`
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
} else if (isFirefox) {
|
2020-02-20 12:57:15 +00:00
|
|
|
expect(puppeteer.defaultArgs()).toContain('--headless');
|
|
|
|
expect(puppeteer.defaultArgs()).toContain('--no-remote');
|
2021-11-10 12:31:15 +00:00
|
|
|
if (os.platform() === 'darwin') {
|
|
|
|
expect(puppeteer.defaultArgs()).toContain('--foreground');
|
|
|
|
} else {
|
|
|
|
expect(puppeteer.defaultArgs()).not.toContain('--foreground');
|
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(puppeteer.defaultArgs({ headless: false })).not.toContain(
|
|
|
|
'--headless'
|
|
|
|
);
|
|
|
|
expect(puppeteer.defaultArgs({ userDataDir: 'foo' })).toContain(
|
|
|
|
'--profile'
|
|
|
|
);
|
|
|
|
expect(puppeteer.defaultArgs({ userDataDir: 'foo' })).toContain(
|
|
|
|
'foo'
|
|
|
|
);
|
2019-02-20 06:08:09 +00:00
|
|
|
} else {
|
|
|
|
expect(puppeteer.defaultArgs()).toContain('-headless');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(puppeteer.defaultArgs({ headless: false })).not.toContain(
|
|
|
|
'-headless'
|
|
|
|
);
|
|
|
|
expect(puppeteer.defaultArgs({ userDataDir: 'foo' })).toContain(
|
|
|
|
'-profile'
|
|
|
|
);
|
|
|
|
expect(puppeteer.defaultArgs({ userDataDir: 'foo' })).toContain(
|
2020-10-13 10:59:58 +00:00
|
|
|
path.resolve('foo')
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2019-02-20 06:08:09 +00:00
|
|
|
}
|
2019-01-24 06:04:42 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report the correct product', async () => {
|
|
|
|
const { isChrome, isFirefox, puppeteer } = getTestState();
|
|
|
|
if (isChrome) expect(puppeteer.product).toBe('chrome');
|
|
|
|
else if (isFirefox) expect(puppeteer.product).toBe('firefox');
|
2019-11-26 09:23:19 +00:00
|
|
|
});
|
2021-11-10 12:31:15 +00:00
|
|
|
it('should work with no default arguments', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { defaultBrowserOptions, puppeteer } = getTestState();
|
2019-01-24 06:04:42 +00:00
|
|
|
const options = Object.assign({}, defaultBrowserOptions);
|
|
|
|
options.ignoreDefaultArgs = true;
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
expect(await page.evaluate('11 * 11')).toBe(121);
|
|
|
|
await page.close();
|
|
|
|
await browser.close();
|
|
|
|
});
|
2022-04-20 09:16:42 +00:00
|
|
|
itChromeOnly(
|
|
|
|
'should filter out ignored default arguments in Chrome',
|
|
|
|
async () => {
|
|
|
|
const { defaultBrowserOptions, puppeteer } = getTestState();
|
|
|
|
// Make sure we launch with `--enable-automation` by default.
|
|
|
|
const defaultArgs = puppeteer.defaultArgs();
|
|
|
|
const browser = await puppeteer.launch(
|
|
|
|
Object.assign({}, defaultBrowserOptions, {
|
|
|
|
// Ignore first and third default argument.
|
|
|
|
ignoreDefaultArgs: [defaultArgs[0], defaultArgs[2]],
|
|
|
|
})
|
|
|
|
);
|
|
|
|
const spawnargs = browser.process().spawnargs;
|
|
|
|
if (!spawnargs) {
|
|
|
|
throw new Error('spawnargs not present');
|
|
|
|
}
|
|
|
|
expect(spawnargs.indexOf(defaultArgs[0])).toBe(-1);
|
|
|
|
expect(spawnargs.indexOf(defaultArgs[1])).not.toBe(-1);
|
|
|
|
expect(spawnargs.indexOf(defaultArgs[2])).toBe(-1);
|
|
|
|
await browser.close();
|
2020-06-23 05:18:46 +00:00
|
|
|
}
|
2022-04-20 09:16:42 +00:00
|
|
|
);
|
|
|
|
itFirefoxOnly(
|
|
|
|
'should filter out ignored default argument in Firefox',
|
|
|
|
async () => {
|
|
|
|
const { defaultBrowserOptions, puppeteer } = getTestState();
|
2022-04-19 12:09:23 +00:00
|
|
|
|
2022-04-20 09:16:42 +00:00
|
|
|
const defaultArgs = puppeteer.defaultArgs();
|
|
|
|
const browser = await puppeteer.launch(
|
|
|
|
Object.assign({}, defaultBrowserOptions, {
|
|
|
|
// Only the first argument is fixed, others are optional.
|
|
|
|
ignoreDefaultArgs: [defaultArgs[0]],
|
|
|
|
})
|
|
|
|
);
|
|
|
|
const spawnargs = browser.process().spawnargs;
|
|
|
|
if (!spawnargs) {
|
|
|
|
throw new Error('spawnargs not present');
|
|
|
|
}
|
|
|
|
expect(spawnargs.indexOf(defaultArgs[0])).toBe(-1);
|
|
|
|
expect(spawnargs.indexOf(defaultArgs[1])).not.toBe(-1);
|
|
|
|
await browser.close();
|
2022-04-19 12:09:23 +00:00
|
|
|
}
|
2022-04-20 09:16:42 +00:00
|
|
|
);
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should have default URL when launching browser', async function () {
|
|
|
|
const { defaultBrowserOptions, puppeteer } = getTestState();
|
2019-01-24 06:04:42 +00:00
|
|
|
const browser = await puppeteer.launch(defaultBrowserOptions);
|
2020-05-07 10:54:55 +00:00
|
|
|
const pages = (await browser.pages()).map((page) => page.url());
|
2019-01-24 06:04:42 +00:00
|
|
|
expect(pages).toEqual(['about:blank']);
|
|
|
|
await browser.close();
|
|
|
|
});
|
2022-04-27 15:34:53 +00:00
|
|
|
itFailsFirefox(
|
|
|
|
'should have custom URL when launching browser',
|
|
|
|
async () => {
|
|
|
|
const { server, puppeteer, defaultBrowserOptions } = getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-04-27 15:34:53 +00:00
|
|
|
const options = Object.assign({}, defaultBrowserOptions);
|
|
|
|
options.args = [server.EMPTY_PAGE].concat(options.args || []);
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const pages = await browser.pages();
|
|
|
|
expect(pages.length).toBe(1);
|
|
|
|
const page = pages[0];
|
|
|
|
if (page.url() !== server.EMPTY_PAGE) await page.waitForNavigation();
|
|
|
|
expect(page.url()).toBe(server.EMPTY_PAGE);
|
|
|
|
await browser.close();
|
|
|
|
}
|
|
|
|
);
|
2021-09-27 08:01:35 +00:00
|
|
|
it('should pass the timeout parameter to browser.waitForTarget', async () => {
|
|
|
|
const { puppeteer, defaultBrowserOptions } = getTestState();
|
|
|
|
const options = Object.assign({}, defaultBrowserOptions, {
|
|
|
|
timeout: 1,
|
|
|
|
});
|
|
|
|
let error = null;
|
|
|
|
await puppeteer.launch(options).catch((error_) => (error = error_));
|
|
|
|
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should set the default viewport', async () => {
|
|
|
|
const { puppeteer, defaultBrowserOptions } = getTestState();
|
2019-01-24 06:04:42 +00:00
|
|
|
const options = Object.assign({}, defaultBrowserOptions, {
|
|
|
|
defaultViewport: {
|
|
|
|
width: 456,
|
2020-05-07 10:54:55 +00:00
|
|
|
height: 789,
|
|
|
|
},
|
2019-01-24 06:04:42 +00:00
|
|
|
});
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
expect(await page.evaluate('window.innerWidth')).toBe(456);
|
|
|
|
expect(await page.evaluate('window.innerHeight')).toBe(789);
|
|
|
|
await browser.close();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should disable the default viewport', async () => {
|
|
|
|
const { puppeteer, defaultBrowserOptions } = getTestState();
|
2019-01-24 06:04:42 +00:00
|
|
|
const options = Object.assign({}, defaultBrowserOptions, {
|
2020-05-07 10:54:55 +00:00
|
|
|
defaultViewport: null,
|
2019-01-24 06:04:42 +00:00
|
|
|
});
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
expect(page.viewport()).toBe(null);
|
|
|
|
await browser.close();
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should take fullPage screenshots when defaultViewport is null', async () => {
|
|
|
|
const { server, puppeteer, defaultBrowserOptions } = getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
const options = Object.assign({}, defaultBrowserOptions, {
|
|
|
|
defaultViewport: null,
|
|
|
|
});
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
|
|
|
const screenshot = await page.screenshot({
|
|
|
|
fullPage: true,
|
|
|
|
});
|
|
|
|
expect(screenshot).toBeInstanceOf(Buffer);
|
|
|
|
await browser.close();
|
|
|
|
});
|
2021-09-29 14:05:18 +00:00
|
|
|
it('should set the debugging port', async () => {
|
|
|
|
const { puppeteer, defaultBrowserOptions } = getTestState();
|
|
|
|
|
|
|
|
const options = Object.assign({}, defaultBrowserOptions, {
|
|
|
|
defaultViewport: null,
|
|
|
|
debuggingPort: 9999,
|
|
|
|
});
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const url = new URL(browser.wsEndpoint());
|
|
|
|
await browser.close();
|
|
|
|
expect(url.port).toBe('9999');
|
|
|
|
});
|
|
|
|
it('should not allow setting debuggingPort and pipe', async () => {
|
|
|
|
const { puppeteer, defaultBrowserOptions } = getTestState();
|
|
|
|
|
|
|
|
const options = Object.assign({}, defaultBrowserOptions, {
|
|
|
|
defaultViewport: null,
|
|
|
|
debuggingPort: 9999,
|
|
|
|
pipe: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
let error = null;
|
|
|
|
await puppeteer.launch(options).catch((error_) => (error = error_));
|
|
|
|
expect(error.message).toContain('either pipe or debugging port');
|
|
|
|
});
|
2021-05-06 20:30:04 +00:00
|
|
|
itChromeOnly(
|
|
|
|
'should launch Chrome properly with --no-startup-window and waitForInitialPage=false',
|
|
|
|
async () => {
|
|
|
|
const { defaultBrowserOptions, puppeteer } = getTestState();
|
|
|
|
const options = {
|
|
|
|
waitForInitialPage: false,
|
|
|
|
// This is needed to prevent Puppeteer from adding an initial blank page.
|
|
|
|
// See also https://github.com/puppeteer/puppeteer/blob/ad6b736039436fcc5c0a262e5b575aa041427be3/src/node/Launcher.ts#L200
|
|
|
|
ignoreDefaultArgs: true,
|
|
|
|
...defaultBrowserOptions,
|
2022-05-02 09:37:21 +00:00
|
|
|
args: ['--no-startup-window'],
|
2021-05-06 20:30:04 +00:00
|
|
|
};
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const pages = await browser.pages();
|
|
|
|
expect(pages.length).toBe(0);
|
|
|
|
await browser.close();
|
|
|
|
}
|
|
|
|
);
|
2019-01-24 06:04:42 +00:00
|
|
|
});
|
2020-04-17 13:27:50 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Puppeteer.launch', function () {
|
2020-04-15 11:30:42 +00:00
|
|
|
let productName;
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
before(async () => {
|
|
|
|
const { puppeteer } = getTestState();
|
2020-04-15 11:30:42 +00:00
|
|
|
productName = puppeteer._productName;
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
after(async () => {
|
|
|
|
const { puppeteer } = getTestState();
|
2020-10-12 09:30:35 +00:00
|
|
|
// @ts-expect-error launcher is a private property that users can't
|
|
|
|
// touch, but for testing purposes we need to reset it.
|
2020-04-15 11:30:42 +00:00
|
|
|
puppeteer._lazyLauncher = undefined;
|
|
|
|
puppeteer._productName = productName;
|
|
|
|
});
|
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
itOnlyRegularInstall('should be able to launch Chrome', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { puppeteer } = getTestState();
|
|
|
|
const browser = await puppeteer.launch({ product: 'chrome' });
|
2020-04-17 13:27:50 +00:00
|
|
|
const userAgent = await browser.userAgent();
|
|
|
|
await browser.close();
|
|
|
|
expect(userAgent).toContain('Chrome');
|
|
|
|
});
|
2020-04-15 11:30:42 +00:00
|
|
|
|
2021-11-10 12:31:15 +00:00
|
|
|
itOnlyRegularInstall(
|
|
|
|
'falls back to launching chrome if there is an unknown product but logs a warning',
|
|
|
|
async () => {
|
|
|
|
const { puppeteer } = getTestState();
|
|
|
|
const consoleStub = sinon.stub(console, 'warn');
|
|
|
|
const browser = await puppeteer.launch({
|
|
|
|
// @ts-expect-error purposeful bad input
|
|
|
|
product: 'SO_NOT_A_PRODUCT',
|
|
|
|
});
|
|
|
|
const userAgent = await browser.userAgent();
|
|
|
|
await browser.close();
|
|
|
|
expect(userAgent).toContain('Chrome');
|
|
|
|
expect(consoleStub.callCount).toEqual(1);
|
|
|
|
expect(consoleStub.firstCall.args).toEqual([
|
|
|
|
'Warning: unknown product name SO_NOT_A_PRODUCT. Falling back to chrome.',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
);
|
2020-05-12 09:30:24 +00:00
|
|
|
|
2021-04-20 06:56:20 +00:00
|
|
|
itOnlyRegularInstall(
|
|
|
|
'should be able to launch Firefox',
|
|
|
|
async function () {
|
|
|
|
this.timeout(FIREFOX_TIMEOUT);
|
|
|
|
const { puppeteer } = getTestState();
|
|
|
|
const browser = await puppeteer.launch({ product: 'firefox' });
|
|
|
|
const userAgent = await browser.userAgent();
|
|
|
|
await browser.close();
|
|
|
|
expect(userAgent).toContain('Firefox');
|
|
|
|
}
|
|
|
|
);
|
2020-04-15 11:30:42 +00:00
|
|
|
});
|
2020-04-17 13:27:50 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Puppeteer.connect', function () {
|
|
|
|
it('should be able to connect multiple times to the same browser', async () => {
|
|
|
|
const { puppeteer, defaultBrowserOptions } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-24 06:04:42 +00:00
|
|
|
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
|
2020-04-09 05:56:25 +00:00
|
|
|
const otherBrowser = await puppeteer.connect({
|
2020-05-07 10:54:55 +00:00
|
|
|
browserWSEndpoint: originalBrowser.wsEndpoint(),
|
2019-01-24 06:04:42 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
const page = await otherBrowser.newPage();
|
2019-01-24 06:04:42 +00:00
|
|
|
expect(await page.evaluate(() => 7 * 8)).toBe(56);
|
2020-04-09 05:56:25 +00:00
|
|
|
otherBrowser.disconnect();
|
2019-01-24 06:04:42 +00:00
|
|
|
|
|
|
|
const secondPage = await originalBrowser.newPage();
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await secondPage.evaluate(() => 7 * 6)).toBe(42);
|
2019-01-24 06:04:42 +00:00
|
|
|
await originalBrowser.close();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should be able to close remote browser', async () => {
|
|
|
|
const { defaultBrowserOptions, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-16 02:26:14 +00:00
|
|
|
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const remoteBrowser = await puppeteer.connect({
|
2020-05-07 10:54:55 +00:00
|
|
|
browserWSEndpoint: originalBrowser.wsEndpoint(),
|
2019-02-16 02:26:14 +00:00
|
|
|
});
|
|
|
|
await Promise.all([
|
|
|
|
utils.waitEvent(originalBrowser, 'disconnected'),
|
|
|
|
remoteBrowser.close(),
|
|
|
|
]);
|
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should support ignoreHTTPSErrors option', async () => {
|
2021-05-12 14:48:30 +00:00
|
|
|
const { httpsServer, puppeteer, defaultBrowserOptions } =
|
|
|
|
getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-24 06:04:42 +00:00
|
|
|
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const browserWSEndpoint = originalBrowser.wsEndpoint();
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const browser = await puppeteer.connect({
|
|
|
|
browserWSEndpoint,
|
|
|
|
ignoreHTTPSErrors: true,
|
|
|
|
});
|
2019-01-24 06:04:42 +00:00
|
|
|
const page = await browser.newPage();
|
|
|
|
let error = null;
|
2019-07-15 01:33:31 +00:00
|
|
|
const [serverRequest, response] = await Promise.all([
|
|
|
|
httpsServer.waitForRequest('/empty.html'),
|
2020-05-07 10:54:55 +00:00
|
|
|
page.goto(httpsServer.EMPTY_PAGE).catch((error_) => (error = error_)),
|
2019-07-15 01:33:31 +00:00
|
|
|
]);
|
2019-01-24 06:04:42 +00:00
|
|
|
expect(error).toBe(null);
|
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
expect(response.securityDetails()).toBeTruthy();
|
2019-07-15 01:33:31 +00:00
|
|
|
const protocol = serverRequest.socket.getProtocol().replace('v', ' ');
|
|
|
|
expect(response.securityDetails().protocol()).toBe(protocol);
|
2019-01-24 06:04:42 +00:00
|
|
|
await page.close();
|
|
|
|
await browser.close();
|
|
|
|
});
|
2021-10-27 11:43:57 +00:00
|
|
|
// @see https://github.com/puppeteer/puppeteer/issues/4197
|
|
|
|
itFailsFirefox('should support targetFilter option', async () => {
|
2021-05-03 11:48:31 +00:00
|
|
|
const { server, puppeteer, defaultBrowserOptions } = getTestState();
|
|
|
|
|
|
|
|
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const browserWSEndpoint = originalBrowser.wsEndpoint();
|
|
|
|
|
|
|
|
const page1 = await originalBrowser.newPage();
|
|
|
|
await page1.goto(server.EMPTY_PAGE);
|
|
|
|
|
|
|
|
const page2 = await originalBrowser.newPage();
|
|
|
|
await page2.goto(server.EMPTY_PAGE + '?should-be-ignored');
|
|
|
|
|
|
|
|
const browser = await puppeteer.connect({
|
|
|
|
browserWSEndpoint,
|
|
|
|
targetFilter: (targetInfo: Protocol.Target.TargetInfo) =>
|
2021-10-27 11:43:57 +00:00
|
|
|
!targetInfo.url?.includes('should-be-ignored'),
|
2021-05-03 11:48:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const pages = await browser.pages();
|
|
|
|
|
|
|
|
await page2.close();
|
|
|
|
await page1.close();
|
2021-10-27 11:43:57 +00:00
|
|
|
await browser.disconnect();
|
|
|
|
await originalBrowser.close();
|
2021-05-03 11:48:31 +00:00
|
|
|
|
|
|
|
expect(pages.map((p: Page) => p.url()).sort()).toEqual([
|
|
|
|
'about:blank',
|
|
|
|
server.EMPTY_PAGE,
|
|
|
|
]);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox(
|
|
|
|
'should be able to reconnect to a disconnected browser',
|
|
|
|
async () => {
|
|
|
|
const { server, puppeteer, defaultBrowserOptions } = getTestState();
|
|
|
|
|
|
|
|
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const browserWSEndpoint = originalBrowser.wsEndpoint();
|
|
|
|
const page = await originalBrowser.newPage();
|
|
|
|
await page.goto(server.PREFIX + '/frames/nested-frames.html');
|
|
|
|
originalBrowser.disconnect();
|
|
|
|
|
|
|
|
const browser = await puppeteer.connect({ browserWSEndpoint });
|
|
|
|
const pages = await browser.pages();
|
|
|
|
const restoredPage = pages.find(
|
|
|
|
(page) =>
|
|
|
|
page.url() === server.PREFIX + '/frames/nested-frames.html'
|
|
|
|
);
|
|
|
|
expect(utils.dumpFrames(restoredPage.mainFrame())).toEqual([
|
|
|
|
'http://localhost:<PORT>/frames/nested-frames.html',
|
|
|
|
' http://localhost:<PORT>/frames/two-frames.html (2frames)',
|
|
|
|
' http://localhost:<PORT>/frames/frame.html (uno)',
|
|
|
|
' http://localhost:<PORT>/frames/frame.html (dos)',
|
|
|
|
' http://localhost:<PORT>/frames/frame.html (aframe)',
|
|
|
|
]);
|
|
|
|
expect(await restoredPage.evaluate(() => 7 * 8)).toBe(56);
|
|
|
|
await browser.close();
|
|
|
|
}
|
|
|
|
);
|
2019-11-26 12:12:25 +00:00
|
|
|
// @see https://github.com/puppeteer/puppeteer/issues/4197#issuecomment-481793410
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox(
|
|
|
|
'should be able to connect to the same page simultaneously',
|
|
|
|
async () => {
|
2022-03-03 14:05:49 +00:00
|
|
|
const { puppeteer, defaultBrowserOptions } = getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-03-03 14:05:49 +00:00
|
|
|
const browserOne = await puppeteer.launch(defaultBrowserOptions);
|
2020-05-07 10:54:55 +00:00
|
|
|
const browserTwo = await puppeteer.connect({
|
|
|
|
browserWSEndpoint: browserOne.wsEndpoint(),
|
|
|
|
});
|
|
|
|
const [page1, page2] = await Promise.all([
|
2020-06-23 05:18:46 +00:00
|
|
|
new Promise<Page>((x) =>
|
2020-05-07 10:54:55 +00:00
|
|
|
browserOne.once('targetcreated', (target) => x(target.page()))
|
|
|
|
),
|
|
|
|
browserTwo.newPage(),
|
|
|
|
]);
|
|
|
|
expect(await page1.evaluate(() => 7 * 8)).toBe(56);
|
|
|
|
expect(await page2.evaluate(() => 7 * 6)).toBe(42);
|
|
|
|
await browserOne.close();
|
|
|
|
}
|
|
|
|
);
|
2021-04-22 07:55:49 +00:00
|
|
|
it('should be able to reconnect', async () => {
|
2022-03-03 14:05:49 +00:00
|
|
|
const { puppeteer, server, defaultBrowserOptions } = getTestState();
|
|
|
|
const browserOne = await puppeteer.launch(defaultBrowserOptions);
|
2020-11-26 11:43:42 +00:00
|
|
|
const browserWSEndpoint = browserOne.wsEndpoint();
|
|
|
|
const pageOne = await browserOne.newPage();
|
|
|
|
await pageOne.goto(server.EMPTY_PAGE);
|
|
|
|
browserOne.disconnect();
|
|
|
|
|
|
|
|
const browserTwo = await puppeteer.connect({ browserWSEndpoint });
|
|
|
|
const pages = await browserTwo.pages();
|
|
|
|
const pageTwo = pages.find((page) => page.url() === server.EMPTY_PAGE);
|
|
|
|
await pageTwo.reload();
|
|
|
|
const bodyHandle = await pageTwo.waitForSelector('body', {
|
|
|
|
timeout: 10000,
|
|
|
|
});
|
|
|
|
await bodyHandle.dispose();
|
|
|
|
await browserTwo.close();
|
|
|
|
});
|
2019-01-24 06:04:42 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Puppeteer.executablePath', function () {
|
2020-06-12 13:55:51 +00:00
|
|
|
itOnlyRegularInstall('should work', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-24 06:04:42 +00:00
|
|
|
const executablePath = puppeteer.executablePath();
|
|
|
|
expect(fs.existsSync(executablePath)).toBe(true);
|
2019-03-05 07:46:14 +00:00
|
|
|
expect(fs.realpathSync(executablePath)).toBe(executablePath);
|
2019-01-24 06:04:42 +00:00
|
|
|
});
|
2021-07-09 12:43:42 +00:00
|
|
|
it('returns executablePath for channel', () => {
|
|
|
|
const { puppeteer } = getTestState();
|
|
|
|
|
|
|
|
const executablePath = puppeteer.executablePath('chrome');
|
|
|
|
expect(executablePath).toBeTruthy();
|
|
|
|
});
|
2022-02-11 13:07:31 +00:00
|
|
|
describe('when PUPPETEER_EXECUTABLE_PATH is set', () => {
|
|
|
|
const sandbox = sinon.createSandbox();
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
process.env.PUPPETEER_EXECUTABLE_PATH = '';
|
|
|
|
sandbox
|
|
|
|
.stub(process.env, 'PUPPETEER_EXECUTABLE_PATH')
|
|
|
|
.value('SOME_CUSTOM_EXECUTABLE');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => sandbox.restore());
|
|
|
|
|
|
|
|
it('its value is returned', async () => {
|
|
|
|
const { puppeteer } = getTestState();
|
|
|
|
|
|
|
|
const executablePath = puppeteer.executablePath();
|
|
|
|
|
|
|
|
expect(executablePath).toEqual('SOME_CUSTOM_EXECUTABLE');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when the product is chrome, platform is not darwin, and arch is arm64', () => {
|
|
|
|
describe('and the executable exists', () => {
|
|
|
|
itChromeOnly('returns /usr/bin/chromium-browser', async () => {
|
|
|
|
const { puppeteer } = getTestState();
|
|
|
|
const osPlatformStub = sinon.stub(os, 'platform').returns('linux');
|
|
|
|
const osArchStub = sinon.stub(os, 'arch').returns('arm64');
|
|
|
|
const fsExistsStub = sinon.stub(fs, 'existsSync');
|
|
|
|
fsExistsStub.withArgs('/usr/bin/chromium-browser').returns(true);
|
|
|
|
|
|
|
|
const executablePath = puppeteer.executablePath();
|
|
|
|
|
|
|
|
expect(executablePath).toEqual('/usr/bin/chromium-browser');
|
|
|
|
|
|
|
|
osPlatformStub.restore();
|
|
|
|
osArchStub.restore();
|
|
|
|
fsExistsStub.restore();
|
|
|
|
});
|
|
|
|
describe('and PUPPETEER_EXECUTABLE_PATH is set', () => {
|
|
|
|
const sandbox = sinon.createSandbox();
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
process.env.PUPPETEER_EXECUTABLE_PATH = '';
|
|
|
|
sandbox
|
|
|
|
.stub(process.env, 'PUPPETEER_EXECUTABLE_PATH')
|
|
|
|
.value('SOME_CUSTOM_EXECUTABLE');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => sandbox.restore());
|
|
|
|
|
|
|
|
it('its value is returned', async () => {
|
|
|
|
const { puppeteer } = getTestState();
|
|
|
|
|
|
|
|
const executablePath = puppeteer.executablePath();
|
|
|
|
|
|
|
|
expect(executablePath).toEqual('SOME_CUSTOM_EXECUTABLE');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('and the executable does not exist', () => {
|
|
|
|
itChromeOnly(
|
|
|
|
'does not return /usr/bin/chromium-browser',
|
|
|
|
async () => {
|
|
|
|
const { puppeteer } = getTestState();
|
|
|
|
const osPlatformStub = sinon
|
|
|
|
.stub(os, 'platform')
|
|
|
|
.returns('linux');
|
|
|
|
const osArchStub = sinon.stub(os, 'arch').returns('arm64');
|
|
|
|
const fsExistsStub = sinon.stub(fs, 'existsSync');
|
|
|
|
fsExistsStub.withArgs('/usr/bin/chromium-browser').returns(false);
|
|
|
|
|
|
|
|
const executablePath = puppeteer.executablePath();
|
|
|
|
|
|
|
|
expect(executablePath).not.toEqual('/usr/bin/chromium-browser');
|
|
|
|
|
|
|
|
osPlatformStub.restore();
|
|
|
|
osArchStub.restore();
|
|
|
|
fsExistsStub.restore();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2019-01-24 06:04:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Browser target events', function () {
|
|
|
|
itFailsFirefox('should work', async () => {
|
|
|
|
const { server, puppeteer, defaultBrowserOptions } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-24 06:04:42 +00:00
|
|
|
const browser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const events = [];
|
|
|
|
browser.on('targetcreated', () => events.push('CREATED'));
|
|
|
|
browser.on('targetchanged', () => events.push('CHANGED'));
|
|
|
|
browser.on('targetdestroyed', () => events.push('DESTROYED'));
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.close();
|
|
|
|
expect(events).toEqual(['CREATED', 'CHANGED', 'DESTROYED']);
|
|
|
|
await browser.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Browser.Events.disconnected', function () {
|
|
|
|
it('should be emitted when: browser gets closed, disconnected or underlying websocket gets closed', async () => {
|
|
|
|
const { puppeteer, defaultBrowserOptions } = getTestState();
|
2019-01-24 06:04:42 +00:00
|
|
|
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const browserWSEndpoint = originalBrowser.wsEndpoint();
|
2020-05-07 10:54:55 +00:00
|
|
|
const remoteBrowser1 = await puppeteer.connect({ browserWSEndpoint });
|
|
|
|
const remoteBrowser2 = await puppeteer.connect({ browserWSEndpoint });
|
2019-01-24 06:04:42 +00:00
|
|
|
|
|
|
|
let disconnectedOriginal = 0;
|
|
|
|
let disconnectedRemote1 = 0;
|
|
|
|
let disconnectedRemote2 = 0;
|
|
|
|
originalBrowser.on('disconnected', () => ++disconnectedOriginal);
|
|
|
|
remoteBrowser1.on('disconnected', () => ++disconnectedRemote1);
|
|
|
|
remoteBrowser2.on('disconnected', () => ++disconnectedRemote2);
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
utils.waitEvent(remoteBrowser2, 'disconnected'),
|
|
|
|
remoteBrowser2.disconnect(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(disconnectedOriginal).toBe(0);
|
|
|
|
expect(disconnectedRemote1).toBe(0);
|
|
|
|
expect(disconnectedRemote2).toBe(1);
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
utils.waitEvent(remoteBrowser1, 'disconnected'),
|
|
|
|
utils.waitEvent(originalBrowser, 'disconnected'),
|
|
|
|
originalBrowser.close(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(disconnectedOriginal).toBe(1);
|
|
|
|
expect(disconnectedRemote1).toBe(1);
|
|
|
|
expect(disconnectedRemote2).toBe(1);
|
|
|
|
});
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|