Skip to content

Commit

Permalink
feat(lang): emulate language on firefox (#1453)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Mar 21, 2020
1 parent 21630d6 commit e210e56
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -9,7 +9,7 @@
"main": "index.js",
"playwright": {
"chromium_revision": "751710",
"firefox_revision": "1044",
"firefox_revision": "1045",
"webkit_revision": "1180"
},
"scripts": {
Expand Down
9 changes: 6 additions & 3 deletions src/firefox/ffBrowser.ts
Expand Up @@ -170,8 +170,8 @@ export class FFBrowserContext extends BrowserContextBase {
async _initialize() {
if (this._options.permissions)
await this.grantPermissions(this._options.permissions);
if (this._options.extraHTTPHeaders)
await this.setExtraHTTPHeaders(this._options.extraHTTPHeaders);
if (this._options.extraHTTPHeaders || this._options.locale)
await this.setExtraHTTPHeaders(this._options.extraHTTPHeaders || {});
if (this._options.offline)
await this.setOffline(this._options.offline);
if (this._options.httpCredentials)
Expand Down Expand Up @@ -257,7 +257,10 @@ export class FFBrowserContext extends BrowserContextBase {

async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {
this._options.extraHTTPHeaders = network.verifyHeaders(headers);
await this._browser._connection.send('Browser.setExtraHTTPHeaders', { browserContextId: this._browserContextId || undefined, headers: headersArray(this._options.extraHTTPHeaders) });
const allHeaders = { ...this._options.extraHTTPHeaders };
if (this._options.locale)
allHeaders['Accept-Language'] = this._options.locale;
await this._browser._connection.send('Browser.setExtraHTTPHeaders', { browserContextId: this._browserContextId || undefined, headers: headersArray(allHeaders) });
}

async setOffline(offline: boolean): Promise<void> {
Expand Down
2 changes: 2 additions & 0 deletions src/firefox/ffPage.ts
Expand Up @@ -87,6 +87,7 @@ 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.
Expand All @@ -95,6 +96,7 @@ export class FFPage implements PageDelegate {
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
3 changes: 3 additions & 0 deletions test/assets/formatted-number.html
@@ -0,0 +1,3 @@
<script>
window.result = (1000000.50).toLocaleString().replace(/\s/g, ' ');
</script>
20 changes: 17 additions & 3 deletions test/emulation.spec.js
Expand Up @@ -291,7 +291,7 @@ module.exports.describe = function({testRunner, expect, playwright, headless, FF
});
});

describe.fail(FFOX)('BrowserContext({locale})', function() {
describe('BrowserContext({locale})', function() {
it('should affect accept-language header', async({browser, server}) => {
const context = await browser.newContext({ locale: 'fr-CH' });
const page = await context.newPage();
Expand All @@ -308,7 +308,7 @@ module.exports.describe = function({testRunner, expect, playwright, headless, FF
expect(await page.evaluate(() => navigator.language)).toBe('fr-CH');
await context.close();
});
it('should format number', async({browser, server}) => {
it.fail(FFOX)('should format number', async({browser, server}) => {
{
const context = await browser.newContext({ locale: 'en-US' });
const page = await context.newPage();
Expand All @@ -324,7 +324,7 @@ module.exports.describe = function({testRunner, expect, playwright, headless, FF
await context.close();
}
});
it('should format date', async({browser, server}) => {
it.fail(FFOX)('should format date', async({browser, server}) => {
{
const context = await browser.newContext({ locale: 'en-US', timezoneId: 'America/Los_Angeles' });
const page = await context.newPage();
Expand All @@ -342,6 +342,20 @@ module.exports.describe = function({testRunner, expect, playwright, headless, FF
await context.close();
}
});
it.fail(CHROMIUM || FFOX)('should apply to 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.result);
expect(result).toBe('1 000 000,5');
await context.close();
});
});

describe('focus', function() {
Expand Down

0 comments on commit e210e56

Please sign in to comment.