Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add page.emulateTimezone(timezoneId) #4949

Merged
merged 4 commits into from Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/api.md
Expand Up @@ -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)
Expand Down Expand Up @@ -1280,6 +1281,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. 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`
Expand Down
13 changes: 13 additions & 0 deletions lib/Page.js
Expand Up @@ -808,6 +808,19 @@ class Page extends EventEmitter {
await this._client.send('Emulation.setEmulatedMedia', {media: mediaType || ''});
}

/**
* @param {?string} timezoneId
*/
async emulateTimezone(timezoneId) {
try {
await this._client.send('Emulation.setTimezoneOverride', {timezoneId: timezoneId || ''});
} catch (exception) {
if (exception.message.includes('Invalid timezone'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Braces for multi-line if.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current puppeteer code style doesn't allow braces around single-line if bodies.

throw new Error(`Invalid timezone ID: ${timezoneId}`);
throw exception;
}
}

/**
* @param {!Puppeteer.Viewport} viewport
*/
Expand Down
25 changes: 25 additions & 0 deletions test/emulation.spec.js
Expand Up @@ -114,4 +114,29 @@ 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}) => {
const date = new Date(1479579154987);
await page.emulateTimezone('America/Jamaica');
expect(await page.evaluate(() => date.toString())).toBe('Sat Nov 19 2016 13:12:34 GMT-0500 (Eastern Standard Time)');

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

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

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

fit('should throw for invalid timezone IDs', async({page, server}) => {
mathiasbynens marked this conversation as resolved.
Show resolved Hide resolved
let error = null;
await page.emulateTimezone('Foo/Bar').catch(e => error = e);
expect(error.message).toBe('Invalid timezone ID: Foo/Bar');
await page.emulateTimezone('Baz/Qux').catch(e => error = e);
expect(error.message).toBe('Invalid timezone ID: Baz/Qux');
});
});
mathiasbynens marked this conversation as resolved.
Show resolved Hide resolved
};