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

fix(firefox): make interception, locale and geolocation work on browser context level #1472

Merged
merged 1 commit into from
Mar 22, 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": "1047",
"firefox_revision": "1048",
"webkit_revision": "1182"
},
"scripts": {
Expand Down
10 changes: 6 additions & 4 deletions src/firefox/ffBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class FFBrowser extends platform.EventEmitter implements Browser {
bypassCSP: options.bypassCSP,
javaScriptDisabled: options.javaScriptEnabled === false ? true : undefined,
viewport,
locale: options.locale,
removeOnDetach: true
});
// TODO: move ignoreHTTPSErrors to browser context level.
Expand Down Expand Up @@ -174,6 +175,8 @@ export class FFBrowserContext extends BrowserContextBase {
await this.setOffline(this._options.offline);
if (this._options.httpCredentials)
await this.setHTTPCredentials(this._options.httpCredentials);
if (this._options.geolocation)
await this.setGeolocation(this._options.geolocation);
}

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

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

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

async close() {
Expand Down
8 changes: 0 additions & 8 deletions src/firefox/ffPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,13 @@ export class FFPage implements PageDelegate {
}

async _initialize() {
const geolocation = this._browserContext._options.geolocation;
const language = this._browserContext._options.locale;
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(),
language ? this._session.send('Page.setLanguageOverride', { language }) : Promise.resolve(),
new Promise(f => this._session.once('Page.ready', f)),
]);
this._pageCallback(this._page);
Expand Down Expand Up @@ -485,10 +481,6 @@ 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
1 change: 1 addition & 0 deletions test/assets/formatted-number.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<script>
window.result = (1000000.50).toLocaleString().replace(/\s/g, ' ');
window.initialNavigatorLanguage = navigator.language;
</script>
2 changes: 1 addition & 1 deletion test/browsercontext.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, FF
});
});

describe.fail(FFOX)('BrowserContext.route', () => {
describe('BrowserContext.route', () => {
it('should intercept', async({browser, server}) => {
const context = await browser.newContext();
let intercepted = false;
Expand Down
15 changes: 14 additions & 1 deletion test/emulation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ module.exports.describe = function({testRunner, expect, playwright, headless, FF
await context.close();
}
});
it.fail(CHROMIUM || FFOX)('should apply to popups', async({browser, server}) => {
it.fail(CHROMIUM || FFOX)('should format number in popups', async({browser, server}) => {
const context = await browser.newContext({ locale: 'fr-CH' });
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
Expand All @@ -356,6 +356,19 @@ module.exports.describe = function({testRunner, expect, playwright, headless, FF
expect(result).toBe('1 000 000,5');
await context.close();
});
it.fail(CHROMIUM)('should affect navigator.language in popups', async({browser, server}) => {
const context = await browser.newContext({ locale: 'fr-CH' });
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
const [popup] = await Promise.all([
page.waitForEvent('popup'),
page.evaluate(url => window._popup = window.open(url), server.PREFIX + '/formatted-number.html'),
]);
await popup.waitForLoadState({ waitUntil: 'domcontentloaded' });
const result = await popup.evaluate(() => window.initialNavigatorLanguage);
expect(result).toBe('fr-CH');
await context.close();
});
});

describe('focus', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/geolocation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ module.exports.describe = function ({ testRunner, expect, FFOX, WEBKIT }) {
expect(allMessages).toContain('lat=20 lng=30');
expect(allMessages).toContain('lat=40 lng=50');
});
it.fail(FFOX)('should use context options for popup', async({page, context, server}) => {
it('should use context options for popup', async({page, context, server}) => {
await context.grantPermissions(['geolocation']);
await context.setGeolocation({ longitude: 10, latitude: 10 });
const [popup] = await Promise.all([
Expand Down
2 changes: 1 addition & 1 deletion test/popup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, WE
expect(userAgent).toBe('hey');
expect(request.headers['user-agent']).toBe('hey');
});
it.fail(FFOX)('should respect routes from browser context', async function({browser, server}) {
it('should respect routes from browser context', async function({browser, server}) {
const context = await browser.newContext();
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
Expand Down