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:
Andrey Lushnikov 2018-09-20 11:55:23 -07:00 committed by GitHub
parent 5acf953104
commit 4e48dfc7a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 3 deletions

View File

@ -48,6 +48,7 @@
* [browser.newPage()](#browsernewpage) * [browser.newPage()](#browsernewpage)
* [browser.pages()](#browserpages) * [browser.pages()](#browserpages)
* [browser.process()](#browserprocess) * [browser.process()](#browserprocess)
* [browser.target()](#browsertarget)
* [browser.targets()](#browsertargets) * [browser.targets()](#browsertargets)
* [browser.userAgent()](#browseruseragent) * [browser.userAgent()](#browseruseragent)
* [browser.version()](#browserversion) * [browser.version()](#browserversion)
@ -677,6 +678,11 @@ the method will return an array with all the pages in all browser contexts.
#### browser.process() #### browser.process()
- returns: <?[ChildProcess]> Spawned browser process. Returns `null` if the browser instance was created with [`puppeteer.connect`](#puppeteerconnectoptions) method. - 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() #### browser.targets()
- returns: <[Array]<[Target]>> - returns: <[Array]<[Target]>>

View File

@ -185,6 +185,13 @@ class Browser extends EventEmitter {
return Array.from(this._targets.values()).filter(target => target._isInitialized); 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>>} * @return {!Promise<!Array<!Puppeteer.Page>>}
*/ */

View File

@ -270,7 +270,7 @@ class Launcher {
} }
/** /**
* @param {!(BrowserOptions & {browserWSEndpoint: string})} options * @param {!(BrowserOptions & {browserWSEndpoint: string, transport?: !Puppeteer.ConnectionTransport})} options
* @return {!Promise<!Browser>} * @return {!Promise<!Browser>}
*/ */
async connect(options) { async connect(options) {
@ -278,9 +278,9 @@ class Launcher {
browserWSEndpoint, browserWSEndpoint,
ignoreHTTPSErrors = false, ignoreHTTPSErrors = false,
defaultViewport = {width: 800, height: 600}, defaultViewport = {width: 800, height: 600},
transport = await WebSocketTransport.create(browserWSEndpoint),
slowMo = 0, slowMo = 0,
} = options; } = options;
const transport = await WebSocketTransport.create(browserWSEndpoint);
const connection = new Connection(browserWSEndpoint, transport, slowMo); const connection = new Connection(browserWSEndpoint, transport, slowMo);
const {browserContextIds} = await connection.send('Target.getBrowserContexts'); const {browserContextIds} = await connection.send('Target.getBrowserContexts');
return Browser.create(connection, browserContextIds, ignoreHTTPSErrors, defaultViewport, null, () => connection.send('Browser.close').catch(debugError)); return Browser.create(connection, browserContextIds, ignoreHTTPSErrors, defaultViewport, null, () => connection.send('Browser.close').catch(debugError));

View File

@ -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>} * @return {!Promise<!Puppeteer.Browser>}
*/ */
connect(options) { connect(options) {

View File

@ -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() { describe('Browser.process', function() {
it('should return child_process instance', async function({browser}) { it('should return child_process instance', async function({browser}) {
const process = await browser.process(); const process = await browser.process();

View File

@ -80,6 +80,7 @@ ESTreeWalker._walkOrder = {
'ArrayExpression': ['elements'], 'ArrayExpression': ['elements'],
'ArrowFunctionExpression': ['params', 'body'], 'ArrowFunctionExpression': ['params', 'body'],
'AssignmentExpression': ['left', 'right'], 'AssignmentExpression': ['left', 'right'],
'AssignmentPattern': ['left', 'right'],
'BinaryExpression': ['left', 'right'], 'BinaryExpression': ['left', 'right'],
'BlockStatement': ['body'], 'BlockStatement': ['body'],
'BreakStatement': ['label'], 'BreakStatement': ['label'],

View File

@ -69,6 +69,28 @@ describe('Puppeteer-Web', () => {
]); ]);
await browser2.close(); 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()) { if (process.env.CI && testRunner.hasFocusedTestsOrSuites()) {