Skip to content

Commit

Permalink
feat(geo): implement geo override in ff (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Mar 21, 2020
1 parent 840e69b commit c539325
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"main": "index.js",
"playwright": {
"chromium_revision": "751710",
"firefox_revision": "1043",
"firefox_revision": "1044",
"webkit_revision": "1180"
},
"scripts": {
Expand Down
2 changes: 0 additions & 2 deletions src/chromium/crBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,6 @@ export class CRBrowserContext extends BrowserContextBase {
async _initialize() {
if (this._options.permissions)
await this.grantPermissions(this._options.permissions);
if (this._options.geolocation)
await this.setGeolocation(this._options.geolocation);
if (this._options.offline)
await this.setOffline(this._options.offline);
if (this._options.httpCredentials)
Expand Down
10 changes: 6 additions & 4 deletions src/firefox/ffBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import { Browser, createPageInNewContext } from '../browser';
import { assertBrowserContextIsNotOwned, BrowserContext, BrowserContextBase, BrowserContextOptions, validateBrowserContextOptions } from '../browserContext';
import { assertBrowserContextIsNotOwned, BrowserContext, BrowserContextBase, BrowserContextOptions, validateBrowserContextOptions, verifyGeolocation } from '../browserContext';
import { Events } from '../events';
import { assert, helper, RegisteredListener } from '../helper';
import * as network from '../network';
Expand Down Expand Up @@ -170,8 +170,6 @@ export class FFBrowserContext extends BrowserContextBase {
async _initialize() {
if (this._options.permissions)
await this.grantPermissions(this._options.permissions);
if (this._options.geolocation)
await this.setGeolocation(this._options.geolocation);
if (this._options.extraHTTPHeaders)
await this.setExtraHTTPHeaders(this._options.extraHTTPHeaders);
if (this._options.offline)
Expand Down Expand Up @@ -250,7 +248,11 @@ export class FFBrowserContext extends BrowserContextBase {
}

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

async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {
Expand Down
6 changes: 6 additions & 0 deletions src/firefox/ffPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ export class FFPage implements PageDelegate {
}

async _initialize() {
const geolocation = this._browserContext._options.geolocation;
try {
await Promise.all([
// TODO: we should get rid of this call to resolve before any early events arrive, e.g. dialogs.
this._session.send('Page.addScriptToEvaluateOnNewDocument', {
script: '',
worldName: UTILITY_WORLD_NAME,
}),
geolocation ? this._setGeolocation(geolocation) : Promise.resolve(),
new Promise(f => this._session.once('Page.ready', f)),
]);
this._pageCallback(this._page);
Expand Down Expand Up @@ -481,6 +483,10 @@ export class FFPage implements PageDelegate {
throw new Error('Frame has been detached.');
return result.handle;
}

async _setGeolocation(geolocation: types.Geolocation | null) {
await this._session.send('Page.setGeolocationOverride', geolocation || {});
}
}

function toRemoteObject(handle: dom.ElementHandle): Protocol.Runtime.RemoteObject {
Expand Down
27 changes: 26 additions & 1 deletion test/geolocation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports.describe = function ({ testRunner, expect, FFOX, WEBKIT }) {
const {it, fit, xit, dit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;

describe.fail(FFOX)('Overrides.setGeolocation', function() {
describe('Overrides.setGeolocation', function() {
it('should work', async({page, server, context}) => {
await context.grantPermissions(['geolocation']);
await page.goto(server.EMPTY_PAGE);
Expand Down Expand Up @@ -87,5 +87,30 @@ module.exports.describe = function ({ testRunner, expect, FFOX, WEBKIT }) {
});
await context.close();
});
it('watchPosition should be notified', async({page, server, context}) => {
await context.grantPermissions(['geolocation']);
await page.goto(server.EMPTY_PAGE);
const messages = [];
page.on('console', message => messages.push(message.text()));

await context.setGeolocation({latitude: 0, longitude: 0});
await page.evaluate(() => {
navigator.geolocation.watchPosition(pos => {
const coords = pos.coords;
console.log(`lat=${coords.latitude} lng=${coords.longitude}`);
}, err => {});
});
await context.setGeolocation({latitude: 0, longitude: 10});
await page.waitForEvent('console', message => message.text().includes('lat=0 lng=10'));
await context.setGeolocation({latitude: 20, longitude: 30});
await page.waitForEvent('console', message => message.text().includes('lat=20 lng=30'));
await context.setGeolocation({latitude: 40, longitude: 50});
await page.waitForEvent('console', message => message.text().includes('lat=40 lng=50'));

const allMessages = messages.join('|');
expect(allMessages).toContain('lat=0 lng=10');
expect(allMessages).toContain('lat=20 lng=30');
expect(allMessages).toContain('lat=40 lng=50');
});
});
};

0 comments on commit c539325

Please sign in to comment.