Add Page.browser() method (#2387)

Call page.browser() to get the browser instance associated with a
page.

Fixes #2275
This commit is contained in:
Arne Martin Aurlien 2018-04-17 19:37:17 +02:00 committed by Andrey Lushnikov
parent 6eb6ce0ec9
commit 082b11aa57
5 changed files with 39 additions and 3 deletions

View File

@ -63,6 +63,7 @@
* [page.addStyleTag(options)](#pageaddstyletagoptions)
* [page.authenticate(credentials)](#pageauthenticatecredentials)
* [page.bringToFront()](#pagebringtofront)
* [page.browser()](#pagebrowser)
* [page.click(selector[, options])](#pageclickselector-options)
* [page.close()](#pageclose)
* [page.content()](#pagecontent)
@ -235,6 +236,7 @@
* [securityDetails.validFrom()](#securitydetailsvalidfrom)
* [securityDetails.validTo()](#securitydetailsvalidto)
- [class: Target](#class-target)
* [target.browser()](#targetbrowser)
* [target.createCDPSession()](#targetcreatecdpsession)
* [target.page()](#targetpage)
* [target.type()](#targettype)
@ -713,6 +715,12 @@ To disable authentication, pass `null`.
Brings page to front (activates tab).
#### page.browser()
- returns: <[Browser]>
Get the browser the page belongs to.
#### page.click(selector[, options])
- `selector` <[string]> A [selector] to search for element to click. If there are multiple elements satisfying the selector, the first will be clicked.
- `options` <[Object]>
@ -2553,6 +2561,12 @@ Contains the URL of the response.
### class: Target
#### target.browser()
- returns: <[Browser]>
Get the browser the target belongs to.
#### target.createCDPSession()
- returns: <[Promise]<[CDPSession]>>

View File

@ -68,7 +68,7 @@ class Browser extends EventEmitter {
*/
async _targetCreated(event) {
const targetInfo = event.targetInfo;
const target = new Target(targetInfo, () => this._connection.createSession(targetInfo.targetId), this._ignoreHTTPSErrors, !this._appMode, this._screenshotTaskQueue);
const target = new Target(targetInfo, this, () => this._connection.createSession(targetInfo.targetId), this._ignoreHTTPSErrors, !this._appMode, this._screenshotTaskQueue);
console.assert(!this._targets.has(event.targetInfo.targetId), 'Target should not exist before targetCreated');
this._targets.set(event.targetInfo.targetId, target);

View File

@ -114,6 +114,13 @@ class Page extends EventEmitter {
return this._target;
}
/**
* @return {!Puppeteer.Browser}
*/
browser() {
return this._target.browser();
}
_onTargetCrashed() {
this.emit('error', new Error('Page crashed!'));
}

View File

@ -4,13 +4,15 @@ const {helper} = require('./helper');
class Target {
/**
* @param {!Puppeteer.TargetInfo} targetInfo
* @param {!Puppeteer.Browser} browser
* @param {!function():!Promise<!Puppeteer.CDPSession>} sessionFactory
* @param {boolean} ignoreHTTPSErrors
* @param {boolean} setDefaultViewport
* @param {!Puppeteer.TaskQueue} screenshotTaskQueue
*/
constructor(targetInfo, sessionFactory, ignoreHTTPSErrors, setDefaultViewport, screenshotTaskQueue) {
constructor(targetInfo, browser, sessionFactory, ignoreHTTPSErrors, setDefaultViewport, screenshotTaskQueue) {
this._targetInfo = targetInfo;
this._browser = browser;
this._targetId = targetInfo.targetId;
this._sessionFactory = sessionFactory;
this._ignoreHTTPSErrors = ignoreHTTPSErrors;
@ -60,6 +62,13 @@ class Target {
return 'other';
}
/**
* @return {!Puppeteer.Browser}
*/
browser() {
return this._browser;
}
/**
* @param {!Puppeteer.TargetInfo} targetInfo
*/
@ -76,4 +85,4 @@ class Target {
helper.tracePublicAPI(Target);
module.exports = Target;
module.exports = Target;

View File

@ -1562,4 +1562,10 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
await closedPromise;
});
});
describe('Page.browser', function() {
it('should return the correct browser instance', async function({ page, browser }) {
expect(page.browser()).toBe(browser);
});
});
};