feat(launcher): throw on browser launch failure, default args (#119)

This commit is contained in:
Yury Semikhatsky 2019-12-02 17:17:53 -07:00 committed by Andrey Lushnikov
parent b3f55716ab
commit 2ca2a4cb18
5 changed files with 62 additions and 11 deletions

View file

@ -137,6 +137,15 @@ export class Launcher {
} }
); );
if (!chromeProcess.pid) {
let reject;
const result = new Promise((f, r) => reject = r);
chromeProcess.once('error', error => {
reject(new Error('Failed to launch browser: ' + error));
});
return result as Promise<Browser>;
}
if (dumpio) { if (dumpio) {
chromeProcess.stderr.pipe(process.stderr); chromeProcess.stderr.pipe(process.stderr);
chromeProcess.stdout.pipe(process.stdout); chromeProcess.stdout.pipe(process.stdout);

View file

@ -121,6 +121,15 @@ export class Launcher {
} }
); );
if (!firefoxProcess.pid) {
let reject;
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) { if (dumpio) {
firefoxProcess.stderr.pipe(process.stderr); firefoxProcess.stderr.pipe(process.stderr);
firefoxProcess.stdout.pipe(process.stdout); firefoxProcess.stdout.pipe(process.stdout);

View file

@ -22,6 +22,8 @@ import { Connection } from './Connection';
import { Viewport } from './Page'; import { Viewport } from './Page';
import { PipeTransport } from './PipeTransport'; import { PipeTransport } from './PipeTransport';
const DEFAULT_ARGS = [
];
export class Launcher { export class Launcher {
private _projectRoot: string; private _projectRoot: string;
@ -32,8 +34,18 @@ export class Launcher {
this._preferredRevision = preferredRevision; this._preferredRevision = preferredRevision;
} }
defaultArgs(options: any = {}) {
const {
args = [],
} = options;
const webkitArguments = [...DEFAULT_ARGS];
webkitArguments.push(...args);
return webkitArguments;
}
async launch(options: LauncherLaunchOptions = {}): Promise<Browser> { async launch(options: LauncherLaunchOptions = {}): Promise<Browser> {
const { const {
ignoreDefaultArgs = false,
args = [], args = [],
dumpio = false, dumpio = false,
executablePath = null, executablePath = null,
@ -45,7 +57,12 @@ export class Launcher {
slowMo = 0 slowMo = 0
} = options; } = options;
const webkitArguments = args.slice(); const webkitArguments = [];
if (!ignoreDefaultArgs)
webkitArguments.push(...this.defaultArgs(options));
else
webkitArguments.push(...args);
let webkitExecutable = executablePath; let webkitExecutable = executablePath;
if (!executablePath) { if (!executablePath) {
const {missingText, executablePath} = this._resolveExecutablePath(); const {missingText, executablePath} = this._resolveExecutablePath();
@ -73,6 +90,15 @@ export class Launcher {
} }
); );
if (!webkitProcess.pid) {
let reject;
const result = new Promise((f, r) => reject = r);
webkitProcess.once('error', error => {
reject(new Error('Failed to launch browser: ' + error));
});
return result as Promise<Browser>;
}
if (dumpio) { if (dumpio) {
webkitProcess.stderr.pipe(process.stderr); webkitProcess.stderr.pipe(process.stderr);
webkitProcess.stdout.pipe(process.stdout); webkitProcess.stdout.pipe(process.stdout);
@ -147,6 +173,7 @@ export class Launcher {
} }
export type LauncherLaunchOptions = { export type LauncherLaunchOptions = {
ignoreDefaultArgs?: boolean,
args?: string[], args?: string[],
executablePath?: string, executablePath?: string,
handleSIGINT?: boolean, handleSIGINT?: boolean,

View file

@ -48,6 +48,10 @@ export class Playwright {
return Errors; return Errors;
} }
defaultArgs(options: any | undefined): string[] {
return this._launcher.defaultArgs(options);
}
createBrowserFetcher(options: BrowserFetcherOptions | undefined): BrowserFetcher { createBrowserFetcher(options: BrowserFetcherOptions | undefined): BrowserFetcher {
return new BrowserFetcher(this._projectRoot, options); return new BrowserFetcher(this._projectRoot, options);
} }

View file

@ -101,7 +101,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
} }
}); });
}); });
describe.skip(WEBKIT)('Playwright.launch', function() { describe('Playwright.launch', function() {
it('should reject all promises when browser is closed', async() => { it('should reject all promises when browser is closed', async() => {
const browser = await playwright.launch(defaultBrowserOptions); const browser = await playwright.launch(defaultBrowserOptions);
const page = await browser.newPage(); const page = await browser.newPage();
@ -117,7 +117,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
await playwright.launch(options).catch(e => waitError = e); await playwright.launch(options).catch(e => waitError = e);
expect(waitError.message).toContain('Failed to launch'); expect(waitError.message).toContain('Failed to launch');
}); });
it('userDataDir option', async({server}) => { it.skip(WEBKIT)('userDataDir option', async({server}) => {
const userDataDir = await mkdtempAsync(TMP_FOLDER); const userDataDir = await mkdtempAsync(TMP_FOLDER);
const options = Object.assign({userDataDir}, defaultBrowserOptions); const options = Object.assign({userDataDir}, defaultBrowserOptions);
const browser = await playwright.launch(options); const browser = await playwright.launch(options);
@ -129,7 +129,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778 // This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
await rmAsync(userDataDir).catch(e => {}); await rmAsync(userDataDir).catch(e => {});
}); });
it('userDataDir argument', async({server}) => { it.skip(WEBKIT)('userDataDir argument', async({server}) => {
const userDataDir = await mkdtempAsync(TMP_FOLDER); const userDataDir = await mkdtempAsync(TMP_FOLDER);
const options = Object.assign({}, defaultBrowserOptions); const options = Object.assign({}, defaultBrowserOptions);
if (CHROME || WEBKIT) { if (CHROME || WEBKIT) {
@ -151,7 +151,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778 // This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
await rmAsync(userDataDir).catch(e => {}); await rmAsync(userDataDir).catch(e => {});
}); });
it('userDataDir option should restore state', async({server}) => { it.skip(WEBKIT)('userDataDir option should restore state', async({server}) => {
const userDataDir = await mkdtempAsync(TMP_FOLDER); const userDataDir = await mkdtempAsync(TMP_FOLDER);
const options = Object.assign({userDataDir}, defaultBrowserOptions); const options = Object.assign({userDataDir}, defaultBrowserOptions);
const browser = await playwright.launch(options); const browser = await playwright.launch(options);
@ -187,11 +187,13 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
await rmAsync(userDataDir).catch(e => {}); await rmAsync(userDataDir).catch(e => {});
}); });
it('should return the default arguments', async() => { it('should return the default arguments', async() => {
if (CHROME || WEBKIT) { if (CHROME) {
expect(playwright.defaultArgs()).toContain('--no-first-run'); expect(playwright.defaultArgs()).toContain('--no-first-run');
expect(playwright.defaultArgs()).toContain('--headless'); expect(playwright.defaultArgs()).toContain('--headless');
expect(playwright.defaultArgs({headless: false})).not.toContain('--headless'); expect(playwright.defaultArgs({headless: false})).not.toContain('--headless');
expect(playwright.defaultArgs({userDataDir: 'foo'})).toContain('--user-data-dir=foo'); expect(playwright.defaultArgs({userDataDir: 'foo'})).toContain('--user-data-dir=foo');
} else if (WEBKIT) {
expect(playwright.defaultArgs().length).toBe(0);
} else { } else {
expect(playwright.defaultArgs({browser: 'firefox'})).toContain('-headless'); expect(playwright.defaultArgs({browser: 'firefox'})).toContain('-headless');
expect(playwright.defaultArgs({browser: 'firefox', headless: false})).not.toContain('-headless'); expect(playwright.defaultArgs({browser: 'firefox', headless: false})).not.toContain('-headless');
@ -208,7 +210,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
await page.close(); await page.close();
await browser.close(); await browser.close();
}); });
it('should filter out ignored default arguments', async() => { it.skip(WEBKIT)('should filter out ignored default arguments', async() => {
// Make sure we launch with `--enable-automation` by default. // Make sure we launch with `--enable-automation` by default.
const defaultArgs = playwright.defaultArgs(defaultBrowserOptions); const defaultArgs = playwright.defaultArgs(defaultBrowserOptions);
const browser = await playwright.launch(Object.assign({}, defaultBrowserOptions, { const browser = await playwright.launch(Object.assign({}, defaultBrowserOptions, {
@ -221,13 +223,13 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
expect(spawnargs.indexOf(defaultArgs[2])).toBe(-1); expect(spawnargs.indexOf(defaultArgs[2])).toBe(-1);
await browser.close(); await browser.close();
}); });
it.skip(FFOX)('should have default URL when launching browser', async function() { it.skip(FFOX || WEBKIT)('should have default URL when launching browser', async function() {
const browser = await playwright.launch(defaultBrowserOptions); const browser = await playwright.launch(defaultBrowserOptions);
const pages = (await browser.pages()).map(page => page.url()); const pages = (await browser.pages()).map(page => page.url());
expect(pages).toEqual(['about:blank']); expect(pages).toEqual(['about:blank']);
await browser.close(); await browser.close();
}); });
it.skip(FFOX)('should have custom URL when launching browser', async function({server}) { it.skip(FFOX || WEBKIT)('should have custom URL when launching browser', async function({server}) {
const options = Object.assign({}, defaultBrowserOptions); const options = Object.assign({}, defaultBrowserOptions);
options.args = [server.EMPTY_PAGE].concat(options.args || []); options.args = [server.EMPTY_PAGE].concat(options.args || []);
const browser = await playwright.launch(options); const browser = await playwright.launch(options);
@ -239,7 +241,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
expect(page.url()).toBe(server.EMPTY_PAGE); expect(page.url()).toBe(server.EMPTY_PAGE);
await browser.close(); await browser.close();
}); });
it('should set the default viewport', async() => { it.skip(WEBKIT)('should set the default viewport', async() => {
const options = Object.assign({}, defaultBrowserOptions, { const options = Object.assign({}, defaultBrowserOptions, {
defaultViewport: { defaultViewport: {
width: 456, width: 456,
@ -360,7 +362,7 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
}); });
}); });
describe.skip(WEBKIT)('Top-level requires', function() { describe('Top-level requires', function() {
it('should require top-level Errors', async() => { it('should require top-level Errors', async() => {
const Errors = require(path.join(utils.projectRoot(), '/Errors')); const Errors = require(path.join(utils.projectRoot(), '/Errors'));
expect(Errors.TimeoutError).toBe(playwright.errors.TimeoutError); expect(Errors.TimeoutError).toBe(playwright.errors.TimeoutError);