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 all commits
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 @@ -111,6 +111,7 @@
* [page.emulateMedia(type)](#pageemulatemediatype)
* [page.emulateMediaFeatures(features)](#pageemulatemediafeaturesfeatures)
* [page.emulateMediaType(type)](#pageemulatemediatypetype)
* [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 @@ -1350,6 +1351,10 @@ await page.evaluate(() => matchMedia('print').matches));
// → true
```

#### 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 @@ -824,6 +824,19 @@ class Page extends EventEmitter {
}
}

/**
* @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
27 changes: 27 additions & 0 deletions test/emulation.spec.js
Expand Up @@ -157,4 +157,31 @@ module.exports.addTests = function({testRunner, expect, puppeteer}) {
});
});

describe_fails_ffox('Page.emulateTimezone', function() {
it('should work', async({page, server}) => {
page.evaluate(() => {
globalThis.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)');
});

it('should throw for invalid timezone IDs', async({page, server}) => {
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

};