From a8c9eeab0bfcf65c65ab5bcaa19ca483ac2f9e95 Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Tue, 17 Sep 2019 15:00:59 +0200 Subject: [PATCH] feat(api): add page.emulateTimezone(timezoneId) This enables dynamically changing the timezone of the page. --- docs/api.md | 5 +++++ lib/Page.js | 7 +++++++ test/emulation.spec.js | 16 ++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/docs/api.md b/docs/api.md index aa78657c39865..1d318429358a7 100644 --- a/docs/api.md +++ b/docs/api.md @@ -109,6 +109,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) @@ -1280,6 +1281,10 @@ List of all available devices is available in the source code: [DeviceDescriptor - `mediaType` 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` Changes the timezone of the page. See [ICU’s `metaZones.txt`](https://cs.chromium.org/chromium/src/third_party/icu/source/data/misc/metaZones.txt?rcl=faee8bc70570192d82d2978a71e2a615788597d1) for a list of supported timezone IDs. 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` diff --git a/lib/Page.js b/lib/Page.js index 0bdd217c8291f..ecc1790cf080e 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -808,6 +808,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 */ diff --git a/test/emulation.spec.js b/test/emulation.spec.js index 485394cc89251..cd9a40807e344 100644 --- a/test/emulation.spec.js +++ b/test/emulation.spec.js @@ -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)'); + }); + }); };