diff --git a/docs/api.md b/docs/api.md index c9b4735cf5..426bc41630 100644 --- a/docs/api.md +++ b/docs/api.md @@ -60,8 +60,9 @@ * [browserContext.setCookies(cookies)](#browsercontextsetcookiescookies) * [browserContext.targets()](#browsercontexttargets) * [browserContext.waitForTarget(predicate[, options])](#browsercontextwaitfortargetpredicate-options) -- [class: Geolocation](#class-geolocation) - * [geolocation.set(options)](#geolocationsetoptions) +- [class: Overrides](#class-overrides) + * [overrides.setGeolocation(options)](#overridessetgeolocationoptions) + * [overrides.setTimezone(timezoneId)](#overridessettimezonetimezoneid) - [class: Permissions](#class-permissions) * [permissions.clearOverrides()](#permissionsclearoverrides) * [permissions.override(origin, permissions)](#permissionsoverrideorigin-permissions) @@ -97,7 +98,6 @@ * [page.dblclick(selector[, options])](#pagedblclickselector-options) * [page.emulate(options)](#pageemulateoptions) * [page.emulateMedia(options)](#pageemulatemediaoptions) - * [page.emulateTimezone(timezoneId)](#pageemulatetimezonetimezoneid) * [page.evaluate(pageFunction[, ...args])](#pageevaluatepagefunction-args) * [page.evaluateHandle(pageFunction[, ...args])](#pageevaluatehandlepagefunction-args) * [page.evaluateOnNewDocument(pageFunction[, ...args])](#pageevaluateonnewdocumentpagefunction-args) @@ -105,7 +105,6 @@ * [page.fill(selector, value)](#pagefillselector-value) * [page.focus(selector)](#pagefocusselector) * [page.frames()](#pageframes) - * [page.geolocation](#pagegeolocation) * [page.goBack([options])](#pagegobackoptions) * [page.goForward([options])](#pagegoforwardoptions) * [page.goto(url[, options])](#pagegotourl-options) @@ -115,6 +114,7 @@ * [page.keyboard](#pagekeyboard) * [page.mainFrame()](#pagemainframe) * [page.mouse](#pagemouse) + * [page.overrides](#pageoverrides) * [page.pdf](#pagepdf) * [page.reload([options])](#pagereloadoptions) * [page.screenshot([options])](#pagescreenshotoptions) @@ -844,9 +844,9 @@ await page.evaluate(() => window.open('https://www.example.com/')); const newWindowTarget = await browserContext.waitForTarget(target => target.url() === 'https://www.example.com/'); ``` -### class: Geolocation +### class: Overrides -#### geolocation.set(options) +#### overrides.setGeolocation(options) - `options` <[Object]> - `latitude` <[number]> Latitude between -90 and 90. - `longitude` <[number]> Longitude between -180 and 180. @@ -856,11 +856,14 @@ const newWindowTarget = await browserContext.waitForTarget(target => target.url( Sets the page's geolocation. ```js -await page.geolocation.set({latitude: 59.95, longitude: 30.31667}); +await page.overrides.setGeolocation({latitude: 59.95, longitude: 30.31667}); ``` > **NOTE** Consider using [browserContext.permissions.override](#permissionsoverrideorigin-permissions) to grant permissions for the page to read its geolocation. +#### overrides.setTimezone(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]> ### class: Permissions @@ -1269,10 +1272,6 @@ await page.evaluate(() => matchMedia('(prefers-color-scheme: no-preference)').ma // → false ``` -#### 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` @@ -1443,9 +1442,6 @@ Shortcut for [page.mainFrame().focus(selector)](#framefocusselector). #### page.frames() - returns: <[Array]<[Frame]>> An array of all frames attached to the page. -#### page.geolocation -- returns: <[Geolocation]> - #### page.goBack([options]) - `options` <[Object]> Navigation parameters which might have the following properties: - `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the [page.setDefaultNavigationTimeout(timeout)](#pagesetdefaultnavigationtimeouttimeout) or [page.setDefaultTimeout(timeout)](#pagesetdefaulttimeouttimeout) methods. @@ -1535,6 +1531,9 @@ Page is guaranteed to have a main frame which persists during navigations. - returns: <[Mouse]> +#### page.overrides +- returns: <[Overrides]> + #### page.pdf - returns: <[PDF]> diff --git a/src/chromium/Page.ts b/src/chromium/Page.ts index de469d8c45..d34c03cdfd 100644 --- a/src/chromium/Page.ts +++ b/src/chromium/Page.ts @@ -30,7 +30,7 @@ import { EmulationManager } from './EmulationManager'; import { Events } from './events'; import { Accessibility } from './features/accessibility'; import { Coverage } from './features/coverage'; -import { Geolocation } from './features/geolocation'; +import { Overrides } from './features/overrides'; import { Interception } from './features/interception'; import { PDF } from './features/pdf'; import { Workers } from './features/workers'; @@ -68,7 +68,7 @@ export class Page extends EventEmitter { private _emulationManager: EmulationManager; readonly accessibility: Accessibility; readonly coverage: Coverage; - readonly geolocation: Geolocation; + readonly overrides: Overrides; readonly interception: Interception; readonly pdf: PDF; readonly workers: Workers; @@ -102,7 +102,7 @@ export class Page extends EventEmitter { this.coverage = new Coverage(client); this.pdf = new PDF(client); this.workers = new Workers(client, this._addConsoleMessage.bind(this), this._handleException.bind(this)); - this.geolocation = new Geolocation(client); + this.overrides = new Overrides(client); this.interception = new Interception(this._frameManager.networkManager()); this._screenshotTaskQueue = screenshotTaskQueue; @@ -501,16 +501,6 @@ export class Page extends EventEmitter { this._emulatedMediaType = options.type; } - async emulateTimezone(timezoneId: string | null) { - try { - await this._client.send('Emulation.setTimezoneOverride', {timezoneId: timezoneId || ''}); - } catch (exception) { - if (exception.message.includes('Invalid timezone')) - throw new Error(`Invalid timezone ID: ${timezoneId}`); - throw exception; - } - } - async setViewport(viewport: Viewport) { const needsReload = await this._emulationManager.emulateViewport(viewport); this._viewport = viewport; diff --git a/src/chromium/api.ts b/src/chromium/api.ts index 30620936d7..0fe5e9072e 100644 --- a/src/chromium/api.ts +++ b/src/chromium/api.ts @@ -11,7 +11,7 @@ export { Dialog } from './Dialog'; export { ExecutionContext } from './ExecutionContext'; export { Accessibility } from './features/accessibility'; export { Coverage } from './features/coverage'; -export { Geolocation } from './features/geolocation'; +export { Overrides } from './features/overrides'; export { Interception } from './features/interception'; export { PDF } from './features/pdf'; export { Permissions } from './features/permissions'; diff --git a/src/chromium/features/geolocation.spec.js b/src/chromium/features/geolocation.spec.js index 4560514c74..7c2d0e8a40 100644 --- a/src/chromium/features/geolocation.spec.js +++ b/src/chromium/features/geolocation.spec.js @@ -25,11 +25,11 @@ module.exports.addTests = function ({ testRunner, expect }) { // FIXME: not supported in WebKit (as well as Emulation domain in general). // It was removed from WebKit in https://webkit.org/b/126630 - describe('Geolocation.set', function() { + describe('Overrides.setGeolocation', function() { it('should work', async({page, server, context}) => { await context.permissions.override(server.PREFIX, ['geolocation']); await page.goto(server.EMPTY_PAGE); - await page.geolocation.set({longitude: 10, latitude: 10}); + await page.overrides.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}); }))); @@ -41,7 +41,7 @@ module.exports.addTests = function ({ testRunner, expect }) { it('should throw when invalid longitude', async({page, server, context}) => { let error = null; try { - await page.geolocation.set({longitude: 200, latitude: 10}); + await page.overrides.setGeolocation({longitude: 200, latitude: 10}); } catch (e) { error = e; } diff --git a/src/chromium/features/geolocation.ts b/src/chromium/features/overrides.ts similarity index 74% rename from src/chromium/features/geolocation.ts rename to src/chromium/features/overrides.ts index a4171ca098..fec511f526 100644 --- a/src/chromium/features/geolocation.ts +++ b/src/chromium/features/overrides.ts @@ -17,14 +17,14 @@ import { CDPSession } from '../Connection'; -export class Geolocation { +export class Overrides { private _client: CDPSession; constructor(client: CDPSession) { this._client = client; } - async set(options: { longitude: number; latitude: number; accuracy: (number | undefined); }) { + async setGeolocation(options: { longitude: number; latitude: number; accuracy: (number | undefined); }) { const { longitude, latitude, accuracy = 0} = options; if (longitude < -180 || longitude > 180) throw new Error(`Invalid longitude "${longitude}": precondition -180 <= LONGITUDE <= 180 failed.`); @@ -34,4 +34,14 @@ export class Geolocation { throw new Error(`Invalid accuracy "${accuracy}": precondition 0 <= ACCURACY failed.`); await this._client.send('Emulation.setGeolocationOverride', {longitude, latitude, accuracy}); } + + async setTimezone(timezoneId: string | null) { + try { + await this._client.send('Emulation.setTimezoneOverride', {timezoneId: timezoneId || ''}); + } catch (exception) { + if (exception.message.includes('Invalid timezone')) + throw new Error(`Invalid timezone ID: ${timezoneId}`); + throw exception; + } + } } diff --git a/test/emulation.spec.js b/test/emulation.spec.js index dddb3c8c3e..3c33e44a4a 100644 --- a/test/emulation.spec.js +++ b/test/emulation.spec.js @@ -139,29 +139,29 @@ module.exports.addTests = function({testRunner, expect, playwright, FFOX, CHROME }); }); - describe.skip(FFOX || WEBKIT)('Page.emulateTimezone', function() { + describe.skip(FFOX || WEBKIT)('Overrides.setTimezone', function() { it('should work', async({page, server}) => { page.evaluate(() => { globalThis.date = new Date(1479579154987); }); - await page.emulateTimezone('America/Jamaica'); + await page.overrides.setTimezone('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'); + await page.overrides.setTimezone('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'); + await page.overrides.setTimezone('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'); + await page.overrides.setTimezone('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); + await page.overrides.setTimezone('Foo/Bar').catch(e => error = e); expect(error.message).toBe('Invalid timezone ID: Foo/Bar'); - await page.emulateTimezone('Baz/Qux').catch(e => error = e); + await page.overrides.setTimezone('Baz/Qux').catch(e => error = e); expect(error.message).toBe('Invalid timezone ID: Baz/Qux'); }); });