Skip to content
This repository has been archived by the owner on Jan 15, 2020. It is now read-only.

Commit

Permalink
feat: introduce browser.userAgent() (puppeteer#1716)
Browse files Browse the repository at this point in the history
The patch introduces browser.userAgent() method to retrieve
default browser user agent.

Fixes puppeteer#1706.
  • Loading branch information
yujiosaka authored and aslushnikov committed Jan 5, 2018
1 parent 05b1aca commit 8e9c54a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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`.

Expand Down
17 changes: 16 additions & 1 deletion lib/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -143,6 +151,13 @@ class Browser extends EventEmitter {
disconnect() {
this._connection.dispose();
}

/**
* @return {!Promise<!Object>}
*/
_getVersion() {
return this._connection.send('Browser.getVersion');
}
}

/** @enum {string} */
Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 8e9c54a

Please sign in to comment.