mirror of
https://github.com/microsoft/playwright.git
synced 2024-12-14 13:45:36 +03:00
chore: reuse processLauncher between browsers (#231)
Drive-by: improve test runner to always exit and kill browsers
This commit is contained in:
parent
39fa313535
commit
d378a8d3fc
@ -14,14 +14,12 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
import * as childProcess from 'child_process';
|
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as http from 'http';
|
import * as http from 'http';
|
||||||
import * as https from 'https';
|
import * as https from 'https';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as readline from 'readline';
|
|
||||||
import * as removeFolder from 'rimraf';
|
|
||||||
import * as URL from 'url';
|
import * as URL from 'url';
|
||||||
import { Browser } from './Browser';
|
import { Browser } from './Browser';
|
||||||
import { BrowserFetcher, BrowserFetcherOptions } from '../browserFetcher';
|
import { BrowserFetcher, BrowserFetcherOptions } from '../browserFetcher';
|
||||||
@ -33,9 +31,9 @@ import { PipeTransport } from './PipeTransport';
|
|||||||
import { WebSocketTransport } from './WebSocketTransport';
|
import { WebSocketTransport } from './WebSocketTransport';
|
||||||
import { ConnectionTransport } from '../types';
|
import { ConnectionTransport } from '../types';
|
||||||
import * as util from 'util';
|
import * as util from 'util';
|
||||||
|
import { launchProcess, waitForLine } from '../processLauncher';
|
||||||
|
|
||||||
const mkdtempAsync = helper.promisify(fs.mkdtemp);
|
const mkdtempAsync = helper.promisify(fs.mkdtemp);
|
||||||
const removeFolderAsync = helper.promisify(removeFolder);
|
|
||||||
|
|
||||||
const CHROME_PROFILE_PATH = path.join(os.tmpdir(), 'playwright_dev_profile-');
|
const CHROME_PROFILE_PATH = path.join(os.tmpdir(), 'playwright_dev_profile-');
|
||||||
|
|
||||||
@ -118,113 +116,45 @@ export class Launcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const usePipe = chromeArguments.includes('--remote-debugging-pipe');
|
const usePipe = chromeArguments.includes('--remote-debugging-pipe');
|
||||||
let stdio: ('ignore' | 'pipe')[] = ['pipe', 'pipe', 'pipe'];
|
|
||||||
if (usePipe) {
|
|
||||||
if (dumpio)
|
|
||||||
stdio = ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'];
|
|
||||||
else
|
|
||||||
stdio = ['ignore', 'ignore', 'ignore', 'pipe', 'pipe'];
|
|
||||||
}
|
|
||||||
const chromeProcess = childProcess.spawn(
|
|
||||||
chromeExecutable,
|
|
||||||
chromeArguments,
|
|
||||||
{
|
|
||||||
// On non-windows platforms, `detached: true` makes child process a leader of a new
|
|
||||||
// process group, making it possible to kill child process tree with `.kill(-pid)` command.
|
|
||||||
// @see https://nodejs.org/api/child_process.html#child_process_options_detached
|
|
||||||
detached: process.platform !== 'win32',
|
|
||||||
env,
|
|
||||||
stdio
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!chromeProcess.pid) {
|
const launched = await launchProcess({
|
||||||
let reject: (e: Error) => void;
|
executablePath: chromeExecutable,
|
||||||
const result = new Promise((f, r) => reject = r);
|
args: chromeArguments,
|
||||||
chromeProcess.once('error', error => {
|
env,
|
||||||
reject(new Error('Failed to launch browser: ' + error));
|
handleSIGINT,
|
||||||
});
|
handleSIGTERM,
|
||||||
return result as Promise<Browser>;
|
handleSIGHUP,
|
||||||
}
|
dumpio,
|
||||||
|
pipe: usePipe,
|
||||||
if (dumpio) {
|
tempDir: temporaryUserDataDir
|
||||||
chromeProcess.stderr.pipe(process.stderr);
|
}, () => {
|
||||||
chromeProcess.stdout.pipe(process.stdout);
|
if (temporaryUserDataDir || !connection)
|
||||||
}
|
return Promise.reject();
|
||||||
|
return connection.rootSession.send('Browser.close').catch(error => {
|
||||||
let chromeClosed = false;
|
debugError(error);
|
||||||
const waitForChromeToClose = new Promise((fulfill, reject) => {
|
throw error;
|
||||||
chromeProcess.once('exit', () => {
|
|
||||||
chromeClosed = true;
|
|
||||||
// Cleanup as processes exit.
|
|
||||||
if (temporaryUserDataDir) {
|
|
||||||
removeFolderAsync(temporaryUserDataDir)
|
|
||||||
.then(() => fulfill())
|
|
||||||
.catch((err: Error) => console.error(err));
|
|
||||||
} else {
|
|
||||||
fulfill();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const listeners = [ helper.addEventListener(process, 'exit', killChrome) ];
|
|
||||||
if (handleSIGINT)
|
|
||||||
listeners.push(helper.addEventListener(process, 'SIGINT', () => { killChrome(); process.exit(130); }));
|
|
||||||
if (handleSIGTERM)
|
|
||||||
listeners.push(helper.addEventListener(process, 'SIGTERM', gracefullyCloseChrome));
|
|
||||||
if (handleSIGHUP)
|
|
||||||
listeners.push(helper.addEventListener(process, 'SIGHUP', gracefullyCloseChrome));
|
|
||||||
let connection: Connection | null = null;
|
let connection: Connection | null = null;
|
||||||
try {
|
try {
|
||||||
if (!usePipe) {
|
if (!usePipe) {
|
||||||
const browserWSEndpoint = await waitForWSEndpoint(chromeProcess, timeout, this._preferredRevision);
|
const timeoutError = new TimeoutError(`Timed out after ${timeout} ms while trying to connect to Chrome! The only Chrome revision guaranteed to work is r${this._preferredRevision}`);
|
||||||
|
const match = await waitForLine(launched.process, launched.process.stderr, /^DevTools listening on (ws:\/\/.*)$/, timeout, timeoutError);
|
||||||
|
const browserWSEndpoint = match[1];
|
||||||
const transport = await WebSocketTransport.create(browserWSEndpoint);
|
const transport = await WebSocketTransport.create(browserWSEndpoint);
|
||||||
connection = new Connection(browserWSEndpoint, transport, slowMo);
|
connection = new Connection(browserWSEndpoint, transport, slowMo);
|
||||||
} else {
|
} else {
|
||||||
const transport = new PipeTransport(chromeProcess.stdio[3] as NodeJS.WritableStream, chromeProcess.stdio[4] as NodeJS.ReadableStream);
|
const transport = new PipeTransport(launched.process.stdio[3] as NodeJS.WritableStream, launched.process.stdio[4] as NodeJS.ReadableStream);
|
||||||
connection = new Connection('', transport, slowMo);
|
connection = new Connection('', transport, slowMo);
|
||||||
}
|
}
|
||||||
const browser = await Browser.create(connection, [], ignoreHTTPSErrors, defaultViewport, chromeProcess, gracefullyCloseChrome);
|
const browser = await Browser.create(connection, [], ignoreHTTPSErrors, defaultViewport, launched.process, launched.gracefullyClose);
|
||||||
await browser._waitForTarget(t => t.type() === 'page');
|
await browser._waitForTarget(t => t.type() === 'page');
|
||||||
return browser;
|
return browser;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
killChrome();
|
await launched.gracefullyClose();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
function gracefullyCloseChrome(): Promise<any> {
|
|
||||||
helper.removeEventListeners(listeners);
|
|
||||||
if (temporaryUserDataDir) {
|
|
||||||
killChrome();
|
|
||||||
} else if (connection) {
|
|
||||||
// Attempt to close chrome gracefully
|
|
||||||
connection.rootSession.send('Browser.close').catch(error => {
|
|
||||||
debugError(error);
|
|
||||||
killChrome();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return waitForChromeToClose;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This method has to be sync to be used as 'exit' event handler.
|
|
||||||
function killChrome() {
|
|
||||||
helper.removeEventListeners(listeners);
|
|
||||||
if (chromeProcess.pid && !chromeProcess.killed && !chromeClosed) {
|
|
||||||
// Force kill chrome.
|
|
||||||
try {
|
|
||||||
if (process.platform === 'win32')
|
|
||||||
childProcess.execSync(`taskkill /pid ${chromeProcess.pid} /T /F`);
|
|
||||||
else
|
|
||||||
process.kill(-chromeProcess.pid, 'SIGKILL');
|
|
||||||
} catch (e) {
|
|
||||||
// the process might have already stopped
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Attempt to remove temporary profile directory to avoid littering.
|
|
||||||
try {
|
|
||||||
removeFolder.sync(temporaryUserDataDir);
|
|
||||||
} catch (e) { }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultArgs(options: LauncherChromeArgOptions = {}): string[] {
|
defaultArgs(options: LauncherChromeArgOptions = {}): string[] {
|
||||||
@ -298,51 +228,6 @@ export class Launcher {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function waitForWSEndpoint(chromeProcess: childProcess.ChildProcess, timeout: number, preferredRevision: string): Promise<string> {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const rl = readline.createInterface({ input: chromeProcess.stderr });
|
|
||||||
let stderr = '';
|
|
||||||
const listeners = [
|
|
||||||
helper.addEventListener(rl, 'line', onLine),
|
|
||||||
helper.addEventListener(rl, 'close', () => onClose()),
|
|
||||||
helper.addEventListener(chromeProcess, 'exit', () => onClose()),
|
|
||||||
helper.addEventListener(chromeProcess, 'error', error => onClose(error))
|
|
||||||
];
|
|
||||||
const timeoutId = timeout ? setTimeout(onTimeout, timeout) : 0;
|
|
||||||
|
|
||||||
function onClose(error?: Error) {
|
|
||||||
cleanup();
|
|
||||||
reject(new Error([
|
|
||||||
'Failed to launch chrome!' + (error ? ' ' + error.message : ''),
|
|
||||||
stderr,
|
|
||||||
'',
|
|
||||||
'TROUBLESHOOTING: https://github.com/Microsoft/playwright/blob/master/docs/troubleshooting.md',
|
|
||||||
'',
|
|
||||||
].join('\n')));
|
|
||||||
}
|
|
||||||
|
|
||||||
function onTimeout() {
|
|
||||||
cleanup();
|
|
||||||
reject(new TimeoutError(`Timed out after ${timeout} ms while trying to connect to Chrome! The only Chrome revision guaranteed to work is r${preferredRevision}`));
|
|
||||||
}
|
|
||||||
|
|
||||||
function onLine(line: string) {
|
|
||||||
stderr += line + '\n';
|
|
||||||
const match = line.match(/^DevTools listening on (ws:\/\/.*)$/);
|
|
||||||
if (!match)
|
|
||||||
return;
|
|
||||||
cleanup();
|
|
||||||
resolve(match[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
function cleanup() {
|
|
||||||
if (timeoutId)
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
helper.removeEventListeners(listeners);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getWSEndpoint(browserURL: string): Promise<string> {
|
function getWSEndpoint(browserURL: string): Promise<string> {
|
||||||
let resolve: (url: string) => void;
|
let resolve: (url: string) => void;
|
||||||
let reject: (e: Error) => void;
|
let reject: (e: Error) => void;
|
||||||
|
@ -31,20 +31,20 @@ export class Browser extends EventEmitter implements BrowserInterface {
|
|||||||
private _connection: Connection;
|
private _connection: Connection;
|
||||||
_defaultViewport: types.Viewport;
|
_defaultViewport: types.Viewport;
|
||||||
private _process: import('child_process').ChildProcess;
|
private _process: import('child_process').ChildProcess;
|
||||||
private _closeCallback: () => void;
|
private _closeCallback: () => Promise<void>;
|
||||||
_targets: Map<string, Target>;
|
_targets: Map<string, Target>;
|
||||||
private _defaultContext: BrowserContext;
|
private _defaultContext: BrowserContext;
|
||||||
private _contexts: Map<string, BrowserContext>;
|
private _contexts: Map<string, BrowserContext>;
|
||||||
private _eventListeners: RegisteredListener[];
|
private _eventListeners: RegisteredListener[];
|
||||||
|
|
||||||
static async create(connection: Connection, defaultViewport: types.Viewport | null, process: import('child_process').ChildProcess | null, closeCallback: () => void) {
|
static async create(connection: Connection, defaultViewport: types.Viewport | null, process: import('child_process').ChildProcess | null, closeCallback: () => Promise<void>) {
|
||||||
const {browserContextIds} = await connection.send('Target.getBrowserContexts');
|
const {browserContextIds} = await connection.send('Target.getBrowserContexts');
|
||||||
const browser = new Browser(connection, browserContextIds, defaultViewport, process, closeCallback);
|
const browser = new Browser(connection, browserContextIds, defaultViewport, process, closeCallback);
|
||||||
await connection.send('Target.enable');
|
await connection.send('Target.enable');
|
||||||
return browser;
|
return browser;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(connection: Connection, browserContextIds: Array<string>, defaultViewport: types.Viewport | null, process: import('child_process').ChildProcess | null, closeCallback: () => void) {
|
constructor(connection: Connection, browserContextIds: Array<string>, defaultViewport: types.Viewport | null, process: import('child_process').ChildProcess | null, closeCallback: () => Promise<void>) {
|
||||||
super();
|
super();
|
||||||
this._connection = connection;
|
this._connection = connection;
|
||||||
this._defaultViewport = defaultViewport;
|
this._defaultViewport = defaultViewport;
|
||||||
@ -167,7 +167,7 @@ export class Browser extends EventEmitter implements BrowserInterface {
|
|||||||
|
|
||||||
async close() {
|
async close() {
|
||||||
helper.removeEventListeners(this._eventListeners);
|
helper.removeEventListeners(this._eventListeners);
|
||||||
this._closeCallback();
|
await this._closeCallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
_createBrowserContext(browserContextId: string | null): BrowserContext {
|
_createBrowserContext(browserContextId: string | null): BrowserContext {
|
||||||
|
@ -14,22 +14,20 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as removeFolder from 'rimraf';
|
|
||||||
import * as childProcess from 'child_process';
|
|
||||||
import {Connection} from './Connection';
|
import {Connection} from './Connection';
|
||||||
import {Browser} from './Browser';
|
import {Browser} from './Browser';
|
||||||
import {BrowserFetcher, BrowserFetcherOptions} from '../browserFetcher';
|
import {BrowserFetcher, BrowserFetcherOptions} from '../browserFetcher';
|
||||||
import * as readline from 'readline';
|
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as util from 'util';
|
import * as util from 'util';
|
||||||
import {helper, debugError, assert} from '../helper';
|
import {debugError, assert} from '../helper';
|
||||||
import {TimeoutError} from '../Errors';
|
import {TimeoutError} from '../Errors';
|
||||||
import {WebSocketTransport} from './WebSocketTransport';
|
import {WebSocketTransport} from './WebSocketTransport';
|
||||||
|
import { launchProcess, waitForLine } from '../processLauncher';
|
||||||
|
|
||||||
const mkdtempAsync = util.promisify(fs.mkdtemp);
|
const mkdtempAsync = util.promisify(fs.mkdtemp);
|
||||||
const removeFolderAsync = util.promisify(removeFolder);
|
|
||||||
|
|
||||||
const FIREFOX_PROFILE_PATH = path.join(os.tmpdir(), 'playwright_firefox_profile-');
|
const FIREFOX_PROFILE_PATH = path.join(os.tmpdir(), 'playwright_firefox_profile-');
|
||||||
|
|
||||||
@ -103,107 +101,45 @@ export class Launcher {
|
|||||||
throw new Error(missingText);
|
throw new Error(missingText);
|
||||||
firefoxExecutable = executablePath;
|
firefoxExecutable = executablePath;
|
||||||
}
|
}
|
||||||
const stdio = ['pipe', 'pipe', 'pipe'];
|
const launched = await launchProcess({
|
||||||
const firefoxProcess = childProcess.spawn(
|
executablePath: firefoxExecutable,
|
||||||
firefoxExecutable,
|
args: firefoxArguments,
|
||||||
firefoxArguments,
|
env: os.platform() === 'linux' ? {
|
||||||
{
|
...env,
|
||||||
// On non-windows platforms, `detached: false` makes child process a leader of a new
|
// On linux Juggler ships the libstdc++ it was linked against.
|
||||||
// process group, making it possible to kill child process tree with `.kill(-pid)` command.
|
LD_LIBRARY_PATH: `${path.dirname(firefoxExecutable)}:${process.env.LD_LIBRARY_PATH}`,
|
||||||
// @see https://nodejs.org/api/child_process.html#child_process_options_detached
|
} : env,
|
||||||
detached: process.platform !== 'win32',
|
handleSIGINT,
|
||||||
stdio,
|
handleSIGTERM,
|
||||||
// On linux Juggler ships the libstdc++ it was linked against.
|
handleSIGHUP,
|
||||||
env: os.platform() === 'linux' ? {
|
dumpio,
|
||||||
...env,
|
pipe: false,
|
||||||
LD_LIBRARY_PATH: `${path.dirname(firefoxExecutable)}:${process.env.LD_LIBRARY_PATH}`,
|
tempDir: temporaryProfileDir
|
||||||
} : env,
|
}, () => {
|
||||||
}
|
if (temporaryProfileDir || !connection)
|
||||||
);
|
return Promise.reject();
|
||||||
|
return connection.send('Browser.close').catch(error => {
|
||||||
if (!firefoxProcess.pid) {
|
debugError(error);
|
||||||
let reject;
|
throw error;
|
||||||
const result = new Promise((f, r) => reject = r);
|
|
||||||
firefoxProcess.once('error', error => {
|
|
||||||
reject(new Error('Failed to launch browser: ' + error));
|
|
||||||
});
|
|
||||||
return result as Promise<Browser>;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dumpio) {
|
|
||||||
firefoxProcess.stderr.pipe(process.stderr);
|
|
||||||
firefoxProcess.stdout.pipe(process.stdout);
|
|
||||||
}
|
|
||||||
|
|
||||||
let firefoxClosed = false;
|
|
||||||
const waitForFirefoxToClose = new Promise((fulfill, reject) => {
|
|
||||||
firefoxProcess.once('close', () => {
|
|
||||||
firefoxClosed = true;
|
|
||||||
// Cleanup as processes exit.
|
|
||||||
if (temporaryProfileDir) {
|
|
||||||
removeFolderAsync(temporaryProfileDir)
|
|
||||||
.then(() => fulfill())
|
|
||||||
.catch(err => console.error(err));
|
|
||||||
} else {
|
|
||||||
fulfill();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const listeners = [ helper.addEventListener(process, 'close', killFirefox) ];
|
|
||||||
if (handleSIGINT)
|
|
||||||
listeners.push(helper.addEventListener(process, 'SIGINT', () => { killFirefox(); process.exit(130); }));
|
|
||||||
if (handleSIGTERM)
|
|
||||||
listeners.push(helper.addEventListener(process, 'SIGTERM', gracefullyCloseFirefox));
|
|
||||||
if (handleSIGHUP)
|
|
||||||
listeners.push(helper.addEventListener(process, 'SIGHUP', gracefullyCloseFirefox));
|
|
||||||
let connection: Connection | null = null;
|
let connection: Connection | null = null;
|
||||||
try {
|
try {
|
||||||
const url = await waitForWSEndpoint(firefoxProcess, timeout);
|
const timeoutError = new TimeoutError(`Timed out after ${timeout} ms while trying to connect to Firefox!`);
|
||||||
|
const match = await waitForLine(launched.process, launched.process.stdout, /^Juggler listening on (ws:\/\/.*)$/, timeout, timeoutError);
|
||||||
|
const url = match[1];
|
||||||
const transport = await WebSocketTransport.create(url);
|
const transport = await WebSocketTransport.create(url);
|
||||||
connection = new Connection(url, transport, slowMo);
|
connection = new Connection(url, transport, slowMo);
|
||||||
const browser = await Browser.create(connection, defaultViewport, firefoxProcess, gracefullyCloseFirefox);
|
const browser = await Browser.create(connection, defaultViewport, launched.process, launched.gracefullyClose);
|
||||||
if (ignoreHTTPSErrors)
|
if (ignoreHTTPSErrors)
|
||||||
await connection.send('Browser.setIgnoreHTTPSErrors', {enabled: true});
|
await connection.send('Browser.setIgnoreHTTPSErrors', {enabled: true});
|
||||||
await browser._waitForTarget(t => t.type() === 'page');
|
await browser._waitForTarget(t => t.type() === 'page');
|
||||||
return browser;
|
return browser;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
killFirefox();
|
await launched.gracefullyClose;
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
function gracefullyCloseFirefox() {
|
|
||||||
helper.removeEventListeners(listeners);
|
|
||||||
if (temporaryProfileDir) {
|
|
||||||
killFirefox();
|
|
||||||
} else if (connection) {
|
|
||||||
connection.send('Browser.close').catch(error => {
|
|
||||||
debugError(error);
|
|
||||||
killFirefox();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return waitForFirefoxToClose;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This method has to be sync to be used as 'exit' event handler.
|
|
||||||
function killFirefox() {
|
|
||||||
helper.removeEventListeners(listeners);
|
|
||||||
if (firefoxProcess.pid && !firefoxProcess.killed && !firefoxClosed) {
|
|
||||||
// Force kill chrome.
|
|
||||||
try {
|
|
||||||
if (process.platform === 'win32')
|
|
||||||
childProcess.execSync(`taskkill /pid ${firefoxProcess.pid} /T /F`);
|
|
||||||
else
|
|
||||||
process.kill(-firefoxProcess.pid, 'SIGKILL');
|
|
||||||
} catch (e) {
|
|
||||||
// the process might have already stopped
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Attempt to remove temporary profile directory to avoid littering.
|
|
||||||
try {
|
|
||||||
removeFolder.sync(temporaryProfileDir);
|
|
||||||
} catch (e) { }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async connect(options: any = {}): Promise<Browser> {
|
async connect(options: any = {}): Promise<Browser> {
|
||||||
@ -234,48 +170,6 @@ export class Launcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function waitForWSEndpoint(firefoxProcess: import('child_process').ChildProcess, timeout: number): Promise<string> {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const rl = readline.createInterface({ input: firefoxProcess.stdout });
|
|
||||||
let stderr = '';
|
|
||||||
const listeners = [
|
|
||||||
helper.addEventListener(rl, 'line', onLine),
|
|
||||||
helper.addEventListener(rl, 'close', () => onClose()),
|
|
||||||
helper.addEventListener(firefoxProcess, 'error', error => onClose(error))
|
|
||||||
];
|
|
||||||
const timeoutId = timeout ? setTimeout(onTimeout, timeout) : 0;
|
|
||||||
|
|
||||||
function onClose(error?: Error) {
|
|
||||||
cleanup();
|
|
||||||
reject(new Error([
|
|
||||||
'Failed to launch Firefox!' + (error ? ' ' + error.message : ''),
|
|
||||||
stderr,
|
|
||||||
'',
|
|
||||||
].join('\n')));
|
|
||||||
}
|
|
||||||
|
|
||||||
function onTimeout() {
|
|
||||||
cleanup();
|
|
||||||
reject(new TimeoutError(`Timed out after ${timeout} ms while trying to connect to Firefox!`));
|
|
||||||
}
|
|
||||||
|
|
||||||
function onLine(line: string) {
|
|
||||||
stderr += line + '\n';
|
|
||||||
const match = line.match(/^Juggler listening on (ws:\/\/.*)$/);
|
|
||||||
if (!match)
|
|
||||||
return;
|
|
||||||
cleanup();
|
|
||||||
resolve(match[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
function cleanup() {
|
|
||||||
if (timeoutId)
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
helper.removeEventListeners(listeners);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createBrowserFetcher(projectRoot: string, options: BrowserFetcherOptions = {}): BrowserFetcher {
|
export function createBrowserFetcher(projectRoot: string, options: BrowserFetcherOptions = {}): BrowserFetcher {
|
||||||
const downloadURLs = {
|
const downloadURLs = {
|
||||||
linux: '%s/builds/firefox/%s/firefox-linux.zip',
|
linux: '%s/builds/firefox/%s/firefox-linux.zip',
|
||||||
|
@ -14,8 +14,8 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
import * as childProcess from 'child_process';
|
|
||||||
import { debugError, helper, assert } from '../helper';
|
import { debugError, assert } from '../helper';
|
||||||
import { Browser } from './Browser';
|
import { Browser } from './Browser';
|
||||||
import { BrowserFetcher, BrowserFetcherOptions } from '../browserFetcher';
|
import { BrowserFetcher, BrowserFetcherOptions } from '../browserFetcher';
|
||||||
import { Connection } from './Connection';
|
import { Connection } from './Connection';
|
||||||
@ -25,6 +25,7 @@ import { execSync } from 'child_process';
|
|||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as util from 'util';
|
import * as util from 'util';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
|
import { launchProcess } from '../processLauncher';
|
||||||
|
|
||||||
const DEFAULT_ARGS = [
|
const DEFAULT_ARGS = [
|
||||||
];
|
];
|
||||||
@ -74,97 +75,41 @@ export class Launcher {
|
|||||||
throw new Error(missingText);
|
throw new Error(missingText);
|
||||||
webkitExecutable = executablePath;
|
webkitExecutable = executablePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
let stdio: ('ignore' | 'pipe')[] = ['pipe', 'pipe', 'pipe'];
|
|
||||||
if (dumpio)
|
|
||||||
stdio = ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'];
|
|
||||||
else
|
|
||||||
stdio = ['ignore', 'ignore', 'ignore', 'pipe', 'pipe'];
|
|
||||||
webkitArguments.push('--inspector-pipe');
|
webkitArguments.push('--inspector-pipe');
|
||||||
// Headless options is only implemented on Mac at the moment.
|
// Headless options is only implemented on Mac at the moment.
|
||||||
if (process.platform === 'darwin' && options.headless !== false)
|
if (process.platform === 'darwin' && options.headless !== false)
|
||||||
webkitArguments.push('--headless');
|
webkitArguments.push('--headless');
|
||||||
const webkitProcess = childProcess.spawn(
|
|
||||||
webkitExecutable,
|
|
||||||
webkitArguments,
|
|
||||||
{
|
|
||||||
// On non-windows platforms, `detached: true` makes child process a leader of a new
|
|
||||||
// process group, making it possible to kill child process tree with `.kill(-pid)` command.
|
|
||||||
// @see https://nodejs.org/api/child_process.html#child_process_options_detached
|
|
||||||
detached: process.platform !== 'win32',
|
|
||||||
env,
|
|
||||||
stdio
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!webkitProcess.pid) {
|
const launched = await launchProcess({
|
||||||
let reject;
|
executablePath: webkitExecutable,
|
||||||
const result = new Promise((f, r) => reject = r);
|
args: webkitArguments,
|
||||||
webkitProcess.once('error', error => {
|
env,
|
||||||
reject(new Error('Failed to launch browser: ' + error));
|
handleSIGINT,
|
||||||
});
|
handleSIGTERM,
|
||||||
return result as Promise<Browser>;
|
handleSIGHUP,
|
||||||
}
|
dumpio,
|
||||||
|
pipe: true,
|
||||||
if (dumpio) {
|
tempDir: null
|
||||||
webkitProcess.stderr.pipe(process.stderr);
|
}, () => {
|
||||||
webkitProcess.stdout.pipe(process.stdout);
|
if (!connection)
|
||||||
}
|
return Promise.reject();
|
||||||
|
return connection.send('Browser.close').catch(error => {
|
||||||
let webkitClosed = false;
|
debugError(error);
|
||||||
const waitForChromeToClose = new Promise((fulfill, reject) => {
|
throw error;
|
||||||
webkitProcess.once('exit', () => {
|
|
||||||
webkitClosed = true;
|
|
||||||
fulfill();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const listeners = [ helper.addEventListener(process, 'exit', killWebKit) ];
|
|
||||||
if (handleSIGINT)
|
|
||||||
listeners.push(helper.addEventListener(process, 'SIGINT', () => { killWebKit(); process.exit(130); }));
|
|
||||||
if (handleSIGTERM)
|
|
||||||
listeners.push(helper.addEventListener(process, 'SIGTERM', gracefullyCloseWebkit));
|
|
||||||
if (handleSIGHUP)
|
|
||||||
listeners.push(helper.addEventListener(process, 'SIGHUP', gracefullyCloseWebkit));
|
|
||||||
let connection: Connection | null = null;
|
let connection: Connection | null = null;
|
||||||
try {
|
try {
|
||||||
const transport = new PipeTransport(webkitProcess.stdio[3] as NodeJS.WritableStream, webkitProcess.stdio[4] as NodeJS.ReadableStream);
|
const transport = new PipeTransport(launched.process.stdio[3] as NodeJS.WritableStream, launched.process.stdio[4] as NodeJS.ReadableStream);
|
||||||
connection = new Connection(transport, slowMo);
|
connection = new Connection(transport, slowMo);
|
||||||
const browser = new Browser(connection, defaultViewport, webkitProcess, gracefullyCloseWebkit);
|
const browser = new Browser(connection, defaultViewport, launched.process, launched.gracefullyClose);
|
||||||
await browser._waitForTarget(t => t._type === 'page');
|
await browser._waitForTarget(t => t._type === 'page');
|
||||||
return browser;
|
return browser;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
killWebKit();
|
await launched.gracefullyClose();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
function gracefullyCloseWebkit(): Promise<any> {
|
|
||||||
helper.removeEventListeners(listeners);
|
|
||||||
if (connection) {
|
|
||||||
// Attempt to close chrome gracefully
|
|
||||||
connection.send('Browser.close').catch(error => {
|
|
||||||
debugError(error);
|
|
||||||
killWebKit();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return waitForChromeToClose;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This method has to be sync to be used as 'exit' event handler.
|
|
||||||
function killWebKit() {
|
|
||||||
helper.removeEventListeners(listeners);
|
|
||||||
if (webkitProcess.pid && !webkitProcess.killed && !webkitClosed) {
|
|
||||||
// Force kill chrome.
|
|
||||||
try {
|
|
||||||
if (process.platform === 'win32')
|
|
||||||
childProcess.execSync(`taskkill /pid ${webkitProcess.pid} /T /F`);
|
|
||||||
else
|
|
||||||
process.kill(-webkitProcess.pid, 'SIGKILL');
|
|
||||||
} catch (e) {
|
|
||||||
// the process might have already stopped
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
executablePath(): string {
|
executablePath(): string {
|
||||||
|
@ -189,7 +189,6 @@ module.exports.addTests = ({testRunner, product, playwrightPath}) => {
|
|||||||
require('./chromium/connect.spec.js').addTests(testOptions);
|
require('./chromium/connect.spec.js').addTests(testOptions);
|
||||||
require('./chromium/launcher.spec.js').addTests(testOptions);
|
require('./chromium/launcher.spec.js').addTests(testOptions);
|
||||||
require('./chromium/headful.spec.js').addTests(testOptions);
|
require('./chromium/headful.spec.js').addTests(testOptions);
|
||||||
require('./chromium/headful.spec.js').addTests(testOptions);
|
|
||||||
require('./chromium/oopif.spec.js').addTests(testOptions);
|
require('./chromium/oopif.spec.js').addTests(testOptions);
|
||||||
require('./chromium/tracing.spec.js').addTests(testOptions);
|
require('./chromium/tracing.spec.js').addTests(testOptions);
|
||||||
}
|
}
|
||||||
|
83
test/test.js
83
test/test.js
@ -73,48 +73,49 @@ beforeEach(async({server, httpsServer}) => {
|
|||||||
httpsServer.reset();
|
httpsServer.reset();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (process.env.BROWSER === 'firefox') {
|
|
||||||
describe('Firefox', () => {
|
|
||||||
require('./playwright.spec.js').addTests({
|
|
||||||
product: 'Firefox',
|
|
||||||
playwrightPath: path.join(utils.projectRoot(), 'firefox.js'),
|
|
||||||
testRunner,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else if (process.env.BROWSER === 'webkit') {
|
|
||||||
describe('WebKit', () => {
|
|
||||||
require('./playwright.spec.js').addTests({
|
|
||||||
product: 'WebKit',
|
|
||||||
playwrightPath: path.join(utils.projectRoot(), 'webkit.js'),
|
|
||||||
testRunner,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
describe('Chromium', () => {
|
|
||||||
require('./playwright.spec.js').addTests({
|
|
||||||
product: 'Chromium',
|
|
||||||
playwrightPath: path.join(utils.projectRoot(), 'chromium.js'),
|
|
||||||
testRunner,
|
|
||||||
});
|
|
||||||
if (process.env.COVERAGE)
|
|
||||||
utils.recordAPICoverage(testRunner, require('../lib/api').Chromium, require('../lib/chromium/events').Events);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.env.CI && testRunner.hasFocusedTestsOrSuites()) {
|
|
||||||
console.error('ERROR: "focused" tests/suites are prohibitted on bots. Remove any "fit"/"fdescribe" declarations.');
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
new Reporter(testRunner, {
|
|
||||||
verbose: process.argv.includes('--verbose'),
|
|
||||||
summary: !process.argv.includes('--verbose'),
|
|
||||||
projectFolder: utils.projectRoot(),
|
|
||||||
showSlowTests: process.env.CI ? 5 : 0,
|
|
||||||
});
|
|
||||||
|
|
||||||
(async() => {
|
(async() => {
|
||||||
|
if (process.env.BROWSER === 'firefox') {
|
||||||
|
await describe('Firefox', () => {
|
||||||
|
require('./playwright.spec.js').addTests({
|
||||||
|
product: 'Firefox',
|
||||||
|
playwrightPath: path.join(utils.projectRoot(), 'firefox.js'),
|
||||||
|
testRunner,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else if (process.env.BROWSER === 'webkit') {
|
||||||
|
await describe('WebKit', () => {
|
||||||
|
require('./playwright.spec.js').addTests({
|
||||||
|
product: 'WebKit',
|
||||||
|
playwrightPath: path.join(utils.projectRoot(), 'webkit.js'),
|
||||||
|
testRunner,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await describe('Chromium', () => {
|
||||||
|
require('./playwright.spec.js').addTests({
|
||||||
|
product: 'Chromium',
|
||||||
|
playwrightPath: path.join(utils.projectRoot(), 'chromium.js'),
|
||||||
|
testRunner,
|
||||||
|
});
|
||||||
|
if (process.env.COVERAGE)
|
||||||
|
utils.recordAPICoverage(testRunner, require('../lib/api').Chromium, require('../lib/chromium/events').Events);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process.env.CI && testRunner.hasFocusedTestsOrSuites()) {
|
||||||
|
console.error('ERROR: "focused" tests/suites are prohibitted on bots. Remove any "fit"/"fdescribe" declarations.');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
new Reporter(testRunner, {
|
||||||
|
verbose: process.argv.includes('--verbose'),
|
||||||
|
summary: !process.argv.includes('--verbose'),
|
||||||
|
projectFolder: utils.projectRoot(),
|
||||||
|
showSlowTests: process.env.CI ? 5 : 0,
|
||||||
|
});
|
||||||
|
|
||||||
// await utils.initializeFlakinessDashboardIfNeeded(testRunner);
|
// await utils.initializeFlakinessDashboardIfNeeded(testRunner);
|
||||||
testRunner.run();
|
const result = await testRunner.run();
|
||||||
|
process.exit(result.terminationError ? 130 : 0);
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user