Skip to content

Commit 62cef72

Browse files
authored
chore(bidi): add support for prefers-color-scheme emulation (#43668)
1 parent 57decd2 commit 62cef72

6 files changed

Lines changed: 103 additions & 9 deletions

File tree

‎packages/playwright-core/src/server/bidi/bidiBrowser.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ export class BidiBrowserContext extends BrowserContext {
246246
userContexts: [this._userContextId()],
247247
}));
248248
promises.push(this.doUpdateDefaultViewport());
249+
promises.push(this.doUpdateDefaultEmulatedMedia());
249250
if (this._options.geolocation)
250251
promises.push(this.setGeolocation(this._options.geolocation));
251252
if (this._options.locale) {
@@ -493,6 +494,17 @@ export class BidiBrowserContext extends BrowserContext {
493494
}
494495

495496
override async doUpdateDefaultEmulatedMedia() {
497+
const features = this._options;
498+
const colorScheme =
499+
features.colorScheme === 'no-override'
500+
? null
501+
: features.colorScheme === 'no-preference'
502+
? undefined
503+
: features.colorScheme;
504+
await this._browser._browserSession.send('emulation.setMediaFeaturesOverride', {
505+
features: { 'prefers-color-scheme': colorScheme },
506+
userContexts: [this._userContextId()],
507+
});
496508
}
497509

498510
override async doExposePlaywrightBinding() {

‎packages/playwright-core/src/server/bidi/bidiPage.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,17 @@ export class BidiPage implements PageDelegate {
367367
}
368368

369369
async updateEmulateMedia(): Promise<void> {
370+
const features = this._page.emulatedMedia();
371+
const colorScheme =
372+
features.colorScheme === 'no-override'
373+
? null
374+
: features.colorScheme === 'no-preference'
375+
? undefined
376+
: features.colorScheme;
377+
await this._session.send('emulation.setMediaFeaturesOverride', {
378+
features: { 'prefers-color-scheme': colorScheme },
379+
contexts: [this._session.sessionId, ...this._session._browsingContexts],
380+
});
370381
}
371382

372383
async updateUserAgent(): Promise<void> {

‎packages/playwright-core/src/server/bidi/third_party/bidiCommands.d.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ export interface Commands {
142142
params: Bidi.Emulation.SetLocaleOverrideParameters;
143143
returnType: Bidi.EmptyResult;
144144
};
145+
'emulation.setMediaFeaturesOverride': {
146+
params: Bidi.Emulation.SetMediaFeaturesOverrideParameters;
147+
returnType: Bidi.Emulation.SetMediaFeaturesOverrideResult;
148+
};
145149
'emulation.setNetworkConditions': {
146150
params: Bidi.Emulation.SetNetworkConditionsParameters;
147151
returnType: Bidi.Emulation.SetNetworkConditionsResult;

‎packages/playwright-core/src/server/bidi/third_party/bidiProtocolCore.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ export type EmulationCommand =
11521152
| Emulation.SetForcedColorsModeThemeOverride
11531153
| Emulation.SetGeolocationOverride
11541154
| Emulation.SetLocaleOverride
1155+
| Emulation.SetMediaFeaturesOverride
11551156
| Emulation.SetNetworkConditions
11561157
| Emulation.SetScreenOrientationOverride
11571158
| Emulation.SetScreenSettingsOverride
@@ -1164,6 +1165,7 @@ export type EmulationResult =
11641165
| Emulation.SetForcedColorsModeThemeOverrideResult
11651166
| Emulation.SetGeolocationOverrideResult
11661167
| Emulation.SetLocaleOverrideResult
1168+
| Emulation.SetMediaFeaturesOverrideResult
11671169
| Emulation.SetScreenOrientationOverrideResult
11681170
| Emulation.SetScriptingEnabledResult
11691171
| Emulation.SetScrollbarTypeOverrideResult
@@ -1284,6 +1286,64 @@ export namespace Emulation {
12841286
export namespace Emulation {
12851287
export type SetLocaleOverrideResult = EmptyResult;
12861288
}
1289+
export namespace Emulation {
1290+
export type SetMediaFeaturesOverride = {
1291+
method: 'emulation.setMediaFeaturesOverride';
1292+
params: Emulation.SetMediaFeaturesOverrideParameters;
1293+
};
1294+
}
1295+
export namespace Emulation {
1296+
export type SetMediaFeaturesOverrideParameters = {
1297+
features: Emulation.MediaFeatures | null;
1298+
contexts?: [
1299+
BrowsingContext.BrowsingContext,
1300+
...BrowsingContext.BrowsingContext[],
1301+
];
1302+
userContexts?: [Browser.UserContext, ...Browser.UserContext[]];
1303+
};
1304+
}
1305+
export namespace Emulation {
1306+
export type MediaFeatures = {
1307+
'any-hover'?: 'none' | 'hover' | null;
1308+
'any-pointer'?: 'none' | 'coarse' | 'fine' | null;
1309+
color?: JsUint | null;
1310+
'color-gamut'?: 'srgb' | 'p3' | 'rec2020' | null;
1311+
'color-index'?: JsUint | null;
1312+
'display-mode'?:
1313+
| 'fullscreen'
1314+
| 'standalone'
1315+
| 'minimal-ui'
1316+
| 'browser'
1317+
| 'picture-in-picture'
1318+
| null;
1319+
'dynamic-range'?: 'standard' | 'high' | null;
1320+
'environment-blending'?: 'opaque' | 'additive' | 'subtractive' | null;
1321+
'forced-colors'?: 'none' | 'active' | null;
1322+
grid?: 0 | 1 | null;
1323+
'horizontal-viewport-segments'?: JsUint | null;
1324+
hover?: 'none' | 'hover' | null;
1325+
'inverted-colors'?: 'none' | 'inverted' | null;
1326+
monochrome?: JsUint | null;
1327+
'nav-controls'?: 'none' | 'back' | null;
1328+
'overflow-block'?: 'none' | 'scroll' | 'optional-paged' | 'paged' | null;
1329+
'overflow-inline'?: 'none' | 'scroll' | null;
1330+
pointer?: 'none' | 'coarse' | 'fine' | null;
1331+
'prefers-color-scheme'?: 'light' | 'dark' | null;
1332+
'prefers-contrast'?: 'no-preference' | 'more' | 'less' | 'custom' | null;
1333+
'prefers-reduced-data'?: 'no-preference' | 'reduce' | null;
1334+
'prefers-reduced-motion'?: 'no-preference' | 'reduce' | null;
1335+
'prefers-reduced-transparency'?: 'no-preference' | 'reduce' | null;
1336+
scan?: 'interlace' | 'progressive' | null;
1337+
scripting?: 'none' | 'initial-only' | 'enabled' | null;
1338+
update?: 'none' | 'slow' | 'fast' | null;
1339+
'vertical-viewport-segments'?: JsUint | null;
1340+
'video-color-gamut'?: 'srgb' | 'p3' | 'rec2020' | null;
1341+
'video-dynamic-range'?: 'standard' | 'high' | null;
1342+
};
1343+
}
1344+
export namespace Emulation {
1345+
export type SetMediaFeaturesOverrideResult = EmptyResult;
1346+
}
12871347
export namespace Emulation {
12881348
export type SetNetworkConditions = {
12891349
method: 'emulation.setNetworkConditions';

‎tests/bidi/expectations/moz-firefox-nightly-library.conf‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ library/agent-perform.spec.ts > retrieve a secret [fixme]
22
library/browsercontext-add-cookies.spec.ts > CO-040 should allow unnamed cookies [fixme]
33
library/browsercontext-basic.spec.ts > BC-024 fetch with keepalive should throw when offline [fixme]
44
library/browsercontext-basic.spec.ts > BC-018 should disable javascript [fixme]
5-
library/browsercontext-basic.spec.ts > BC-029 should emulate media in cross-process iframe [fixme]
6-
library/browsercontext-basic.spec.ts > BC-028 should emulate media in popup [fixme]
75
library/browsercontext-cookies-third-party.spec.ts > CO-057 same origin third party 'Partitioned;' cookie with different origin intermediate iframe [fixme]
86
library/browsercontext-cookies-third-party.spec.ts > CO-055 save/load third party 'Partitioned;' cookies [fixme]
97
library/browsercontext-cookies-third-party.spec.ts > CO-053 save/load third party non-partitioned cookies [fixme]
@@ -19,6 +17,7 @@ library/browsercontext-devtools.spec.ts > should close tab via close button [fix
1917
library/browsercontext-devtools.spec.ts > should display screencast image [fixme]
2018
library/browsercontext-devtools.spec.ts > should show no-pages placeholder when all tabs are closed [fixme]
2119
library/browsercontext-events.spec.ts > BC-033 console event should work with element handles [fixme]
20+
library/browsercontext-har.spec.ts > HA-081 should goBack to redirected navigation [fixme]
2221
library/browsercontext-har.spec.ts > HA-082 should goForward to redirected navigation [fixme]
2322
library/browsercontext-har.spec.ts > HA-089 should record overridden requests to har [fixme]
2423
library/browsercontext-har.spec.ts > HA-088 should round-trip har with postData [fixme]
@@ -36,12 +35,14 @@ library/browsercontext-pages.spec.ts > BC-056 should click the button with devic
3635
library/browsercontext-reuse.spec.ts > reuse connect > BC-076 should not cache resources [fixme]
3736
library/browsercontext-reuse.spec.ts > reuse connect > BC-074 should reset serviceworker [fixme]
3837
library/browsercontext-reuse.spec.ts > reuse connect > BC-075 should reset serviceworker that hangs in importScripts [fixme]
39-
library/browsercontext-reuse.spec.ts > reuse connect > BC-083 should update viewport and media [fixme]
4038
library/browsercontext-reuse.spec.ts > reuse launch > BC-076 should not cache resources [fixme]
4139
library/browsercontext-reuse.spec.ts > reuse launch > BC-074 should reset serviceworker [fixme]
4240
library/browsercontext-reuse.spec.ts > reuse launch > BC-075 should reset serviceworker that hangs in importScripts [fixme]
43-
library/browsercontext-reuse.spec.ts > reuse launch > BC-083 should update viewport and media [fixme]
4441
library/browsercontext-service-worker-policy.spec.ts > block > WO-030 should not throw error on about:blank [fixme]
42+
library/browsercontext-service-worker-policy.spec.ts > WO-028 should allow cache-only service worker navigation while offline [fixme]
43+
library/browsercontext-service-worker-policy.spec.ts > WO-028 should allow literal service worker navigation while offline [fixme]
44+
library/browsercontext-service-worker-policy.spec.ts > WO-028 should allow network-fallback service worker navigation while offline [fixme]
45+
library/browsercontext-service-worker-policy.spec.ts > WO-028 should allow preload-fallback service worker navigation while offline [fixme]
4546
library/browsercontext-storage-state.spec.ts > ST-015 should roundtrip local storage in third-party context [fixme]
4647
library/browsercontext-storage-state.spec.ts > ST-014 should set local storage in third-party context [fixme]
4748
library/browsercontext-storage-state.spec.ts > ST-013 should work when service worker is intefering and the origin is not open [fixme]
@@ -97,7 +98,6 @@ library/debug-controller.spec.ts > should work with browser._launchServer [fixme
9798
library/defaultbrowsercontext-1.spec.ts > DC-007 should support bypassCSP option [fixme]
9899
library/defaultbrowsercontext-1.spec.ts > DC-008 should support javascriptEnabled option [fixme]
99100
library/defaultbrowsercontext-2.spec.ts > DC-030 should have passed URL when launching with ignoreDefaultArgs: true [fixme]
100-
library/defaultbrowsercontext-2.spec.ts > DC-014 should support colorScheme option [fixme]
101101
library/defaultbrowsercontext-2.spec.ts > DC-017 should support contrast option [fixme]
102102
library/defaultbrowsercontext-2.spec.ts > DC-016 should support forcedColors option [fixme]
103103
library/defaultbrowsercontext-2.spec.ts > DC-012 should support hasTouch option [fixme]
@@ -106,11 +106,16 @@ library/defaultbrowsercontext-2.spec.ts > DC-013 should work in persistent conte
106106
library/download.spec.ts > download event > DO-027 should be able to cancel pending downloads [fixme]
107107
library/download.spec.ts > download event > DO-017 should report new window downloads [fixme]
108108
library/download.spec.ts > download event > DO-025 should throw if browser dies [fixme]
109+
library/emulation-coop.spec.ts > emulation with COOP > EM-025 should preserve context options [fixme]
110+
library/emulation-coop.spec.ts > emulation with COOP > EM-026 should preserve page.emulateMedia [fixme]
111+
library/emulation-coop.spec.ts > emulation with COOP > EM-027 should preserve mobile emulation [fixme]
109112
library/emulation-focus.spec.ts > EM-015 should focus popups by default [fixme]
110113
library/emulation-focus.spec.ts > EM-021 should focus with more than one page/context [fixme]
111114
library/emulation-focus.spec.ts > EM-014 should think that all pages are focused @smoke [fixme]
115+
library/emulation-focus.spec.ts > EM-023 should not blur iframe when interacting with another page/context [fixme]
112116
library/fetch-proxy.spec.ts > PR-041 context request should pick up proxy credentials [fixme]
113117
library/firefox/launcher.spec.ts > FF-003 should support custom firefox policies [fixme]
118+
library/firefox/launcher.spec.ts > FF-004 should resize viewport with fractional device scale factor [fixme]
114119
library/geolocation.spec.ts > GE-009 should use context options for popup [fixme]
115120
library/har-websocket.spec.ts > HA-104 should attach websocket messages [fixme]
116121
library/har-websocket.spec.ts > HA-105 should attach websocket messages for a still open websocket after stopping [fixme]
@@ -137,6 +142,7 @@ library/har.spec.ts > HA-056 should not hang on slow chunked response [fixme]
137142
library/har.spec.ts > HA-032 should report the correct _transferSize with PNG files [fixme]
138143
library/har.spec.ts > HA-046 should return security details directly from response [fixme]
139144
library/har.spec.ts > HA-045 should return server address directly from response [fixme]
145+
library/har.spec.ts > HA-113 should store binary content with a textual mime type as base64 [fixme]
140146
library/headful.spec.ts > HE-014 should click bottom row w/ infobar in OOPIF [fixme]
141147
library/headful.spec.ts > HE-013 should click in OOPIF [fixme]
142148
library/heap.spec.ts > LE-014 cycle handles [fixme]

‎tests/bidi/expectations/moz-firefox-nightly-page.conf‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,14 @@ page/page-drag.spec.ts > Drag and drop > DD-010 should work if the drag event is
7979
page/page-drag.spec.ts > Drag and drop > DD-004 should work inside iframe [fixme]
8080
page/page-drag.spec.ts > Drag and drop > DD-016 should work with locators [fixme]
8181
page/page-drag.spec.ts > Drag and drop > DD-013 should work with the helper method [fixme]
82+
page/page-drag.spec.ts > Drag and drop > DD-028 should report the default drop effect from effectAllowed [fixme]
8283
page/page-drag.spec.ts > DD-019 should handle custom dataTransfer [fixme]
83-
page/page-emulate-media.spec.ts > EM-007 should change the actual colors in css [fixme]
84-
page/page-emulate-media.spec.ts > EM-004 should default to light [fixme]
85-
page/page-emulate-media.spec.ts > EM-003 should emulate colorScheme should work @smoke [fixme]
8684
page/page-emulate-media.spec.ts > EM-011 should emulate contrast [fixme]
8785
page/page-emulate-media.spec.ts > EM-010 should emulate forcedColors [fixme]
8886
page/page-emulate-media.spec.ts > EM-008 should emulate reduced motion [fixme]
8987
page/page-emulate-media.spec.ts > EM-001 should emulate type @smoke [fixme]
9088
page/page-emulate-media.spec.ts > EM-009 should keep reduced motion and color emulation after reload [fixme]
9189
page/page-emulate-media.spec.ts > EM-012 should report hover and fine pointer for desktop [fixme]
92-
page/page-emulate-media.spec.ts > EM-006 should work during navigation [fixme]
9390
page/page-event-console.spec.ts > CM-008 should trigger correct Log [fixme]
9491
page/page-event-console.spec.ts > CM-001 should work @smoke [fixme]
9592
page/page-event-request.spec.ts > NE-095 should report requests and responses handled by service worker [fixme]
@@ -98,13 +95,16 @@ page/page-expose-function.spec.ts > EF-015 should work with setContent [fixme]
9895
page/page-filechooser.spec.ts > FC-020 should be able to reset selected files with empty file list [fixme]
9996
page/page-filechooser.spec.ts > FC-004 should emit event for iframe [fixme]
10097
page/page-filechooser.spec.ts > FC-011 should respect timeout [fixme]
98+
page/page-filechooser.spec.ts > user activation > FC-026 should not open file chooser without user activation [fixme]
10199
page/page-goto.spec.ts > NA-055 should override referrer-policy [fixme]
102100
page/page-goto.spec.ts > NA-052 should send referer [fixme]
103101
page/page-goto.spec.ts > NA-053 should send referer of cross-origin URL [fixme]
104102
page/page-history.spec.ts > NA-074 goBack/goForward should work with bfcache-able pages [fixme]
105103
page/page-history.spec.ts > NA-073 page.goBack should work for file urls [fixme]
106104
page/page-history.spec.ts > NA-085 regression test for issue 20791 [fixme]
107105
page/page-keyboard.spec.ts > KB-026 should press audio and media control keys [fixme]
106+
page/page-keyboard.spec.ts > KB-023 should press NumpadDecimal as Delete [fixme]
107+
page/page-keyboard.spec.ts > KB-024 should type numpad digits with Shift [fixme]
108108
page/page-mouse.spec.ts > MO-013 should always round down [fixme]
109109
page/page-mouse.spec.ts > MO-002 should dblclick the div [fixme]
110110
page/page-network-idle.spec.ts > NE-082 should not wait for an open EventSource connection [fixme]
@@ -139,6 +139,7 @@ page/page-screenshot.spec.ts > page screenshot animations > SS-054 should not ca
139139
page/page-screenshot.spec.ts > page screenshot animations > SS-053 should not capture pseudo element css animation [fixme]
140140
page/page-screenshot.spec.ts > page screenshot > SS-014 should allow transparency [fixme]
141141
page/page-set-input-files.spec.ts > IF-002 should upload a folder [fixme]
142+
page/page-set-input-files.spec.ts > IF-003 should upload a folder via label [fixme]
142143
page/page-wait-for-load-state.spec.ts > WA-084 should wait for load state of about:blank popup [fixme]
143144
page/page-wait-for-load-state.spec.ts > WA-085 should wait for load state of about:blank popup with noopener [fixme]
144145
page/page-wait-for-load-state.spec.ts > WA-083 should wait for load state of empty url popup [fixme]

0 commit comments

Comments
 (0)