feat(launcher): add experimental "transport" option to pptr.connect (#3265)
This patch: - adds experimental "transport" option to pptr.connect - uses "transport" option to make sure Puppeteer-Web works with Target.exposeDevToolsProtocol Drive-by: add `browser.target()` to access browser target.
This commit is contained in:
parent
5acf953104
commit
4e48dfc7a1
@ -48,6 +48,7 @@
|
||||
* [browser.newPage()](#browsernewpage)
|
||||
* [browser.pages()](#browserpages)
|
||||
* [browser.process()](#browserprocess)
|
||||
* [browser.target()](#browsertarget)
|
||||
* [browser.targets()](#browsertargets)
|
||||
* [browser.userAgent()](#browseruseragent)
|
||||
* [browser.version()](#browserversion)
|
||||
@ -677,6 +678,11 @@ the method will return an array with all the pages in all browser contexts.
|
||||
#### browser.process()
|
||||
- returns: <?[ChildProcess]> Spawned browser process. Returns `null` if the browser instance was created with [`puppeteer.connect`](#puppeteerconnectoptions) method.
|
||||
|
||||
#### browser.target()
|
||||
- returns: <[Target]>
|
||||
|
||||
A target associated with the browser.
|
||||
|
||||
#### browser.targets()
|
||||
- returns: <[Array]<[Target]>>
|
||||
|
||||
|
@ -185,6 +185,13 @@ class Browser extends EventEmitter {
|
||||
return Array.from(this._targets.values()).filter(target => target._isInitialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!Target}
|
||||
*/
|
||||
target() {
|
||||
return this.targets().find(target => target.type() === 'browser');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!Promise<!Array<!Puppeteer.Page>>}
|
||||
*/
|
||||
|
@ -270,7 +270,7 @@ class Launcher {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!(BrowserOptions & {browserWSEndpoint: string})} options
|
||||
* @param {!(BrowserOptions & {browserWSEndpoint: string, transport?: !Puppeteer.ConnectionTransport})} options
|
||||
* @return {!Promise<!Browser>}
|
||||
*/
|
||||
async connect(options) {
|
||||
@ -278,9 +278,9 @@ class Launcher {
|
||||
browserWSEndpoint,
|
||||
ignoreHTTPSErrors = false,
|
||||
defaultViewport = {width: 800, height: 600},
|
||||
transport = await WebSocketTransport.create(browserWSEndpoint),
|
||||
slowMo = 0,
|
||||
} = options;
|
||||
const transport = await WebSocketTransport.create(browserWSEndpoint);
|
||||
const connection = new Connection(browserWSEndpoint, transport, slowMo);
|
||||
const {browserContextIds} = await connection.send('Target.getBrowserContexts');
|
||||
return Browser.create(connection, browserContextIds, ignoreHTTPSErrors, defaultViewport, null, () => connection.send('Browser.close').catch(debugError));
|
||||
|
@ -37,7 +37,7 @@ module.exports = class {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{browserWSEndpoint: string, ignoreHTTPSErrors: boolean}} options
|
||||
* @param {{browserWSEndpoint: string, ignoreHTTPSErrors: boolean, transport?: !Puppeteer.ConnectionTransport}} options
|
||||
* @return {!Promise<!Puppeteer.Browser>}
|
||||
*/
|
||||
connect(options) {
|
||||
|
@ -37,6 +37,13 @@ module.exports.addTests = function({testRunner, expect, headless}) {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Browser.target', function() {
|
||||
it('should return browser target', async({browser}) => {
|
||||
const target = browser.target();
|
||||
expect(target.type()).toBe('browser');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Browser.process', function() {
|
||||
it('should return child_process instance', async function({browser}) {
|
||||
const process = await browser.process();
|
||||
|
@ -80,6 +80,7 @@ ESTreeWalker._walkOrder = {
|
||||
'ArrayExpression': ['elements'],
|
||||
'ArrowFunctionExpression': ['params', 'body'],
|
||||
'AssignmentExpression': ['left', 'right'],
|
||||
'AssignmentPattern': ['left', 'right'],
|
||||
'BinaryExpression': ['left', 'right'],
|
||||
'BlockStatement': ['body'],
|
||||
'BreakStatement': ['label'],
|
||||
|
@ -69,6 +69,28 @@ describe('Puppeteer-Web', () => {
|
||||
]);
|
||||
await browser2.close();
|
||||
});
|
||||
it('should work over exposed DevTools protocol', async({browser, page, serverConfig}) => {
|
||||
// Expose devtools protocol binding into page.
|
||||
const session = await browser.target().createCDPSession();
|
||||
const pageInfo = (await session.send('Target.getTargets')).targetInfos.find(info => info.attached);
|
||||
await session.send('Target.exposeDevToolsProtocol', {targetId: pageInfo.targetId});
|
||||
await session.detach();
|
||||
|
||||
// Use in-page puppeteer to create a new page and navigate it to the EMPTY_PAGE
|
||||
await page.evaluate(async serverConfig => {
|
||||
const puppeteer = require('puppeteer');
|
||||
window.cdp.close = () => {};
|
||||
const browser = await puppeteer.connect({transport: window.cdp});
|
||||
const page = await browser.newPage();
|
||||
await page.goto(serverConfig.EMPTY_PAGE);
|
||||
}, serverConfig);
|
||||
const pageURLs = (await browser.pages()).map(page => page.url()).sort();
|
||||
expect(pageURLs).toEqual([
|
||||
'about:blank',
|
||||
'about:blank',
|
||||
serverConfig.EMPTY_PAGE
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
if (process.env.CI && testRunner.hasFocusedTestsOrSuites()) {
|
||||
|
Loading…
Reference in New Issue
Block a user