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.
This commit is contained in:
Alex Rudenko 2021-09-21 09:39:47 +02:00 committed by GitHub
parent 2b5c0019dc
commit 99ca842124
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View File

@ -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]>

View File

@ -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.
*/

View File

@ -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);
});
});
});