Skip to content

Commit

Permalink
feat: browserType.connectOverCDP
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelEinbinder committed Feb 6, 2021
1 parent 983e043 commit 4c5d821
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 12 deletions.
21 changes: 21 additions & 0 deletions docs/src/api/class-browsertype.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ This methods attaches Playwright to an existing browser instance.
- `timeout` <[float]> Maximum time in milliseconds to wait for the connection to be established. Defaults to
`30000` (30 seconds). Pass `0` to disable timeout.

## async method: BrowserType.connectOverCDP
* langs: js
- returns: <[Browser]>

This methods attaches Playwright to an existing browser instance using the Chrome DevTools Protocol.

The default browser context is accessible via [`method: Browser.contexts`].

:::note
Connecting over the Chrome DevTools Protocol is only supported for Chromium-based browsers.
:::

### param: BrowserType.connectOverCDP.params
- `params` <[Object]>
- `wsEndpoint` <[string]> A browser websocket endpoint to connect to.
- `slowMo` <[float]> Slows down Playwright operations by the specified amount of milliseconds. Useful so that you
can see what is going on. Defaults to 0.
- `logger` <[Logger]> Logger sink for Playwright logging. Optional.
- `timeout` <[float]> Maximum time in milliseconds to wait for the connection to be established. Defaults to
`30000` (30 seconds). Pass `0` to disable timeout.

## method: BrowserType.executablePath
- returns: <[string]>

Expand Down
19 changes: 19 additions & 0 deletions src/client/browserType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,25 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel, chann
});
}, logger);
}

async connectOverCDP(params: ConnectOptions): Promise<Browser> {
if (this.name() !== 'chromium')
throw new Error('Connecting over CDP is only supported in Chromium.');
const logger = params.logger;
return this._wrapApiCall('browserType.connect', async () => {
const result = await this._channel.cdpConnect({
wsEndpoint: params.wsEndpoint,
slowMo: params.slowMo,
timeout: params.timeout
});
const browser = Browser.from(result.browser);
if (result.defaultContext)
browser._contexts.add(BrowserContext.from(result.defaultContext));
browser._isRemote = true;
browser._logger = logger;
return browser;
}, logger);
}
}

export class RemoteBrowser extends ChannelOwner<channels.RemoteBrowserChannel, channels.RemoteBrowserInitializer> {
Expand Down
8 changes: 8 additions & 0 deletions src/dispatchers/browserTypeDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ export class BrowserTypeDispatcher extends Dispatcher<BrowserType, channels.Brow
const browserContext = await this._object.launchPersistentContext(params.userDataDir, params);
return { context: new BrowserContextDispatcher(this._scope, browserContext) };
}

