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.
|
|
|
|
*/
|
|
|
|
|
2020-07-13 09:22:26 +00:00
|
|
|
import { TestServer } from '../utils/testserver/index.js';
|
2020-06-23 05:18:46 +00:00
|
|
|
import * as path from 'path';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as os from 'os';
|
|
|
|
import sinon from 'sinon';
|
2020-10-13 15:19:26 +00:00
|
|
|
import puppeteer from '../lib/cjs/puppeteer/node.js';
|
2020-07-14 15:57:29 +00:00
|
|
|
import {
|
|
|
|
Browser,
|
|
|
|
BrowserContext,
|
|
|
|
} from '../lib/cjs/puppeteer/common/Browser.js';
|
|
|
|
import { Page } from '../lib/cjs/puppeteer/common/Page.js';
|
2020-10-13 15:19:26 +00:00
|
|
|
import { PuppeteerNode } from '../lib/cjs/puppeteer/node/Puppeteer.js';
|
2020-07-13 09:22:26 +00:00
|
|
|
import utils from './utils.js';
|
2020-06-23 05:18:46 +00:00
|
|
|
import rimraf from 'rimraf';
|
|
|
|
|
2020-07-13 09:22:26 +00:00
|
|
|
import { trackCoverage } from './coverage-utils.js';
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const setupServer = async () => {
|
2020-04-09 05:56:25 +00:00
|
|
|
const assetsPath = path.join(__dirname, 'assets');
|
|
|
|
const cachedPath = path.join(__dirname, 'assets', 'cached');
|
|
|
|
|
|
|
|
const port = 8907;
|
|
|
|
const server = await TestServer.create(assetsPath, port);
|
|
|
|
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`;
|
|
|
|
|
|
|
|
const httpsPort = port + 1;
|
|
|
|
const httpsServer = await TestServer.createHTTPS(assetsPath, httpsPort);
|
|
|
|
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`;
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
return { server, httpsServer };
|
2020-04-09 05:56:25 +00:00
|
|
|
};
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
export const getTestState = (): PuppeteerTestState =>
|
|
|
|
state as PuppeteerTestState;
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const product =
|
|
|
|
process.env.PRODUCT || process.env.PUPPETEER_PRODUCT || 'Chromium';
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
const alternativeInstall = process.env.PUPPETEER_ALT_INSTALL || false;
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const isHeadless =
|
|
|
|
(process.env.HEADLESS || 'true').trim().toLowerCase() === 'true';
|
2020-04-09 05:56:25 +00:00
|
|
|
const isFirefox = product === 'firefox';
|
|
|
|
const isChrome = product === 'Chromium';
|
2020-06-12 13:55:51 +00:00
|
|
|
|
|
|
|
let extraLaunchOptions = {};
|
|
|
|
try {
|
|
|
|
extraLaunchOptions = JSON.parse(process.env.EXTRA_LAUNCH_OPTIONS || '{}');
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(
|
|
|
|
`Error parsing EXTRA_LAUNCH_OPTIONS: ${error.message}. Skipping.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultBrowserOptions = Object.assign(
|
|
|
|
{
|
2020-06-15 13:02:00 +00:00
|
|
|
handleSIGINT: true,
|
2020-06-12 13:55:51 +00:00
|
|
|
executablePath: process.env.BINARY,
|
|
|
|
headless: isHeadless,
|
|
|
|
dumpio: !!process.env.DUMPIO,
|
|
|
|
},
|
|
|
|
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.
|
|
|
|
// @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.
|
2020-05-07 10:54:55 +00:00
|
|
|
if (product === 'firefox') await puppeteer._launcher._updateRevision();
|
2020-04-15 11:30:42 +00:00
|
|
|
const executablePath = puppeteer.executablePath();
|
|
|
|
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`
|
|
|
|
);
|
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 => {
|
2020-04-09 05:56:25 +00:00
|
|
|
const suffix = product.toLowerCase();
|
|
|
|
const GOLDEN_DIR = path.join(__dirname, 'golden-' + suffix);
|
|
|
|
const OUTPUT_DIR = path.join(__dirname, 'output-' + suffix);
|
2020-05-26 14:37:39 +00:00
|
|
|
if (fs.existsSync(OUTPUT_DIR)) rimraf.sync(OUTPUT_DIR);
|
2020-04-09 05:56:25 +00:00
|
|
|
utils.extendExpectWithToBeGolden(GOLDEN_DIR, OUTPUT_DIR);
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2020-06-23 05:18:46 +00:00
|
|
|
defaultBrowserOptions: {
|
|
|
|
[x: string]: any;
|
|
|
|
};
|
|
|
|
server: any;
|
|
|
|
httpsServer: any;
|
|
|
|
isFirefox: boolean;
|
|
|
|
isChrome: boolean;
|
|
|
|
isHeadless: boolean;
|
|
|
|
puppeteerPath: string;
|
|
|
|
}
|
|
|
|
const state: Partial<PuppeteerTestState> = {};
|
|
|
|
|
|
|
|
export const itFailsFirefox = (
|
|
|
|
description: string,
|
|
|
|
body: Mocha.Func
|
|
|
|
): Mocha.Test => {
|
|
|
|
if (isFirefox) return xit(description, body);
|
|
|
|
else return it(description, body);
|
2020-04-09 05:56:25 +00:00
|
|
|
};
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
export const itChromeOnly = (
|
|
|
|
description: string,
|
|
|
|
body: Mocha.Func
|
|
|
|
): Mocha.Test => {
|
|
|
|
if (isChrome) return it(description, body);
|
|
|
|
else return xit(description, body);
|
2020-06-12 13:55:51 +00:00
|
|
|
};
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
export const itOnlyRegularInstall = (
|
|
|
|
description: string,
|
|
|
|
body: Mocha.Func
|
|
|
|
): Mocha.Test => {
|
|
|
|
if (alternativeInstall || process.env.BINARY) return xit(description, body);
|
|
|
|
else return it(description, body);
|
2020-06-12 13:55:51 +00:00
|
|
|
};
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
export const itFailsWindowsUntilDate = (
|
|
|
|
date: Date,
|
|
|
|
description: string,
|
|
|
|
body: Mocha.Func
|
|
|
|
): Mocha.Test => {
|
|
|
|
if (os.platform() === 'win32' && Date.now() < date.getTime()) {
|
2020-04-17 13:27:50 +00:00
|
|
|
// we are within the deferred time so skip the test
|
2020-06-23 05:18:46 +00:00
|
|
|
return xit(description, body);
|
2020-04-17 13:27:50 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
return it(description, body);
|
2020-04-17 13:27:50 +00:00
|
|
|
};
|
|
|
|
|
2020-07-01 10:28:13 +00:00
|
|
|
export const itFailsWindows = (description: string, body: Mocha.Func) => {
|
|
|
|
if (os.platform() === 'win32') {
|
|
|
|
return xit(description, body);
|
|
|
|
}
|
|
|
|
return it(description, body);
|
|
|
|
};
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
export const describeFailsFirefox = (
|
|
|
|
description: string,
|
|
|
|
body: (this: Mocha.Suite) => void
|
|
|
|
): void | Mocha.Suite => {
|
|
|
|
if (isFirefox) return xdescribe(description, body);
|
|
|
|
else return describe(description, body);
|
2020-04-09 05:56:25 +00:00
|
|
|
};
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
export const describeChromeOnly = (
|
|
|
|
description: string,
|
|
|
|
body: (this: Mocha.Suite) => void
|
|
|
|
): Mocha.Suite => {
|
|
|
|
if (isChrome) return describe(description, body);
|
2020-04-09 05:56:25 +00:00
|
|
|
};
|
|
|
|
|
2020-06-18 15:26:30 +00:00
|
|
|
let coverageHooks = {
|
2020-06-23 05:18:46 +00:00
|
|
|
beforeAll: (): void => {},
|
|
|
|
afterAll: (): void => {},
|
2020-06-18 15:26:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (process.env.COVERAGE) {
|
|
|
|
coverageHooks = trackCoverage();
|
|
|
|
}
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-04-30 11:03:16 +00:00
|
|
|
console.log(
|
2020-05-07 10:54:55 +00:00
|
|
|
`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())
|
|
|
|
}`
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2020-04-30 11:03:16 +00:00
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
export const setupTestBrowserHooks = () => {
|
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 () => {
|
2020-04-09 05:56:25 +00:00
|
|
|
await state.browser.close();
|
|
|
|
state.browser = null;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
export const setupTestPageAndContextHooks = () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
beforeEach(async () => {
|
2020-04-09 05:56:25 +00:00
|
|
|
state.context = await state.browser.createIncognitoBrowserContext();
|
|
|
|
state.page = await state.context.newPage();
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
afterEach(async () => {
|
2020-04-09 05:56:25 +00:00
|
|
|
await state.context.close();
|
|
|
|
state.context = null;
|
|
|
|
state.page = null;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
export const mochaHooks = {
|
2020-06-18 15:26:30 +00:00
|
|
|
beforeAll: [
|
|
|
|
async () => {
|
|
|
|
const { server, httpsServer } = await setupServer();
|
|
|
|
|
|
|
|
state.puppeteer = puppeteer;
|
|
|
|
state.defaultBrowserOptions = defaultBrowserOptions;
|
|
|
|
state.server = server;
|
|
|
|
state.httpsServer = httpsServer;
|
|
|
|
state.isFirefox = isFirefox;
|
|
|
|
state.isChrome = isChrome;
|
|
|
|
state.isHeadless = isHeadless;
|
|
|
|
state.puppeteerPath = path.resolve(path.join(__dirname, '..'));
|
|
|
|
},
|
|
|
|
coverageHooks.beforeAll,
|
|
|
|
],
|
|
|
|
|
|
|
|
beforeEach: async () => {
|
|
|
|
state.server.reset();
|
|
|
|
state.httpsServer.reset();
|
|
|
|
},
|
|
|
|
|
|
|
|
afterAll: [
|
|
|
|
async () => {
|
|
|
|
await state.server.stop();
|
|
|
|
state.server = null;
|
|
|
|
await state.httpsServer.stop();
|
|
|
|
state.httpsServer = null;
|
|
|
|
},
|
|
|
|
coverageHooks.afterAll,
|
|
|
|
],
|
|
|
|
|
|
|
|
afterEach: () => {
|
|
|
|
sinon.restore();
|
|
|
|
},
|
|
|
|
};
|