Skip to content

Commit

Permalink
feat(api): add page.emulateTimezone(timezoneId)
Browse files Browse the repository at this point in the history
This enables dynamically changing the timezone of the page.
  • Loading branch information
mathiasbynens committed Sep 17, 2019
1 parent fff2737 commit 683bcd2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/api.md
Expand Up @@ -108,6 +108,7 @@
* [page.deleteCookie(...cookies)](#pagedeletecookiecookies)
* [page.emulate(options)](#pageemulateoptions)
* [page.emulateMedia(mediaType)](#pageemulatemediamediatype)
* [page.emulateTimezone(timezoneId)](#pageemulatetimezonetimezoneid)
* [page.evaluate(pageFunction[, ...args])](#pageevaluatepagefunction-args)
* [page.evaluateHandle(pageFunction[, ...args])](#pageevaluatehandlepagefunction-args)
* [page.evaluateOnNewDocument(pageFunction[, ...args])](#pageevaluateonnewdocumentpagefunction-args)
Expand Down Expand Up @@ -1279,6 +1280,10 @@ List of all available devices is available in the source code: [DeviceDescriptor
- `mediaType` <?[string]> Changes the CSS media type of the page. The only allowed values are `'screen'`, `'print'` and `null`. Passing `null` disables media emulation.
- returns: <[Promise]>

#### page.emulateTimezone(timezoneId)
- `timezoneId` <?[string]> Changes the timezone of the page. Passing `null` disables timezone emulation.
- returns: <[Promise]>

#### page.evaluate(pageFunction[, ...args])
- `pageFunction` <[function]|[string]> Function to be evaluated in the page context
- `...args` <...[Serializable]|[JSHandle]> Arguments to pass to `pageFunction`
Expand Down
7 changes: 7 additions & 0 deletions lib/Page.js
Expand Up @@ -807,6 +807,13 @@ class Page extends EventEmitter {
await this._client.send('Emulation.setEmulatedMedia', {media: mediaType || ''});
}

/**
* @param {?string} timezoneId
*/
async emulateTimezone(timezoneId) {
await this._client.send('Emulation.setTimezoneOverride', {timezoneId: timezoneId || ''});
}

/**
* @param {!Puppeteer.Viewport} viewport
*/
Expand Down
16 changes: 16 additions & 0 deletions test/emulation.spec.js
Expand Up @@ -114,4 +114,20 @@ module.exports.addTests = function({testRunner, expect, puppeteer}) {
expect(error.message).toBe('Unsupported media type: bad');
});
});

describe('Page.emulateTimezone', function() {
it('should work', async({page, server}) => {
await page.emulateTimezone('America/Jamaica');
expect(await page.evaluate(() => new Date(1479579154987).toString())).toBe('Sat Nov 19 2016 13:12:34 GMT-0500 (Eastern Standard Time)');

await page.emulateTimezone('Pacific/Honolulu');
expect(await page.evaluate(() => new Date(1479579154987).toString())).toBe('Sat Nov 19 2016 08:12:34 GMT-1000 (Hawaii-Aleutian Standard Time)');

await page.emulateTimezone('America/Argentina/Buenos_Aires');
expect(await page.evaluate(() => new Date(1479579154987).toString())).toBe('Sat Nov 19 2016 15:12:34 GMT-0300 (Argentina Standard Time)');

await page.emulateTimezone('Europe/Berlin');
expect(await page.evaluate(() => new Date(1479579154987).toString())).toBe('Sat Nov 19 2016 19:12:34 GMT+0100 (Central European Standard Time)');
});
});
};

0 comments on commit 683bcd2

Please sign in to comment.