feat(geo): implement geo override in ff (#1438)

This commit is contained in:
Pavel Feldman 2020-03-20 19:17:46 -07:00 committed by GitHub
parent 840e69b85c
commit c539325615
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 8 deletions

View file

@ -9,7 +9,7 @@
"main": "index.js", "main": "index.js",
"playwright": { "playwright": {
"chromium_revision": "751710", "chromium_revision": "751710",
"firefox_revision": "1043", "firefox_revision": "1044",
"webkit_revision": "1180" "webkit_revision": "1180"
}, },
"scripts": { "scripts": {

View file

@ -268,8 +268,6 @@ export class CRBrowserContext extends BrowserContextBase {
async _initialize() { async _initialize() {
if (this._options.permissions) if (this._options.permissions)
await this.grantPermissions(this._options.permissions); await this.grantPermissions(this._options.permissions);
if (this._options.geolocation)
await this.setGeolocation(this._options.geolocation);
if (this._options.offline) if (this._options.offline)
await this.setOffline(this._options.offline); await this.setOffline(this._options.offline);
if (this._options.httpCredentials) if (this._options.httpCredentials)

View file

@ -16,7 +16,7 @@
*/ */
import { Browser, createPageInNewContext } from '../browser'; 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 { Events } from '../events';
import { assert, helper, RegisteredListener } from '../helper'; import { assert, helper, RegisteredListener } from '../helper';
import * as network from '../network'; import * as network from '../network';
@ -170,8 +170,6 @@ export class FFBrowserContext extends BrowserContextBase {
async _initialize() { async _initialize() {
if (this._options.permissions) if (this._options.permissions)
await this.grantPermissions(this._options.permissions); await this.grantPermissions(this._options.permissions);
if (this._options.geolocation)
await this.setGeolocation(this._options.geolocation);
if (this._options.extraHTTPHeaders) if (this._options.extraHTTPHeaders)
await this.setExtraHTTPHeaders(this._options.extraHTTPHeaders); await this.setExtraHTTPHeaders(this._options.extraHTTPHeaders);
if (this._options.offline) if (this._options.offline)
@ -250,7 +248,11 @@ export class FFBrowserContext extends BrowserContextBase {
} }
async setGeolocation(geolocation: types.Geolocation | null): Promise<void> { 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> { async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {

View file

@ -86,6 +86,7 @@ export class FFPage implements PageDelegate {
} }
async _initialize() { async _initialize() {
const geolocation = this._browserContext._options.geolocation;
try { try {
await Promise.all([ await Promise.all([
// TODO: we should get rid of this call to resolve before any early events arrive, e.g. dialogs. // TODO: we should get rid of this call to resolve before any early events arrive, e.g. dialogs.
@ -93,6 +94,7 @@ export class FFPage implements PageDelegate {
script: '', script: '',
worldName: UTILITY_WORLD_NAME, worldName: UTILITY_WORLD_NAME,
}), }),
geolocation ? this._setGeolocation(geolocation) : Promise.resolve(),
new Promise(f => this._session.once('Page.ready', f)), new Promise(f => this._session.once('Page.ready', f)),
]); ]);
this._pageCallback(this._page); this._pageCallback(this._page);
@ -481,6 +483,10 @@ export class FFPage implements PageDelegate {
throw new Error('Frame has been detached.'); throw new Error('Frame has been detached.');
return result.handle; return result.handle;
} }
async _setGeolocation(geolocation: types.Geolocation | null) {
await this._session.send('Page.setGeolocationOverride', geolocation || {});
}
} }
function toRemoteObject(handle: dom.ElementHandle): Protocol.Runtime.RemoteObject { function toRemoteObject(handle: dom.ElementHandle): Protocol.Runtime.RemoteObject {

View file

@ -23,7 +23,7 @@ module.exports.describe = function ({ testRunner, expect, FFOX, WEBKIT }) {
const {it, fit, xit, dit} = testRunner; const {it, fit, xit, dit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
describe.fail(FFOX)('Overrides.setGeolocation', function() { describe('Overrides.setGeolocation', function() {
it('should work', async({page, server, context}) => { it('should work', async({page, server, context}) => {
await context.grantPermissions(['geolocation']); await context.grantPermissions(['geolocation']);
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
@ -87,5 +87,30 @@ module.exports.describe = function ({ testRunner, expect, FFOX, WEBKIT }) {
}); });
await context.close(); 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');
});
}); });
}; };