feat: introduce browser.userAgent() (#1716)

The patch introduces browser.userAgent() method to retrieve
default browser user agent.

Fixes #1706.
This commit is contained in:
yujiosaka 2018-01-06 03:14:30 +09:00 committed by Andrey Lushnikov
parent 05b1aca21e
commit 8e9c54a789
3 changed files with 30 additions and 1 deletions

View File

@ -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`.

View File

@ -131,10 +131,18 @@ class Browser extends EventEmitter {
* @return {!Promise<string>}
*/
async version() {
const version = await this._connection.send('Browser.getVersion');
const version = await this._getVersion();
return version.product;
}
/**
* @return {!Promise<string>}
*/
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<!Object>}
*/
_getVersion() {
return this._connection.send('Browser.getVersion');
}
}
/** @enum {string} */

View File

@ -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();