Skip to content

Commit

Permalink
feat(page): Introduce page.setGeolocation method (puppeteer#3160)
Browse files Browse the repository at this point in the history
  • Loading branch information
aslushnikov authored Aug 31, 2018
1 parent 1000997 commit 1702928
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Next Release: **Sep 6, 2018**
* [page.setCookie(...cookies)](#pagesetcookiecookies)
* [page.setDefaultNavigationTimeout(timeout)](#pagesetdefaultnavigationtimeouttimeout)
* [page.setExtraHTTPHeaders(headers)](#pagesetextrahttpheadersheaders)
* [page.setGeolocation(options)](#pagesetgeolocationoptions)
* [page.setJavaScriptEnabled(enabled)](#pagesetjavascriptenabledenabled)
* [page.setOfflineMode(enabled)](#pagesetofflinemodeenabled)
* [page.setRequestInterception(value)](#pagesetrequestinterceptionvalue)
Expand Down Expand Up @@ -1556,6 +1557,21 @@ The extra HTTP headers will be sent with every request the page initiates.

> **NOTE** page.setExtraHTTPHeaders does not guarantee the order of headers in the outgoing requests.
#### page.setGeolocation(options)
- `options`
- `latitude` <[number]> Latitude between -90 and 90.
- `longitude` <[number]> Longitude between -180 and 180.
- `accuracy` <[number]> Optional non-negative accuracy value.
- returns: <[Promise]>

Sets the page's geolocation.

```js
await page.setGeolocation({latitude: 59.95, longitude: 30.31667});
```

> **NOTE** Consider using [browserContext.overridePermissions](#browsercontextoverridepermissionsorigin-permissions) to grant permissions for the page to read its geolocation.
#### page.setJavaScriptEnabled(enabled)
- `enabled` <[boolean]> Whether or not to enable JavaScript on the page.
- returns: <[Promise]>
Expand Down
14 changes: 14 additions & 0 deletions lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ class Page extends EventEmitter {
});
}

/**
* @param {!{longitude: number, latitude: number, accuracy: (number|undefined)}} options
*/
async setGeolocation(options) {
const { longitude, latitude, accuracy = 0} = options;
if (longitude < -180 || longitude > 180)
throw new Error(`Invalid longitude "${longitude}": precondition -180 <= LONGITUDE <= 180 failed.`);
if (latitude < -90 || latitude > 90)
throw new Error(`Invalid latitude "${latitude}": precondition -90 <= LATITUDE <= 90 failed.`);
if (accuracy < 0)
throw new Error(`Invalid accuracy "${accuracy}": precondition 0 <= ACCURACY failed.`);
await this._client.send('Emulation.setGeolocationOverride', {longitude, latitude, accuracy});
}

/**
* @return {!Puppeteer.Target}
*/
Expand Down
24 changes: 24 additions & 0 deletions test/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ module.exports.addTests = function({testRunner, expect, headless}) {
});
});

describe('Page.setGeolocation', function() {
it('should work', async({page, server, context}) => {
await context.overridePermissions(server.PREFIX, ['geolocation']);
await page.goto(server.EMPTY_PAGE);
await page.setGeolocation({longitude: 10, latitude: 10});
const geolocation = await page.evaluate(() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => {
resolve({latitude: position.coords.latitude, longitude: position.coords.longitude});
})));
expect(geolocation).toEqual({
latitude: 10,
longitude: 10
});
});
it('should throw when invalid longitude', async({page, server, context}) => {
let error = null;
try {
await page.setGeolocation({longitude: 200, latitude: 10});
} catch (e) {
error = e;
}
expect(error.message).toContain('Invalid longitude "200"');
});
});

describe('Page.evaluate', function() {
it('should work', async({page, server}) => {
const result = await page.evaluate(() => 7 * 3);
Expand Down

0 comments on commit 1702928

Please sign in to comment.