Skip to content

Commit 6e8895f

Browse files
authored
fix(firefox): make interception, locale and geolocation work on browser context level (#1472)
1 parent 93954fe commit 6e8895f

8 files changed

+25
-17
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": "1047",
12+
"firefox_revision": "1048",
1313
"webkit_revision": "1182"
1414
},
1515
"scripts": {

src/firefox/ffBrowser.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export class FFBrowser extends platform.EventEmitter implements Browser {
9494
bypassCSP: options.bypassCSP,
9595
javaScriptDisabled: options.javaScriptEnabled === false ? true : undefined,
9696
viewport,
97+
locale: options.locale,
9798
removeOnDetach: true
9899
});
99100
// TODO: move ignoreHTTPSErrors to browser context level.
@@ -174,6 +175,8 @@ export class FFBrowserContext extends BrowserContextBase {
174175
await this.setOffline(this._options.offline);
175176
if (this._options.httpCredentials)
176177
await this.setHTTPCredentials(this._options.httpCredentials);
178+
if (this._options.geolocation)
179+
await this.setGeolocation(this._options.geolocation);
177180
}
178181

179182
_ffPages(): FFPage[] {
@@ -249,8 +252,7 @@ export class FFBrowserContext extends BrowserContextBase {
249252
if (geolocation)
250253
geolocation = verifyGeolocation(geolocation);
251254
this._options.geolocation = geolocation || undefined;
252-
for (const page of this.pages())
253-
await (page._delegate as FFPage)._setGeolocation(geolocation);
255+
await this._browser._connection.send('Browser.setGeolocationOverride', { browserContextId: this._browserContextId || undefined, geolocation });
254256
}
255257

256258
async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {
@@ -292,8 +294,8 @@ export class FFBrowserContext extends BrowserContextBase {
292294

293295
async route(url: types.URLMatch, handler: network.RouteHandler): Promise<void> {
294296
this._routes.push({ url, handler });
295-
throw new Error('Not implemented');
296-
// TODO: update interception on the context if this is a first route.
297+
if (this._routes.length === 1)
298+
await this._browser._connection.send('Browser.setRequestInterception', { browserContextId: this._browserContextId || undefined, enabled: true });
297299
}
298300

299301
async close() {

src/firefox/ffPage.ts

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

8888
async _initialize() {
89-
const geolocation = this._browserContext._options.geolocation;
90-
const language = this._browserContext._options.locale;
9189
try {
9290
await Promise.all([
9391
// TODO: we should get rid of this call to resolve before any early events arrive, e.g. dialogs.
9492
this._session.send('Page.addScriptToEvaluateOnNewDocument', {
9593
script: '',
9694
worldName: UTILITY_WORLD_NAME,
9795
}),
98-
geolocation ? this._setGeolocation(geolocation) : Promise.resolve(),
99-
language ? this._session.send('Page.setLanguageOverride', { language }) : Promise.resolve(),
10096
new Promise(f => this._session.once('Page.ready', f)),
10197
]);
10298
this._pageCallback(this._page);
@@ -485,10 +481,6 @@ export class FFPage implements PageDelegate {
485481
throw new Error('Frame has been detached.');
486482
return result.handle;
487483
}
488-
489-
async _setGeolocation(geolocation: types.Geolocation | null) {
490-
await this._session.send('Page.setGeolocationOverride', geolocation || {});
491-
}
492484
}
493485

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

test/assets/formatted-number.html

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<script>
22
window.result = (1000000.50).toLocaleString().replace(/\s/g, ' ');
3+
window.initialNavigatorLanguage = navigator.language;
34
</script>

test/browsercontext.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, FF
367367
});
368368
});
369369

370-
describe.fail(FFOX)('BrowserContext.route', () => {
370+
describe('BrowserContext.route', () => {
371371
it('should intercept', async({browser, server}) => {
372372
const context = await browser.newContext();
373373
let intercepted = false;

test/emulation.spec.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ module.exports.describe = function({testRunner, expect, playwright, headless, FF
342342
await context.close();
343343
}
344344
});
345-
it.fail(CHROMIUM || FFOX)('should apply to popups', async({browser, server}) => {
345+
it.fail(CHROMIUM || FFOX)('should format number in popups', async({browser, server}) => {
346346
const context = await browser.newContext({ locale: 'fr-CH' });
347347
const page = await context.newPage();
348348
await page.goto(server.EMPTY_PAGE);
@@ -356,6 +356,19 @@ module.exports.describe = function({testRunner, expect, playwright, headless, FF
356356
expect(result).toBe('1 000 000,5');
357357
await context.close();
358358
});
359+
it.fail(CHROMIUM)('should affect navigator.language in popups', async({browser, server}) => {
360+
const context = await browser.newContext({ locale: 'fr-CH' });
361+
const page = await context.newPage();
362+
await page.goto(server.EMPTY_PAGE);
363+
const [popup] = await Promise.all([
364+
page.waitForEvent('popup'),
365+
page.evaluate(url => window._popup = window.open(url), server.PREFIX + '/formatted-number.html'),
366+
]);
367+
await popup.waitForLoadState({ waitUntil: 'domcontentloaded' });
368+
const result = await popup.evaluate(() => window.initialNavigatorLanguage);
369+
expect(result).toBe('fr-CH');
370+
await context.close();
371+
});
359372
});
360373

361374
describe('focus', function() {

test/geolocation.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ module.exports.describe = function ({ testRunner, expect, FFOX, WEBKIT }) {
112112
expect(allMessages).toContain('lat=20 lng=30');
113113
expect(allMessages).toContain('lat=40 lng=50');
114114
});
115-
it.fail(FFOX)('should use context options for popup', async({page, context, server}) => {
115+
it('should use context options for popup', async({page, context, server}) => {
116116
await context.grantPermissions(['geolocation']);
117117
await context.setGeolocation({ longitude: 10, latitude: 10 });
118118
const [popup] = await Promise.all([

test/popup.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, WE
3939
expect(userAgent).toBe('hey');
4040
expect(request.headers['user-agent']).toBe('hey');
4141
});
42-
it.fail(FFOX)('should respect routes from browser context', async function({browser, server}) {
42+
it('should respect routes from browser context', async function({browser, server}) {
4343
const context = await browser.newContext();
4444
const page = await context.newPage();
4545
await page.goto(server.EMPTY_PAGE);

0 commit comments

Comments
 (0)