chore: simplify launch routine a bit more (#2336)

This commit is contained in:
Dmitry Gozman 2020-05-21 19:16:13 -07:00 committed by GitHub
parent 3c84e9ec3a
commit 3aca21c13b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 55 deletions

View File

@ -124,7 +124,20 @@ export abstract class BrowserTypeBase implements BrowserType {
if ((options as any).__testHookBeforeCreateBrowser)
await (options as any).__testHookBeforeCreateBrowser();
const browser = await this._connectToServer(browserServer, persistent);
const browserOptions: BrowserOptions = {
slowMo: options.slowMo,
persistent,
headful: browserServer._headful,
logger: browserServer._logger,
downloadsPath: browserServer._downloadsPath,
ownedServer: browserServer,
};
for (const [key, value] of Object.entries(options)) {
if (key.startsWith('__testHook'))
(browserOptions as any)[key] = value;
}
const browser = await this._connectToTransport(browserServer._transport, browserOptions);
if (persistent && (!options.ignoreDefaultArgs || Array.isArray(options.ignoreDefaultArgs))) {
const context = browser._defaultContext!;
await context._loadDefaultContext();
@ -171,6 +184,5 @@ export abstract class BrowserTypeBase implements BrowserType {
}
abstract _launchServer(options: LaunchServerOptions, launchType: LaunchType, logger: RootLogger, deadline: number, userDataDir?: string): Promise<BrowserServer>;
abstract _connectToServer(browserServer: BrowserServer, persistent: PersistentContextOptions | undefined): Promise<BrowserBase>;
abstract _connectToTransport(transport: ConnectionTransport, options: BrowserOptions): Promise<BrowserBase>;
}
}

View File

@ -32,8 +32,7 @@ import { ConnectionTransport, ProtocolRequest } from '../transport';
import { InnerLogger, logError, RootLogger } from '../logger';
import { BrowserDescriptor } from '../install/browserPaths';
import { CRDevTools } from '../chromium/crDevTools';
import { BrowserBase, BrowserOptions } from '../browser';
import { PersistentContextOptions } from '../browserContext';
import { BrowserOptions } from '../browser';
export class Chromium extends BrowserTypeBase {
private _devtools: CRDevTools | undefined;
@ -48,25 +47,13 @@ export class Chromium extends BrowserTypeBase {
return new CRDevTools(path.join(this._browserPath, 'devtools-preferences.json'));
}
async _connectToServer(browserServer: BrowserServer, persistent: PersistentContextOptions | undefined): Promise<BrowserBase> {
const options = browserServer._launchOptions;
async _connectToTransport(transport: ConnectionTransport, options: BrowserOptions): Promise<CRBrowser> {
let devtools = this._devtools;
if ((options as any).__testHookForDevTools) {
devtools = this._createDevTools();
await (options as any).__testHookForDevTools(devtools);
}
return await CRBrowser.connect(browserServer._transport, {
slowMo: options.slowMo,
persistent,
headful: browserServer._headful,
logger: browserServer._logger,
downloadsPath: browserServer._downloadsPath,
ownedServer: browserServer,
}, devtools);
}
_connectToTransport(transport: ConnectionTransport, options: BrowserOptions): Promise<CRBrowser> {
return CRBrowser.connect(transport, options);
return CRBrowser.connect(transport, options, devtools);
}
async _launchServer(options: LaunchServerOptions, launchType: LaunchType, logger: RootLogger, deadline: number, userDataDir?: string): Promise<BrowserServer> {

View File

@ -30,28 +30,11 @@ import { BrowserArgOptions, LaunchServerOptions, BrowserTypeBase, processBrowser
import { launchProcess, waitForLine } from './processLauncher';
import { ConnectionTransport, SequenceNumberMixer, WebSocketTransport } from '../transport';
import { InnerLogger, logError, RootLogger } from '../logger';
import { BrowserDescriptor } from '../install/browserPaths';
import { BrowserBase, BrowserOptions } from '../browser';
import { PersistentContextOptions } from '../browserContext';
import { BrowserOptions } from '../browser';
const mkdtempAsync = util.promisify(fs.mkdtemp);
export class Firefox extends BrowserTypeBase {
constructor(packagePath: string, browser: BrowserDescriptor) {
super(packagePath, browser);
}
_connectToServer(browserServer: BrowserServer, persistent: PersistentContextOptions | undefined): Promise<BrowserBase> {
return FFBrowser.connect(browserServer._transport, {
slowMo: browserServer._launchOptions.slowMo,
logger: browserServer._logger,
persistent,
downloadsPath: browserServer._downloadsPath,
headful: browserServer._headful,
ownedServer: browserServer,
});
}
_connectToTransport(transport: ConnectionTransport, options: BrowserOptions): Promise<FFBrowser> {
return FFBrowser.connect(transport, options);
}

View File

@ -30,26 +30,9 @@ import * as ws from 'ws';
import { BrowserServer, WebSocketWrapper } from './browserServer';
import { Events } from '../events';
import { InnerLogger, logError, RootLogger } from '../logger';
import { BrowserDescriptor } from '../install/browserPaths';
import { BrowserBase, BrowserOptions } from '../browser';
import { PersistentContextOptions } from '../browserContext';
import { BrowserOptions } from '../browser';
export class WebKit extends BrowserTypeBase {
constructor(packagePath: string, browser: BrowserDescriptor) {
super(packagePath, browser);
}
_connectToServer(browserServer: BrowserServer, persistent: PersistentContextOptions | undefined): Promise<BrowserBase> {
return WKBrowser.connect(browserServer._transport, {
slowMo: browserServer._launchOptions.slowMo,
headful: browserServer._headful,
logger: browserServer._logger,
persistent,
downloadsPath: browserServer._downloadsPath,
ownedServer: browserServer
});
}
_connectToTransport(transport: ConnectionTransport, options: BrowserOptions): Promise<WKBrowser> {
return WKBrowser.connect(transport, options);
}