Skip to content

Commit

Permalink
feat(api): support connectOverCDP option to connect()
Browse files Browse the repository at this point in the history
so that when using playwright test it's possible to connect using CDP by adding `connectOverCDP` to `connectOptions`
  • Loading branch information
tjenkinson committed Oct 13, 2022
1 parent 08a3a26 commit aebc82b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
10 changes: 7 additions & 3 deletions packages/playwright-core/src/client/browserType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,17 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel> imple
return context;
}

connect(options: api.ConnectOptions & { wsEndpoint?: string }): Promise<api.Browser>;
connect(options: api.ConnectOptions & { wsEndpoint?: string, connectOverCDP?: boolean }): Promise<api.Browser>;
connect(wsEndpoint: string, options?: api.ConnectOptions): Promise<api.Browser>;
async connect(optionsOrWsEndpoint: string|(api.ConnectOptions & { wsEndpoint?: string }), options?: api.ConnectOptions): Promise<Browser>{
async connect(optionsOrWsEndpoint: string|(api.ConnectOptions & { wsEndpoint?: string, connectOverCDP?: boolean }), options?: api.ConnectOptions): Promise<Browser>{
if (typeof optionsOrWsEndpoint === 'string')
return this._connect(optionsOrWsEndpoint, options);
assert(optionsOrWsEndpoint.wsEndpoint, 'options.wsEndpoint is required');
return this._connect(optionsOrWsEndpoint.wsEndpoint, optionsOrWsEndpoint);
if (optionsOrWsEndpoint.connectOverCDP) {
return this._connectOverCDP(optionsOrWsEndpoint.wsEndpoint, optionsOrWsEndpoint);
} else {
return this._connect(optionsOrWsEndpoint.wsEndpoint, optionsOrWsEndpoint);
}
}

async _connect(wsEndpoint: string, params: Partial<ConnectOptions> = {}): Promise<Browser> {
Expand Down
3 changes: 2 additions & 1 deletion packages/playwright-core/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10957,6 +10957,7 @@ export interface BrowserType<Unused = {}> {
connect(wsEndpoint: string, options?: ConnectOptions): Promise<Browser>;
/**
* wsEndpoint in options is deprecated. Instead use `wsEndpoint`.
* connectOverCDP in options is deprecated. Instead use `connectOverCDP()`.
* @param wsEndpoint A browser websocket endpoint to connect to.
* @param options
* @deprecated
Expand All @@ -10968,7 +10969,7 @@ export interface BrowserType<Unused = {}> {
* @param wsEndpoint A browser websocket endpoint to connect to.
* @param options
*/
connect(options: ConnectOptions & { wsEndpoint?: string }): Promise<Browser>;
connect(options: ConnectOptions & { wsEndpoint?: string, connectOverCDP?: boolean }): Promise<Browser>;
/**
* A path where Playwright expects to find a bundled browser executable.
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/playwright-test/types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2659,6 +2659,11 @@ type ConnectOptions = {
*/
wsEndpoint: string;

/**
* Connect using the Chrome DevTools Protocol.
*/
connectOverCDP?: boolean;

/**
* Additional HTTP headers to be sent with web socket connect request.
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/library/browsercontext-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,24 @@ it('should work with connectOverCDP', async ({ browserName, browserType, server
}
});

it('should support connectOverCDP option to connect', async ({ browserName, browserType, server }, testInfo) => {
it.skip(browserName !== 'chromium');
const port = 9339 + testInfo.workerIndex;
const browserServer = await browserType.launch({
args: ['--remote-debugging-port=' + port]
});
try {
const cdpBrowser = await browserType.connect({ wsEndpoint: `http://127.0.0.1:${port}/`, connectOverCDP: true });
const [context] = cdpBrowser.contexts();
const response = await context.request.get(server.PREFIX + '/simple.json');
expect(response.url()).toBe(server.PREFIX + '/simple.json');
expect(response.status()).toBe(200);
expect(await response.text()).toBe('{"foo": "bar"}\n');
} finally {
await browserServer.close();
}
});

it('should support SameSite cookie attribute over https', async ({ contextFactory, httpsServer, browserName, isWindows }) => {
// Cookies with SameSite=None must also specify the Secure attribute. WebKit navigation
// to HTTP url will fail if the response contains a cookie with Secure attribute, so
Expand Down
5 changes: 5 additions & 0 deletions utils/generate_types/overrides-test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ type ConnectOptions = {
*/
wsEndpoint: string;

/**
* Connect using the Chrome DevTools Protocol.
*/
connectOverCDP?: boolean;

/**
* Additional HTTP headers to be sent with web socket connect request.
*/
Expand Down
3 changes: 2 additions & 1 deletion utils/generate_types/overrides.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,12 @@ export interface BrowserType<Unused = {}> {
connect(wsEndpoint: string, options?: ConnectOptions): Promise<Browser>;
/**
* wsEndpoint in options is deprecated. Instead use `wsEndpoint`.
* connectOverCDP in options is deprecated. Instead use `connectOverCDP()`.
* @param wsEndpoint A browser websocket endpoint to connect to.
* @param options
* @deprecated
*/
connect(options: ConnectOptions & { wsEndpoint?: string }): Promise<Browser>;
connect(options: ConnectOptions & { wsEndpoint?: string, connectOverCDP?: boolean }): Promise<Browser>;
}

export interface CDPSession {
Expand Down

0 comments on commit aebc82b

Please sign in to comment.