Skip to content

fix(emulation): sync navigator.platform with custom user agent across all browsers - #39723

Merged
Dmitry Gozman (dgozman) merged 4 commits into
microsoft:mainfrom
manimovassagh:fix-39568
Mar 25, 2026
Merged

Dmitry Gozman (dgozman) merged 4 commits into
microsoft:mainfrom
manimovassagh:fix-39568

Conversation

@manimovassagh

@manimovassagh Mani Movassagh (manimovassagh) commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

When a custom userAgent is configured (e.g. via device presets like Desktop Chrome),
navigator.platform leaks the host OS instead of matching the emulated user agent:

  • navigator.userAgentData.platform → "Windows" (from the preset UA)
  • navigator.platform → "MacIntel" (host OS leaked)

Libraries like React Aria check userAgentData.platform first, get "Windows", and
then expect Windows keyboard behavior — breaking tests that use ControlOrMeta.

Changes

Per Dmitry Gozman (@dgozman)'s request, unified into a single calculateUserAgentEmulation(options: BrowserContextOptions) in browserContext.ts that returns:

  • navigatorPlatform: string | undefined
  • userAgentMetadata: { mobile, model, architecture, platform, platformVersion } | undefined

The PLAYWRIGHT_NO_UA_PLATFORM check lives inside this function (not duplicated across call sites).

Chromium (crPage.ts): passes platform in Emulation.setUserAgentOverride via userAgentMetadata
Firefox (ffBrowser.ts): calls Browser.setPlatformOverride alongside Browser.setUserAgentOverride
WebKit (wkPage.ts): calls Page.overridePlatform alongside Page.overrideUserAgent

platformVersion is normalized: UA strings encode iOS/macOS versions with underscores (16_6) but Client Hints expect dots (16.6).

When no custom user agent is set, behavior is unchanged. Set PLAYWRIGHT_NO_UA_PLATFORM=1 to opt out.

Fixes #39568

@manimovassagh

Copy link
Copy Markdown
Contributor Author

Updated the PR with both changes:

1. PLAYWRIGHT_NO_UA_PLATFORM env variable
The platform override is active by default. Setting PLAYWRIGHT_NO_UA_PLATFORM=1 disables it, reverting to the previous behavior where navigator.platform reflects the host OS.

2. Firefox and WebKit support
Both browsers already had the protocol commands but weren't using them:

  • Firefox: Browser.setPlatformOverride
  • WebKit: Page.overridePlatform

Added a shared navigatorPlatformFromUA() helper in browserContext.ts that derives navigator.platform from the user agent string. All three browsers now use it when a custom userAgent is set.

Tested locally on Chromium, Firefox, and WebKit — all passing.

@manimovassagh
Mani Movassagh (manimovassagh) force-pushed the fix-39568 branch 2 times, most recently from 7945185 to b01c399 Compare March 17, 2026 19:05
… all browsers

When a custom userAgent is configured (e.g. via device presets), Playwright
sends platform metadata via CDP/protocol but does not override navigator.platform.
This causes navigator.platform to leak the host OS while the emulated platform
reflects the user agent string.

Pass the platform parameter in Emulation.setUserAgentOverride (Chromium),
Browser.setPlatformOverride (Firefox), and Page.overridePlatform (WebKit).

The new behavior can be disabled by setting PLAYWRIGHT_NO_UA_PLATFORM=1.

Fixes microsoft#39568
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

…ion in browserContext

Consolidates calculateUserAgentMetadata (crPage.ts) and navigatorPlatformFromUA
(browserContext.ts) into a single calculateUserAgentEmulation() exported from
browserContext.ts. All three browsers now call this one function which also
handles the PLAYWRIGHT_NO_UA_PLATFORM check internally.

Removes the duplicated env-var checks from crPage.ts, ffBrowser.ts, and wkPage.ts.
…acOS

UA strings encode version as e.g. "iPhone OS 16_6" but Chromium Client
Hints platformVersion expects "16.6". Apply replace(/_/g, '.') for iOS
and macOS matches.
@manimovassagh Mani Movassagh (manimovassagh) changed the title fix(chromium): sync navigator.platform with userAgentData when custom UA is set fix(emulation): sync navigator.platform with custom user agent across all browsers Mar 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Test results for "tests 1"

