From 99ca842124a1edef5e66426621885141a9feaca5 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Tue, 21 Sep 2021 09:39:47 +0200 Subject: [PATCH] feat(page): expose page.client() (#7582) Puppeteer already allows creating a new CDP session via target.createCDPSession but there is no way to get access to any existing session to send some additional commands. --- docs/api.md | 7 +++++++ src/common/Page.ts | 7 +++++++ test/page.spec.ts | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/docs/api.md b/docs/api.md index 4fd5f5b2673..e08a689e642 100644 --- a/docs/api.md +++ b/docs/api.md @@ -133,6 +133,7 @@ * [page.browser()](#pagebrowser) * [page.browserContext()](#pagebrowsercontext) * [page.click(selector[, options])](#pageclickselector-options) + * [page.client()](#pageclient) * [page.close([options])](#pagecloseoptions) * [page.content()](#pagecontent) * [page.cookies([...urls])](#pagecookiesurls) @@ -1465,6 +1466,12 @@ const [response] = await Promise.all([ Shortcut for [page.mainFrame().click(selector[, options])](#frameclickselector-options). +#### page.client() + +- returns: <[CDPSession]> + +Get the CDP session client the page belongs to. + #### page.close([options]) - `options` <[Object]> diff --git a/src/common/Page.ts b/src/common/Page.ts index 29673f1f190..6b710be5f1b 100644 --- a/src/common/Page.ts +++ b/src/common/Page.ts @@ -729,6 +729,13 @@ export class Page extends EventEmitter { return this._target; } + /** + * Get the CDP session client the page belongs to. + */ + client(): CDPSession { + return this._client; + } + /** * Get the browser the page belongs to. */ diff --git a/test/page.spec.ts b/test/page.spec.ts index bc05e757513..495ce94e695 100644 --- a/test/page.spec.ts +++ b/test/page.spec.ts @@ -27,6 +27,7 @@ import { describeFailsFirefox, } from './mocha-utils'; // eslint-disable-line import/extensions import { Page, Metrics } from '../lib/cjs/puppeteer/common/Page.js'; +import { CDPSession } from '../lib/cjs/puppeteer/common/Connection.js'; import { JSHandle } from '../lib/cjs/puppeteer/common/JSHandle.js'; describe('Page', function () { @@ -1902,4 +1903,11 @@ describe('Page', function () { expect(page.browserContext()).toBe(context); }); }); + + describe('Page.client', function () { + it('should return the client instance', async () => { + const { page } = getTestState(); + expect(page.client()).toBeInstanceOf(CDPSession); + }); + }); });