diff --git a/docs/api.md b/docs/api.md index 0335473ebd1..383c60de901 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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]>> diff --git a/lib/Browser.js b/lib/Browser.js index f33c1c04a49..77a67fdcba7 100644 --- a/lib/Browser.js +++ b/lib/Browser.js @@ -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); diff --git a/lib/Page.js b/lib/Page.js index a8384d61aa9..632390b4712 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -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!')); } diff --git a/lib/Target.js b/lib/Target.js index 1b8f606cd5e..4de01b4b350 100644 --- a/lib/Target.js +++ b/lib/Target.js @@ -4,13 +4,15 @@ const {helper} = require('./helper'); class Target { /** * @param {!Puppeteer.TargetInfo} targetInfo + * @param {!Puppeteer.Browser} browser * @param {!function():!Promise} 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; \ No newline at end of file +module.exports = Target; diff --git a/test/page.spec.js b/test/page.spec.js index b98d27d1a09..d6e82ee2f7a 100644 --- a/test/page.spec.js +++ b/test/page.spec.js @@ -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); + }); + }); };