async cdpConnect(params: channels.BrowserTypeCdpConnectParams): Promise<channels.BrowserTypeCdpConnectResult> {
const browser = await this._object.cdpConnect(params.wsEndpoint, params, params.timeout);
return {
browser: new BrowserDispatcher(this._scope, browser),
defaultContext: browser._defaultContext ? new BrowserContextDispatcher(this._scope, browser._defaultContext!) : undefined,
};
}
}
14 changes: 14 additions & 0 deletions src/protocol/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export type BrowserTypeInitializer = {
export interface BrowserTypeChannel extends Channel {
launch(params: BrowserTypeLaunchParams, metadata?: Metadata): Promise<BrowserTypeLaunchResult>;
launchPersistentContext(params: BrowserTypeLaunchPersistentContextParams, metadata?: Metadata): Promise<BrowserTypeLaunchPersistentContextResult>;
cdpConnect(params: BrowserTypeCdpConnectParams, metadata?: Metadata): Promise<BrowserTypeCdpConnectResult>;
}
export type BrowserTypeLaunchParams = {
executablePath?: string,
Expand Down Expand Up @@ -377,6 +378,19 @@ export type BrowserTypeLaunchPersistentContextOptions = {
export type BrowserTypeLaunchPersistentContextResult = {
context: BrowserContextChannel,
};
export type BrowserTypeCdpConnectParams = {
wsEndpoint: string,
slowMo?: number,
timeout?: number,
};
export type BrowserTypeCdpConnectOptions = {
slowMo?: number,
timeout?: number,
};
export type BrowserTypeCdpConnectResult = {
browser: BrowserChannel,
defaultContext?: BrowserContextChannel,
};

// ----------- Browser -----------
export type BrowserInitializer = {
Expand Down
8 changes: 8 additions & 0 deletions src/protocol/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,14 @@ BrowserType:
returns:
context: BrowserContext

cdpConnect:
parameters:
wsEndpoint: string
slowMo: number?
timeout: number?
returns:
browser: Browser
defaultContext: BrowserContext?

Browser:
type: interface
Expand Down
5 changes: 5 additions & 0 deletions src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ export function createScheme(tChannel: (name: string) => Validator): Scheme {
path: tString,
})),
});
scheme.BrowserTypeCdpConnectParams = tObject({
wsEndpoint: tString,
slowMo: tOptional(tNumber),
timeout: tOptional(tNumber),
});
scheme.BrowserCloseParams = tOptional(tObject({}));
scheme.BrowserNewContextParams = tObject({
noDefaultViewport: tOptional(tBoolean),
Expand Down
5 changes: 2 additions & 3 deletions src/server/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { ChildProcess } from 'child_process';
import { RecentLogsCollector } from '../utils/debugLogger';

export interface BrowserProcess {
onclose: ((exitCode: number | null, signal: string | null) => void) | undefined;
onclose?: ((exitCode: number | null, signal: string | null) => void);
process?: ChildProcess;
kill(): Promise<void>;
close(): Promise<void>;
Expand All @@ -35,7 +35,7 @@ export type PlaywrightOptions = {
isInternal: boolean
};

export type BrowserOptions = PlaywrightOptions & {
export type BrowserOptions = PlaywrightOptions & types.UIOptions & {
name: string,
isChromium: boolean,
downloadsPath?: string,
Expand All @@ -45,7 +45,6 @@ export type BrowserOptions = PlaywrightOptions & {
proxy?: ProxySettings,
protocolLogger: types.ProtocolLogger,
browserLogsCollector: RecentLogsCollector,
slowMo?: number,
};

export abstract class Browser extends EventEmitter {
Expand Down
4 changes: 4 additions & 0 deletions src/server/browserType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ export abstract class BrowserType {
return { browserProcess, downloadsPath, transport };
}

async cdpConnect(wsEndpoint: string, uiOptions: types.UIOptions, timeout?: number): Promise<Browser> {
throw new Error('CDP connections are only supported by Chromium');
}

abstract _defaultArgs(options: types.LaunchOptions, isPersistent: boolean, userDataDir: string): string[];
abstract _connectToTransport(transport: ConnectionTransport, options: BrowserOptions): Promise<Browser>;
abstract _amendEnvironment(env: Env, userDataDir: string, executable: string, browserArguments: string[]): Env;
Expand Down
37 changes: 35 additions & 2 deletions src/server/chromium/chromium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ import { Env } from '../processLauncher';
import { kBrowserCloseMessageId } from './crConnection';
import { rewriteErrorMessage } from '../../utils/stackTrace';
import { BrowserType } from '../browserType';
import { ConnectionTransport, ProtocolRequest } from '../transport';
import { ConnectionTransport, ProtocolRequest, WebSocketTransport } from '../transport';
import * as browserPaths from '../../utils/browserPaths';
import { CRDevTools } from './crDevTools';
import { BrowserOptions, PlaywrightOptions } from '../browser';
import { BrowserOptions, BrowserProcess, PlaywrightOptions } from '../browser';
import * as types from '../types';
import { isDebugMode } from '../../utils/utils';
import { RecentLogsCollector } from '../../utils/debugLogger';
import { ProgressController } from '../progress';
import { TimeoutSettings } from '../../utils/timeoutSettings';
import { helper } from '../helper';

export class Chromium extends BrowserType {
private _devtools: CRDevTools | undefined;
Expand All @@ -42,6 +46,35 @@ export class Chromium extends BrowserType {
this._devtools = this._createDevTools();
}

async cdpConnect(wsEndpoint: string, uiOptions: types.UIOptions, timeout?: number) {
const controller = new ProgressController();
controller.setLogName('browser');
const browserLogsCollector = new RecentLogsCollector();
return controller.run(async progress => {
const chromeTransport = await WebSocketTransport.connect(progress, wsEndpoint);
const browserProcess: BrowserProcess = {
close: async () => {
await chromeTransport.closeAndWait();
},
kill: async () => {
await chromeTransport.closeAndWait();
}
};
const browserOptions: BrowserOptions = {
...this._playwrightOptions,
...uiOptions,
name: 'chromium',
isChromium: true,
headful: true,
persistent: { noDefaultViewport: true },
browserProcess,
protocolLogger: helper.debugProtocolLogger(),
browserLogsCollector,
};
return await CRBrowser.connect(chromeTransport, browserOptions, null);
}, TimeoutSettings.timeout({timeout}));
}

private _createDevTools() {
return new CRDevTools(path.join(this._browserPath, 'devtools-preferences.json'));
}
Expand Down
5 changes: 3 additions & 2 deletions src/server/chromium/crBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@ export class CRBrowser extends Browser {
if (targetInfo.type === 'background_page') {
const backgroundPage = new CRPage(session, targetInfo.targetId, context, null, false);
this._backgroundPages.set(targetInfo.targetId, backgroundPage);
backgroundPage.pageOrError().then(() => {
context!.emit(CRBrowserContext.CREvents.BackgroundPage, backgroundPage._page);
backgroundPage.pageOrError().then(pageOrError => {
if (pageOrError instanceof Page)
context!.emit(CRBrowserContext.CREvents.BackgroundPage, backgroundPage._page);
});
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ export class WebSocketTransport implements ConnectionTransport {
}

async closeAndWait() {
const promise = new Promise(f => this.onclose = f);
const promise = new Promise(f => this._ws.once('close', f));
this.close();
return promise; // Make sure to await the actual disconnect.
await promise; // Make sure to await the actual disconnect.
}
}
7 changes: 5 additions & 2 deletions src/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export type BrowserContextOptions = {

export type EnvArray = { name: string, value: string }[];

type LaunchOptionsBase = {
type LaunchOptionsBase = UIOptions & {
executablePath?: string,
args?: string[],
ignoreDefaultArgs?: string[],
Expand All @@ -269,7 +269,6 @@ type LaunchOptionsBase = {
proxy?: ProxySettings,
downloadsPath?: string,
chromiumSandbox?: boolean,
slowMo?: number,
};
export type LaunchOptions = LaunchOptionsBase & {
firefoxUserPrefs?: { [key: string]: string | number | boolean },
Expand Down Expand Up @@ -345,3 +344,7 @@ export type SetStorageState = {
cookies?: SetNetworkCookieParam[],
origins?: OriginStorage[]
}

export type UIOptions = {
slowMo?: number;
}
45 changes: 44 additions & 1 deletion test/chromium/chromium.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright 2018 Google Inc. All rights reserved.
* Modifications copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +15,9 @@
* limitations under the License.
*/
import { it, expect, describe } from '../fixtures';
import type { ChromiumBrowserContext } from '../..';
import type { ChromiumBrowserContext, chromium } from '../..';
import { ChildProcess, spawn } from 'child_process';
import readline from 'readline';

describe('chromium', (suite, { browserName }) => {
suite.skip(browserName !== 'chromium');
Expand Down Expand Up @@ -88,4 +91,44 @@ describe('chromium', (suite, { browserName }) => {
// make it work with Edgium.
expect(serverRequest.headers.intervention).toContain('feature/5718547946799104');
});

it('should connect to an existing cdp session', async ({browserType, testWorkerIndex, browserOptions, createUserDataDir }) => {
const port = 9339 + testWorkerIndex;
const spawnedProcess = spawn(
browserType.executablePath(),
[`--remote-debugging-port=${port}`, `--user-data-dir=${await createUserDataDir()}`],
{
// 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',
}
);
try {
const match = await waitForLine(spawnedProcess, /^DevTools listening on (ws:\/\/.*)$/);
const crBrowserType = browserType as typeof chromium;
const cdpBrowser = await crBrowserType.connectOverCDP({
wsEndpoint: match[1],
});
const contexts = cdpBrowser.contexts();
expect(contexts.length).toBe(1);
await cdpBrowser.close();
} finally {
spawnedProcess.kill();
}
});
});

function waitForLine(process: ChildProcess, regex: RegExp): Promise<RegExpMatchArray> {
return new Promise(resolve => {
const rl = readline.createInterface({ input: process.stderr });
rl.on('line', onLine);

function onLine(line: string) {
const match = line.match(regex);
if (!match)
return;
resolve(match);
}
});
}
33 changes: 33 additions & 0 deletions types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6258,6 +6258,39 @@ export interface BrowserType<Browser> {
*/
connect(params: ConnectOptions): Promise<Browser>;

/**
* This methods attaches Playwright to an existing browser instance using the Chrome DevTools Protocol.
*
* The default browser context is accessible via
* [browser.contexts()](https://playwright.dev/docs/api/class-browser#browsercontexts).
*
* > NOTE: Connecting over the Chrome DevTools Protocol is only supported for Chromium-based browsers.
* @param params
*/
connectOverCDP(params: {
/**
* A browser websocket endpoint to connect to.
*/
wsEndpoint: string;

/**
* Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
* Defaults to 0.
*/
slowMo?: number;

/**
* Logger sink for Playwright logging. Optional.
*/
logger?: Logger;

/**
* Maximum time in milliseconds to wait for the connection to be established. Defaults to `30000` (30 seconds). Pass `0` to
* disable timeout.
*/
timeout?: number;
}): Promise<Browser>;

/**
* A path where Playwright expects to find a bundled browser executable.
*/
Expand Down

0 comments on commit 4c5d821

Please sign in to comment.