2020-04-09 05:56:25 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2020 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-10 13:27:42 +00:00
|
|
|
import Protocol from 'devtools-protocol';
|
|
|
|
import expect from 'expect';
|
2020-06-23 05:18:46 +00:00
|
|
|
import * as fs from 'fs';
|
2022-06-10 13:27:42 +00:00
|
|
|
import * as path from 'path';
|
|
|
|
import rimraf from 'rimraf';
|
2020-06-23 05:18:46 +00:00
|
|
|
import sinon from 'sinon';
|
2022-09-14 14:40:58 +00:00
|
|
|
import {Browser, BrowserContext} from '../../lib/cjs/puppeteer/api/Browser.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {Page} from '../../lib/cjs/puppeteer/common/Page.js';
|
2022-08-17 12:39:41 +00:00
|
|
|
import {isErrorLike} from '../../lib/cjs/puppeteer/util/ErrorLike.js';
|
2022-06-15 10:09:22 +00:00
|
|
|
import {
|
|
|
|
PuppeteerLaunchOptions,
|
|
|
|
PuppeteerNode,
|
|
|
|
} from '../../lib/cjs/puppeteer/node/Puppeteer.js';
|
2022-06-15 10:05:25 +00:00
|
|
|
import puppeteer from '../../lib/cjs/puppeteer/puppeteer.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {TestServer} from '../../utils/testserver/lib/index.js';
|
|
|
|
import {extendExpectWithToBeGolden} from './utils.js';
|
2022-09-08 10:32:39 +00:00
|
|
|
import * as Mocha from 'mocha';
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const setupServer = async () => {
|
2022-06-15 10:05:25 +00:00
|
|
|
const assetsPath = path.join(__dirname, '../assets');
|
|
|
|
const cachedPath = path.join(__dirname, '../assets', 'cached');
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
const server = await TestServer.create(assetsPath);
|
|
|
|
const port = server.port;
|
2020-04-09 05:56:25 +00:00
|
|
|
server.enableHTTPCache(cachedPath);
|
|
|
|
server.PORT = port;
|
|
|
|
server.PREFIX = `http://localhost:${port}`;
|
|
|
|
server.CROSS_PROCESS_PREFIX = `http://127.0.0.1:${port}`;
|
|
|
|
server.EMPTY_PAGE = `http://localhost:${port}/empty.html`;
|
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
const httpsServer = await TestServer.createHTTPS(assetsPath);
|
|
|
|
const httpsPort = httpsServer.port;
|
2020-04-09 05:56:25 +00:00
|
|
|
httpsServer.enableHTTPCache(cachedPath);
|
|
|
|
httpsServer.PORT = httpsPort;
|
|
|
|
httpsServer.PREFIX = `https://localhost:${httpsPort}`;
|
|
|
|
httpsServer.CROSS_PROCESS_PREFIX = `https://127.0.0.1:${httpsPort}`;
|
|
|
|
httpsServer.EMPTY_PAGE = `https://localhost:${httpsPort}/empty.html`;
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
return {server, httpsServer};
|
2020-04-09 05:56:25 +00:00
|
|
|
};
|
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
export const getTestState = (): PuppeteerTestState => {
|
|
|
|
return state as PuppeteerTestState;
|
|
|
|
};
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const product =
|
2022-09-08 10:32:39 +00:00
|
|
|
process.env['PRODUCT'] || process.env['PUPPETEER_PRODUCT'] || 'chrome';
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-05-31 14:34:16 +00:00
|
|
|
const alternativeInstall = process.env['PUPPETEER_ALT_INSTALL'] || false;
|
2020-06-12 13:55:51 +00:00
|
|
|
|
2022-05-31 14:34:16 +00:00
|
|
|
const headless = (process.env['HEADLESS'] || 'true').trim().toLowerCase();
|
2022-04-25 11:11:23 +00:00
|
|
|
const isHeadless = headless === 'true' || headless === 'chrome';
|
2020-04-09 05:56:25 +00:00
|
|
|
const isFirefox = product === 'firefox';
|
2022-09-08 10:32:39 +00:00
|
|
|
const isChrome = product === 'chrome';
|
2022-09-14 14:40:58 +00:00
|
|
|
const protocol = process.env['PUPPETEER_PROTOCOL'] || 'cdp';
|
2020-06-12 13:55:51 +00:00
|
|
|
|
|
|
|
let extraLaunchOptions = {};
|
|
|
|
try {
|
2022-05-31 14:34:16 +00:00
|
|
|
extraLaunchOptions = JSON.parse(process.env['EXTRA_LAUNCH_OPTIONS'] || '{}');
|
2020-06-12 13:55:51 +00:00
|
|
|
} catch (error) {
|
2022-06-10 13:27:42 +00:00
|
|
|
if (isErrorLike(error)) {
|
2022-05-31 14:34:16 +00:00
|
|
|
console.warn(
|
|
|
|
`Error parsing EXTRA_LAUNCH_OPTIONS: ${error.message}. Skipping.`
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
2020-06-12 13:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultBrowserOptions = Object.assign(
|
|
|
|
{
|
2020-06-15 13:02:00 +00:00
|
|
|
handleSIGINT: true,
|
2022-05-31 14:34:16 +00:00
|
|
|
executablePath: process.env['BINARY'],
|
2022-04-25 11:11:23 +00:00
|
|
|
headless: headless === 'chrome' ? ('chrome' as const) : isHeadless,
|
2022-05-31 14:34:16 +00:00
|
|
|
dumpio: !!process.env['DUMPIO'],
|
2022-09-14 14:40:58 +00:00
|
|
|
protocol: protocol as 'cdp' | 'webDriverBiDi',
|
2020-06-12 13:55:51 +00:00
|
|
|
},
|
|
|
|
extraLaunchOptions
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
(async (): Promise<void> => {
|
2020-04-15 11:30:42 +00:00
|
|
|
if (defaultBrowserOptions.executablePath) {
|
2020-05-07 10:54:55 +00:00
|
|
|
console.warn(
|
|
|
|
`WARN: running ${product} tests with ${defaultBrowserOptions.executablePath}`
|
|
|
|
);
|
2020-04-15 11:30:42 +00:00
|
|
|
} else {
|
2020-10-12 09:30:35 +00:00
|
|
|
// TODO(jackfranklin): declare updateRevision in some form for the Firefox
|
|
|
|
// launcher.
|
2022-06-14 11:55:35 +00:00
|
|
|
if (product === 'firefox') {
|
|
|
|
// @ts-expect-error _updateRevision is defined on the FF launcher
|
|
|
|
// but not the Chrome one. The types need tidying so that TS can infer that
|
|
|
|
// properly and not error here.
|
|
|
|
await puppeteer._launcher._updateRevision();
|
|
|
|
}
|
2020-04-15 11:30:42 +00:00
|
|
|
const executablePath = puppeteer.executablePath();
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!fs.existsSync(executablePath)) {
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(
|
|
|
|
`Browser is not downloaded at ${executablePath}. Run 'npm install' and try to re-run tests`
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2020-04-15 11:30:42 +00:00
|
|
|
}
|
|
|
|
})();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-06-23 14:02:09 +00:00
|
|
|
declare module 'expect/build/types' {
|
|
|
|
interface Matchers<R> {
|
|
|
|
toBeGolden(x: string): R;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
const setupGoldenAssertions = (): void => {
|
2022-09-08 10:32:39 +00:00
|
|
|
let suffix = product.toLowerCase();
|
|
|
|
if (suffix === 'chrome') {
|
|
|
|
// TODO: to avoid moving golden folders.
|
|
|
|
suffix = 'chromium';
|
|
|
|
}
|
2022-06-15 10:05:25 +00:00
|
|
|
const GOLDEN_DIR = path.join(__dirname, `../golden-${suffix}`);
|
|
|
|
const OUTPUT_DIR = path.join(__dirname, `../output-${suffix}`);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (fs.existsSync(OUTPUT_DIR)) {
|
|
|
|
rimraf.sync(OUTPUT_DIR);
|
|
|
|
}
|
2022-06-15 10:09:22 +00:00
|
|
|
extendExpectWithToBeGolden(GOLDEN_DIR, OUTPUT_DIR);
|
2020-04-09 05:56:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
setupGoldenAssertions();
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
interface PuppeteerTestState {
|
|
|
|
browser: Browser;
|
|
|
|
context: BrowserContext;
|
|
|
|
page: Page;
|
2020-10-13 15:19:26 +00:00
|
|
|
puppeteer: PuppeteerNode;
|
2022-06-15 10:09:22 +00:00
|
|
|
defaultBrowserOptions: PuppeteerLaunchOptions;
|
2022-06-15 10:05:25 +00:00
|
|
|
server: TestServer;
|
|
|
|
httpsServer: TestServer;
|
2020-06-23 05:18:46 +00:00
|
|
|
isFirefox: boolean;
|
|
|
|
isChrome: boolean;
|
|
|
|
isHeadless: boolean;
|
2022-05-19 07:41:29 +00:00
|
|
|
headless: string;
|
2020-06-23 05:18:46 +00:00
|
|
|
puppeteerPath: string;
|
|
|
|
}
|
|
|
|
const state: Partial<PuppeteerTestState> = {};
|
|
|
|
|
|
|
|
export const itOnlyRegularInstall = (
|
|
|
|
description: string,
|
2022-09-08 10:32:39 +00:00
|
|
|
body: Mocha.AsyncFunc
|
2020-06-23 05:18:46 +00:00
|
|
|
): Mocha.Test => {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (alternativeInstall || process.env['BINARY']) {
|
2022-09-09 09:12:18 +00:00
|
|
|
return it.skip(description, body);
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
|
|
|
return it(description, body);
|
|
|
|
}
|
2020-06-12 13:55:51 +00:00
|
|
|
};
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
if (
|
|
|
|
process.env['MOCHA_WORKER_ID'] === undefined ||
|
|
|
|
process.env['MOCHA_WORKER_ID'] === '0'
|
|
|
|
) {
|
2022-07-05 12:42:55 +00:00
|
|
|
console.log(
|
|
|
|
`Running unit tests with:
|
2020-04-30 11:03:16 +00:00
|
|
|
-> product: ${product}
|
2020-06-12 13:55:51 +00:00
|
|
|
-> binary: ${
|
|
|
|
defaultBrowserOptions.executablePath ||
|
|
|
|
path.relative(process.cwd(), puppeteer.executablePath())
|
2022-07-21 18:50:46 +00:00
|
|
|
}
|
|
|
|
-> mode: ${
|
|
|
|
isHeadless
|
|
|
|
? headless === 'chrome'
|
|
|
|
? '--headless=chrome'
|
|
|
|
: '--headless'
|
|
|
|
: 'headful'
|
2020-06-12 13:55:51 +00:00
|
|
|
}`
|
2022-07-05 12:42:55 +00:00
|
|
|
);
|
|
|
|
}
|
2020-04-30 11:03:16 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
process.on('unhandledRejection', reason => {
|
2021-10-27 11:43:57 +00:00
|
|
|
throw reason;
|
|
|
|
});
|
|
|
|
|
2021-05-12 16:43:05 +00:00
|
|
|
export const setupTestBrowserHooks = (): void => {
|
2020-05-07 10:54:55 +00:00
|
|
|
before(async () => {
|
2020-04-09 05:56:25 +00:00
|
|
|
const browser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
state.browser = browser;
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
after(async () => {
|
2022-09-08 10:32:39 +00:00
|
|
|
await state.browser?.close();
|
2022-05-31 14:34:16 +00:00
|
|
|
state.browser = undefined;
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-05-12 16:43:05 +00:00
|
|
|
export const setupTestPageAndContextHooks = (): void => {
|
2020-05-07 10:54:55 +00:00
|
|
|
beforeEach(async () => {
|
2022-05-31 14:34:16 +00:00
|
|
|
state.context = await state.browser!.createIncognitoBrowserContext();
|
2020-04-09 05:56:25 +00:00
|
|
|
state.page = await state.context.newPage();
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
afterEach(async () => {
|
2022-09-08 10:32:39 +00:00
|
|
|
await state.context?.close();
|
2022-05-31 14:34:16 +00:00
|
|
|
state.context = undefined;
|
|
|
|
state.page = undefined;
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
export const mochaHooks = {
|
2020-06-18 15:26:30 +00:00
|
|
|
beforeAll: [
|
2021-05-12 16:43:05 +00:00
|
|
|
async (): Promise<void> => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {server, httpsServer} = await setupServer();
|
2020-06-18 15:26:30 +00:00
|
|
|
|
|
|
|
state.puppeteer = puppeteer;
|
|
|
|
state.defaultBrowserOptions = defaultBrowserOptions;
|
|
|
|
state.server = server;
|
|
|
|
state.httpsServer = httpsServer;
|
|
|
|
state.isFirefox = isFirefox;
|
|
|
|
state.isChrome = isChrome;
|
|
|
|
state.isHeadless = isHeadless;
|
2022-05-19 07:41:29 +00:00
|
|
|
state.headless = headless;
|
2022-06-15 10:05:25 +00:00
|
|
|
state.puppeteerPath = path.resolve(path.join(__dirname, '../..'));
|
2020-06-18 15:26:30 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
|
2021-05-12 16:43:05 +00:00
|
|
|
beforeEach: async (): Promise<void> => {
|
2022-06-15 10:09:22 +00:00
|
|
|
state.server!.reset();
|
|
|
|
state.httpsServer!.reset();
|
2020-06-18 15:26:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
afterAll: [
|
2021-05-12 16:43:05 +00:00
|
|
|
async (): Promise<void> => {
|
2022-06-15 10:09:22 +00:00
|
|
|
await state.server!.stop();
|
|
|
|
state.server = undefined;
|
|
|
|
await state.httpsServer!.stop();
|
|
|
|
state.httpsServer = undefined;
|
2020-06-18 15:26:30 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
|
2021-05-12 16:43:05 +00:00
|
|
|
afterEach: (): void => {
|
2020-06-18 15:26:30 +00:00
|
|
|
sinon.restore();
|
|
|
|
},
|
|
|
|
};
|
2021-03-17 16:09:10 +00:00
|
|
|
|
2021-05-12 16:43:05 +00:00
|
|
|
export const expectCookieEquals = (
|
|
|
|
cookies: Protocol.Network.Cookie[],
|
|
|
|
expectedCookies: Array<Partial<Protocol.Network.Cookie>>
|
|
|
|
): void => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {isChrome} = getTestState();
|
2021-03-17 16:09:10 +00:00
|
|
|
if (!isChrome) {
|
|
|
|
// Only keep standard properties when testing on a browser other than Chrome.
|
2022-06-22 13:25:44 +00:00
|
|
|
expectedCookies = expectedCookies.map(cookie => {
|
2021-03-17 16:09:10 +00:00
|
|
|
return {
|
|
|
|
domain: cookie.domain,
|
|
|
|
expires: cookie.expires,
|
|
|
|
httpOnly: cookie.httpOnly,
|
|
|
|
name: cookie.name,
|
|
|
|
path: cookie.path,
|
|
|
|
secure: cookie.secure,
|
|
|
|
session: cookie.session,
|
|
|
|
size: cookie.size,
|
|
|
|
value: cookie.value,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-05 12:42:55 +00:00
|
|
|
expect(cookies.length).toBe(expectedCookies.length);
|
|
|
|
for (let i = 0; i < cookies.length; i++) {
|
|
|
|
expect(cookies[i]).toMatchObject(expectedCookies[i]!);
|
|
|
|
}
|
2021-03-17 16:09:10 +00:00
|
|
|
};
|
2021-11-23 07:19:14 +00:00
|
|
|
|
|
|
|
export const shortWaitForArrayToHaveAtLeastNElements = async (
|
|
|
|
data: unknown[],
|
|
|
|
minLength: number,
|
|
|
|
attempts = 3,
|
|
|
|
timeout = 50
|
|
|
|
): Promise<void> => {
|
|
|
|
for (let i = 0; i < attempts; i++) {
|
|
|
|
if (data.length >= minLength) {
|
|
|
|
break;
|
|
|
|
}
|
2022-06-22 13:25:44 +00:00
|
|
|
await new Promise(resolve => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return setTimeout(resolve, timeout);
|
|
|
|
});
|
2021-11-23 07:19:14 +00:00
|
|
|
}
|
|
|
|
};
|
2022-09-15 07:25:20 +00:00
|
|
|
|
|
|
|
export const createTimeout = <T>(
|
|
|
|
n: number,
|
|
|
|
value?: T
|
|
|
|
): Promise<T | undefined> => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(() => {
|
|
|
|
return resolve(value);
|
|
|
|
}, n);
|
|
|
|
});
|
|
|
|
};
|