Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(chromium): apply locale to dedicated workers (#43488)
Yury Semikhatskyb89519fcfeaf
diff --git a/packages/playwright-core/src/server/chromium/crPage.ts b/packages/playwright-core/src/server/chromium/crPage.tsindex 68501601f9..ccb247e106 100644--- a/packages/playwright-core/src/server/chromium/crPage.ts+++ b/packages/playwright-core/src/server/chromium/crPage.ts@@ -792,6 +792,10 @@ class FrameSession {return;}+ this._initializeWorker(session, event);+ }++ private _initializeWorker(session: CRSession, event: Protocol.Target.attachedToTargetPayload) {const url = event.targetInfo.url;const worker = new Worker(this._page, url);this._page.addWorker(event.sessionId, worker);@@ -805,6 +809,11 @@ class FrameSession {worker.workerScriptLoaded();// This might fail if the target is closed before we initialize.session._sendMayFail('Runtime.enable');+ // Dedicated workers inherit the user agent, but not the Accept-Language and+ // navigator.languages that the page's user agent override sets from the locale.+ const options = this._crPage._browserContext._options;+ if (options.userAgent || options.locale)+ this._updateUserAgent(session).catch(() => {});this._crPage._networkManager.addSession(session, this._page.frameManager.frame(event.targetInfo.parentFrameId ?? this._targetId) ?? undefined).catch(() => {});session._sendMayFail('Runtime.runIfWaitingForDebugger');session._sendMayFail('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true });@@ -1074,9 +1083,9 @@ class FrameSession {await this._client.send('Emulation.setEmulatedMedia', { media, features });}- async _updateUserAgent(): Promise<void> {+ async _updateUserAgent(session: CRSession = this._client): Promise<void> {const options = this._crPage._browserContext._options;- await this._client.send('Emulation.setUserAgentOverride', {+ await session.send('Emulation.setUserAgentOverride', {userAgent: options.userAgent || '',acceptLanguage: options.locale,userAgentMetadata: calculateUserAgentMetadata(options),diff --git a/tests/library/browsercontext-locale.spec.ts b/tests/library/browsercontext-locale.spec.tsindex e496a25b07..0151f86f84 100644--- a/tests/library/browsercontext-locale.spec.ts+++ b/tests/library/browsercontext-locale.spec.ts@@ -165,18 +165,31 @@ it('LC-009 should not change default locale in another context', async ({ browse}});-it('LC-010 should propagate locale to workers', async ({ browser, browserName, server }) => {+it('LC-010 should propagate locale to workers', {+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43484' },+}, async ({ browser, browserName, server }) => {const context = await browser.newContext({ locale: 'ru-RU' });const page = await context.newPage();await page.goto(server.EMPTY_PAGE);- const [msg] = await Promise.all([+ const [msg, languages, request] = await Promise.all([page.waitForEvent('console', e => e.text().startsWith('locale:')),- page.evaluate(() => new Worker(URL.createObjectURL(new Blob(['console.log("locale:" + Intl.NumberFormat().resolvedOptions().locale)'], { type: 'application/javascript' })))),+ page.waitForEvent('console', e => e.text().startsWith('languages:')),+ server.waitForRequest('/title.html'),+ page.evaluate(url => new Worker(URL.createObjectURL(new Blob([`+ console.log("locale:" + Intl.NumberFormat().resolvedOptions().locale);+ console.log("languages:" + navigator.languages);+ fetch(${JSON.stringify(url)});+ `], { type: 'application/javascript' }))), server.PREFIX + '/title.html'),]);- if (browserName === 'webkit')- expect(msg.text()).toContain('locale:ru'); // Webkit on Ubuntu is "ru-RU", and on other platforms is "ru"- else+ if (browserName === 'webkit') {+ // Webkit on Ubuntu is "ru-RU", and on other platforms is "ru"+ expect(msg.text()).toContain('locale:ru');+ expect(languages.text()).toContain('languages:ru');+ } else {expect(msg.text()).toBe('locale:ru-RU');+ expect(languages.text()).toBe('languages:ru-RU');+ }+ expect(request.headers['accept-language']).toContain('ru-RU');await context.close();});