Skip to content

Commit 17b1861

Browse files
committed
feat(geo): implement geo override in ff
1 parent 840e69b commit 17b1861

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"main": "index.js",
1010
"playwright": {
1111
"chromium_revision": "751710",
12-
"firefox_revision": "1043",
12+
"firefox_revision": "1044",
1313
"webkit_revision": "1180"
1414
},
1515
"scripts": {

src/chromium/crBrowser.ts

-2
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,6 @@ export class CRBrowserContext extends BrowserContextBase {
268268
async _initialize() {
269269
if (this._options.permissions)
270270
await this.grantPermissions(this._options.permissions);
271-
if (this._options.geolocation)
272-
await this.setGeolocation(this._options.geolocation);
273271
if (this._options.offline)
274272
await this.setOffline(this._options.offline);
275273
if (this._options.httpCredentials)

src/firefox/ffBrowser.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { Browser, createPageInNewContext } from '../browser';
19-
import { assertBrowserContextIsNotOwned, BrowserContext, BrowserContextBase, BrowserContextOptions, validateBrowserContextOptions } from '../browserContext';
19+
import { assertBrowserContextIsNotOwned, BrowserContext, BrowserContextBase, BrowserContextOptions, validateBrowserContextOptions, verifyGeolocation } from '../browserContext';
2020
import { Events } from '../events';
2121
import { assert, helper, RegisteredListener } from '../helper';
2222
import * as network from '../network';
@@ -170,8 +170,6 @@ export class FFBrowserContext extends BrowserContextBase {
170170
async _initialize() {
171171
if (this._options.permissions)
172172
await this.grantPermissions(this._options.permissions);
173-
if (this._options.geolocation)
174-
await this.setGeolocation(this._options.geolocation);
175173
if (this._options.extraHTTPHeaders)
176174
await this.setExtraHTTPHeaders(this._options.extraHTTPHeaders);
177175
if (this._options.offline)
@@ -250,7 +248,11 @@ export class FFBrowserContext extends BrowserContextBase {
250248
}
251249

252250
async setGeolocation(geolocation: types.Geolocation | null): Promise<void> {
253-
throw new Error('Geolocation emulation is not supported in Firefox');
251+
if (geolocation)
252+
geolocation = verifyGeolocation(geolocation);
253+
this._options.geolocation = geolocation || undefined;
254+
for (const page of this.pages())
255+
await (page._delegate as FFPage)._setGeolocation(geolocation);
254256
}
255257

256258
async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {

src/firefox/ffPage.ts

+6
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,15 @@ export class FFPage implements PageDelegate {
8686
}
8787

8888
async _initialize() {
89+
const geolocation = this._browserContext._options.geolocation;
8990
try {
9091
await Promise.all([
9192
// TODO: we should get rid of this call to resolve before any early events arrive, e.g. dialogs.
9293
this._session.send('Page.addScriptToEvaluateOnNewDocument', {
9394
script: '',
9495
worldName: UTILITY_WORLD_NAME,
9596
}),
97+
geolocation ? this._setGeolocation(geolocation) : Promise.resolve(),
9698
new Promise(f => this._session.once('Page.ready', f)),
9799
]);
98100
this._pageCallback(this._page);
@@ -481,6 +483,10 @@ export class FFPage implements PageDelegate {
481483
throw new Error('Frame has been detached.');
482484
return result.handle;
483485
}
486+
487+
async _setGeolocation(geolocation: types.Geolocation | null) {
488+
await this._session.send('Page.setGeolocationOverride', geolocation || {});
489+
}
484490
}
485491

486492
function toRemoteObject(handle: dom.ElementHandle): Protocol.Runtime.RemoteObject {

test/geolocation.spec.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports.describe = function ({ testRunner, expect, FFOX, WEBKIT }) {
2323
const {it, fit, xit, dit} = testRunner;
2424
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
2525

26-
describe.fail(FFOX)('Overrides.setGeolocation', function() {
26+
describe('Overrides.setGeolocation', function() {
2727
it('should work', async({page, server, context}) => {
2828
await context.grantPermissions(['geolocation']);
2929
await page.goto(server.EMPTY_PAGE);
@@ -87,5 +87,30 @@ module.exports.describe = function ({ testRunner, expect, FFOX, WEBKIT }) {
8787
});
8888
await context.close();
8989
});
90+
it('watchPosition should be notified', async({page, server, context}) => {
91+
await context.grantPermissions(['geolocation']);
92+
await page.goto(server.EMPTY_PAGE);
93+
const messages = [];
94+
page.on('console', message => messages.push(message.text()));
95+
96+
await context.setGeolocation({latitude: 0, longitude: 0});
97+
await page.evaluate(() => {
98+
navigator.geolocation.watchPosition(pos => {
99+
const coords = pos.coords;
100+
console.log(`lat=${coords.latitude} lng=${coords.longitude}`);
101+
}, err => {});
102+
});
103+
await context.setGeolocation({latitude: 0, longitude: 10});
104+
await page.waitForEvent('console', message => message.text().includes('lat=0 lng=10'));
105+
await context.setGeolocation({latitude: 20, longitude: 30});
106+
await page.waitForEvent('console', message => message.text().includes('lat=20 lng=30'));
107+
await context.setGeolocation({latitude: 40, longitude: 50});
108+
await page.waitForEvent('console', message => message.text().includes('lat=40 lng=50'));
109+
110+
const allMessages = messages.join('|');
111+
expect(allMessages).toContain('lat=0 lng=10');
112+
expect(allMessages).toContain('lat=20 lng=30');
113+
expect(allMessages).toContain('lat=40 lng=50');
114+
});
90115
});
91116
};

0 commit comments

Comments
 (0)