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(geo): implement geo override in ff #1438

Merged
merged 1 commit into from
Mar 21, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes popups not have geolocation override at the start. We should push to the browser context instead.

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');
});
});
};