Skip to content

Commit 7efff97

Browse files
authored
fix(chromium): properly handle failures to set override (#1498)
1 parent 5bf9f22 commit 7efff97

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/chromium/crPage.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export class CRPage implements PageDelegate {
141141
if (options.userAgent || options.locale)
142142
promises.push(this._client.send('Emulation.setUserAgentOverride', { userAgent: options.userAgent || '', acceptLanguage: options.locale }));
143143
if (options.locale)
144-
promises.push(this._client.send('Emulation.setLocaleOverride', { locale: options.locale }));
144+
promises.push(emulateLocale(this._client, options.locale));
145145
if (options.timezoneId)
146146
promises.push(emulateTimezone(this._client, options.timezoneId));
147147
if (options.geolocation)
@@ -608,10 +608,25 @@ function toRemoteObject(handle: js.JSHandle): Protocol.Runtime.RemoteObject {
608608
return handle._remoteObject as Protocol.Runtime.RemoteObject;
609609
}
610610

611+
async function emulateLocale(session: CRSession, locale: string) {
612+
try {
613+
await session.send('Emulation.setLocaleOverride', { locale });
614+
} catch (exception) {
615+
// All pages in the same renderer share locale. All such pages belong to the same
616+
// context and if locale is overridden for one of them its value is the same as
617+
// we are trying to set so it's not a problem.
618+
if (exception.message.includes('Another locale override is already in effect'))
619+
return;
620+
throw exception;
621+
}
622+
}
623+
611624
async function emulateTimezone(session: CRSession, timezoneId: string) {
612625
try {
613626
await session.send('Emulation.setTimezoneOverride', { timezoneId: timezoneId });
614627
} catch (exception) {
628+
if (exception.message.includes('Timezone override is already in effect'))
629+
return;
615630
if (exception.message.includes('Invalid timezone'))
616631
throw new Error(`Invalid timezone ID: ${timezoneId}`);
617632
throw exception;

test/emulation.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,20 @@ module.exports.describe = function({testRunner, expect, playwright, headless, FF
289289
await context.close();
290290
}
291291
});
292+
it('should work for multiple pages sharing same process', async({browser, server}) => {
293+
const context = await browser.newContext({ timezoneId: 'Europe/Moscow' });
294+
const page = await context.newPage();
295+
await page.goto(server.EMPTY_PAGE);
296+
let [popup] = await Promise.all([
297+
page.waitForEvent('popup'),
298+
page.evaluate(url => { window.open(url); }, server.EMPTY_PAGE),
299+
]);
300+
[popup] = await Promise.all([
301+
popup.waitForEvent('popup'),
302+
popup.evaluate(url => { window.open(url); }, server.EMPTY_PAGE),
303+
]);
304+
await context.close();
305+
});
292306
});
293307

294308
describe('BrowserContext({locale})', function() {
@@ -369,6 +383,20 @@ module.exports.describe = function({testRunner, expect, playwright, headless, FF
369383
expect(result).toBe('fr-CH');
370384
await context.close();
371385
});
386+
it('should work for multiple pages sharing same process', async({browser, server}) => {
387+
const context = await browser.newContext({ locale: 'ru-RU' });
388+
const page = await context.newPage();
389+
await page.goto(server.EMPTY_PAGE);
390+
let [popup] = await Promise.all([
391+
page.waitForEvent('popup'),
392+
page.evaluate(url => { window.open(url); }, server.EMPTY_PAGE),
393+
]);
394+
[popup] = await Promise.all([
395+
popup.waitForEvent('popup'),
396+
popup.evaluate(url => { window.open(url); }, server.EMPTY_PAGE),
397+
]);
398+
await context.close();
399+
});
372400
});
373401

374402
describe('focus', function() {

0 commit comments

Comments
 (0)