From 8e9c54a789c9d51f9861053b21565ad21a5ffe6f Mon Sep 17 00:00:00 2001 From: yujiosaka Date: Sat, 6 Jan 2018 03:14:30 +0900 Subject: [PATCH] feat: introduce browser.userAgent() (#1716) The patch introduces browser.userAgent() method to retrieve default browser user agent. Fixes #1706. --- docs/api.md | 6 ++++++ lib/Browser.js | 17 ++++++++++++++++- test/test.js | 8 ++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/api.md b/docs/api.md index f1fe7d7a..c4a839cb 100644 --- a/docs/api.md +++ b/docs/api.md @@ -24,6 +24,7 @@ * [browser.pages()](#browserpages) * [browser.process()](#browserprocess) * [browser.targets()](#browsertargets) + * [browser.userAgent()](#browseruseragent) * [browser.version()](#browserversion) * [browser.wsEndpoint()](#browserwsendpoint) - [class: Page](#class-page) @@ -362,6 +363,11 @@ Disconnects Puppeteer from the browser, but leaves the Chromium process running. #### browser.targets() - returns: <[Array]<[Target]>> An array of all active targets. +#### browser.userAgent() +- returns: <[Promise]<[string]>> Promise which resolves to the browser's original user agent. + +> **NOTE** Pages can override browser user agent with [page.setUserAgent](#pagesetuseragentuseragent) + #### browser.version() - returns: <[Promise]<[string]>> For headless Chromium, this is similar to `HeadlessChrome/61.0.3153.0`. For non-headless, this is similar to `Chrome/61.0.3153.0`. diff --git a/lib/Browser.js b/lib/Browser.js index fde8746a..06eca858 100644 --- a/lib/Browser.js +++ b/lib/Browser.js @@ -131,10 +131,18 @@ class Browser extends EventEmitter { * @return {!Promise} */ async version() { - const version = await this._connection.send('Browser.getVersion'); + const version = await this._getVersion(); return version.product; } + /** + * @return {!Promise} + */ + async userAgent() { + const version = await this._getVersion(); + return version.userAgent; + } + async close() { await this._closeCallback.call(null); this.disconnect(); @@ -143,6 +151,13 @@ class Browser extends EventEmitter { disconnect() { this._connection.dispose(); } + + /** + * @return {!Promise} + */ + _getVersion() { + return this._connection.send('Browser.getVersion'); + } } /** @enum {string} */ diff --git a/test/test.js b/test/test.js index 8d0bace6..0f7eb4d8 100644 --- a/test/test.js +++ b/test/test.js @@ -273,6 +273,14 @@ describe('Page', function() { }); }); + describe('Browser.userAgent', function() { + it('should include WebKit', async({browser}) => { + const userAgent = await browser.userAgent(); + expect(userAgent.length).toBeGreaterThan(0); + expect(userAgent).toContain('WebKit'); + }); + }); + describe('Browser.process', function() { it('should return child_process instance', async function({browser}) { const process = await browser.process();