Skip to content

Commit

Permalink
feat(page): expose page.client() (#7582)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
OrKoN committed Sep 21, 2021
1 parent 2b5c001 commit 99ca842
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]>
Expand Down
7 changes: 7 additions & 0 deletions src/common/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
8 changes: 8 additions & 0 deletions test/page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 99ca842

Please sign in to comment.