7 flaky ⚠️ [chromium-library] › library/popup.spec.ts:261 › should not throw when click closes popup `@ubuntu-22.04-chromium-tip-of-tree`
⚠️ [chromium-library] › library/trace-viewer.spec.ts:1223 › should display language-specific locators `@ubuntu-22.04-chromium-tip-of-tree`
⚠️ [chromium-page] › page/page-request-continue.spec.ts:754 › propagate headers cross origin redirect after interception `@chromium-ubuntu-22.04-node24`
⚠️ [chromium-library] › library/trace-viewer.spec.ts:1223 › should display language-specific locators `@chromium-ubuntu-22.04-node22`
⚠️ [firefox-library] › library/inspector/cli-codegen-1.spec.ts:1080 › cli codegen › should not throw csp directive violation errors `@firefox-ubuntu-22.04-node20`
⚠️ [webkit-library] › library/screencast.spec.ts:75 › start reuses existing screencast when video recording is running `@webkit-ubuntu-22.04-node20`
⚠️ [webkit-library] › library/trace-viewer.spec.ts:1223 › should display language-specific locators `@webkit-ubuntu-22.04-node20`

38821 passed, 845 skipped


Merge workflow run.

@github-actions

Copy link
Copy Markdown
Contributor

Test results for "MCP"

27 failed
❌ [chromium] › mcp/test-debug.spec.ts:126 › test_debug (multiple pages and custom error) @mcp-ubuntu-latest
❌ [chromium] › mcp/test-debug.spec.ts:167 › test_debug (pause/snapshot/resume) @mcp-ubuntu-latest
❌ [chromium] › mcp/test-run.spec.ts:87 › test_run filters @mcp-ubuntu-latest
❌ [chromium] › mcp/test-run.spec.ts:126 › test_run should include dependencies @mcp-ubuntu-latest
❌ [chromium] › mcp/test-setup.spec.ts:60 › setup should run global setup and teardown @mcp-ubuntu-latest
❌ [chromium] › mcp/test-setup.spec.ts:81 › test_run should run global setup and teardown @mcp-ubuntu-latest
❌ [chromium] › mcp/test-setup.spec.ts:105 › test_debug should run global setup and teardown @mcp-ubuntu-latest
❌ [chrome] › mcp/test-setup.spec.ts:60 › setup should run global setup and teardown @mcp-windows-latest
❌ [chrome] › mcp/test-setup.spec.ts:105 › test_debug should run global setup and teardown @mcp-windows-latest
❌ [chromium] › mcp/test-debug.spec.ts:167 › test_debug (pause/snapshot/resume) @mcp-windows-latest
❌ [chrome] › mcp/test-run.spec.ts:87 › test_run filters @mcp-macos-latest
❌ [chrome] › mcp/test-setup.spec.ts:60 › setup should run global setup and teardown @mcp-macos-latest
❌ [chrome] › mcp/test-setup.spec.ts:81 › test_run should run global setup and teardown @mcp-macos-latest
❌ [chrome] › mcp/test-setup.spec.ts:105 › test_debug should run global setup and teardown @mcp-macos-latest
❌ [chromium] › mcp/test-debug.spec.ts:21 › test_debug (passed) @mcp-macos-latest
❌ [chromium] › mcp/test-setup.spec.ts:60 › setup should run global setup and teardown @mcp-macos-latest
❌ [firefox] › mcp/test-debug.spec.ts:126 › test_debug (multiple pages and custom error) @mcp-ubuntu-latest
❌ [firefox] › mcp/test-debug.spec.ts:167 › test_debug (pause/snapshot/resume) @mcp-ubuntu-latest
❌ [webkit] › mcp/test-debug.spec.ts:21 › test_debug (passed) @mcp-windows-latest
❌ [webkit] › mcp/test-debug.spec.ts:167 › test_debug (pause/snapshot/resume) @mcp-windows-latest
❌ [firefox] › mcp/test-run.spec.ts:87 › test_run filters @mcp-macos-latest
❌ [firefox] › mcp/test-run.spec.ts:126 › test_run should include dependencies @mcp-macos-latest
❌ [firefox] › mcp/test-setup.spec.ts:60 › setup should run global setup and teardown @mcp-macos-latest
❌ [webkit] › mcp/test-debug.spec.ts:21 › test_debug (passed) @mcp-macos-latest
❌ [webkit] › mcp/test-debug.spec.ts:48 › test_debug (pause/resume) @mcp-macos-latest
❌ [webkit] › mcp/test-debug.spec.ts:167 › test_debug (pause/snapshot/resume) @mcp-macos-latest
❌ [webkit] › mcp/test-run.spec.ts:87 › test_run filters @mcp-macos-latest

5431 passed, 340 skipped


Merge workflow run.

@dgozman Dmitry Gozman (dgozman) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: navigator.userAgentData returns Windows on Mac. Incompatible with @react-ara/utils isAppleDevice

3 participants