Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

api: make clear the use of null in page.emulateMedia (#3078)

Dmitry Gozmanced0bc2d1bc1
diff --git a/docs/api.md b/docs/api.md
index c4f0d5dd0a..ef2284f67a 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -1138,8 +1138,8 @@ await page.dispatchEvent('#source', 'dragstart', { dataTransfer });
#### page.emulateMedia(options)
- `options` <[Object]>
- - `media` <"screen"|"print"> Changes the CSS media type of the page. The only allowed values are `'screen'`, `'print'` and `null`. Passing `null` disables CSS media emulation.
- - `colorScheme` <"dark"|"light"|"no-preference"> Emulates `'prefers-colors-scheme'` media feature, supported values are `'light'`, `'dark'`, `'no-preference'`.
+ - `media` <?"screen"|"print"> Changes the CSS media type of the page. The only allowed values are `'screen'`, `'print'` and `null`. Passing `null` disables CSS media emulation. Omitting `media` or passing `undefined` does not change the emulated value.
+ - `colorScheme` <?"dark"|"light"|"no-preference"> Emulates `'prefers-colors-scheme'` media feature, supported values are `'light'`, `'dark'`, `'no-preference'`. Passing `null` disables color scheme emulation. Omitting `colorScheme` or passing `undefined` does not change the emulated value.
- returns: <[Promise]>
```js
diff --git a/src/firefox/ffPage.ts b/src/firefox/ffPage.ts
index 4855609745..142717a6d1 100644
--- a/src/firefox/ffPage.ts
+++ b/src/firefox/ffPage.ts
@@ -293,7 +293,8 @@ export class FFPage implements PageDelegate {
async updateEmulateMedia(): Promise<void> {
const colorScheme = this._page._state.colorScheme || this._browserContext._options.colorScheme || 'light';
await this._session.send('Page.setEmulatedMedia', {
- type: this._page._state.mediaType === null ? undefined : this._page._state.mediaType,
+ // Empty string means reset.
+ type: this._page._state.mediaType === null ? '' : this._page._state.mediaType,
colorScheme
});
}
diff --git a/src/page.ts b/src/page.ts
index 8442fadfec..ff4ba7acd5 100644
--- a/src/page.ts
+++ b/src/page.ts
@@ -385,9 +385,11 @@ export class Page extends EventEmitter {
return waitPromise;
}
- async emulateMedia(options: { media?: types.MediaType, colorScheme?: types.ColorScheme }) {
- assert(!options.media || types.mediaTypes.has(options.media), 'media: expected one of (screen|print)');
- assert(!options.colorScheme || types.colorSchemes.has(options.colorScheme), 'colorScheme: expected one of (dark|light|no-preference)');
+ async emulateMedia(options: { media?: types.MediaType | null, colorScheme?: types.ColorScheme | null }) {
+ if (options.media !== undefined)
+ assert(options.media === null || types.mediaTypes.has(options.media), 'media: expected one of (screen|print)');
+ if (options.colorScheme !== undefined)
+ assert(options.colorScheme === null || types.colorSchemes.has(options.colorScheme), 'colorScheme: expected one of (dark|light|no-preference)');
if (options.media !== undefined)
this._state.mediaType = options.media;
if (options.colorScheme !== undefined)
diff --git a/src/rpc/channels.ts b/src/rpc/channels.ts
index ffa32805a0..310622f747 100644
--- a/src/rpc/channels.ts
+++ b/src/rpc/channels.ts
@@ -618,8 +618,8 @@ export type PageCloseParams = {
};
export type PageCloseResult = void;
export type PageEmulateMediaParams = {
- media?: 'screen' | 'print',
- colorScheme?: 'dark' | 'light' | 'no-preference',
+ media?: 'screen' | 'print' | 'reset',
+ colorScheme?: 'dark' | 'light' | 'no-preference' | 'reset',
};
export type PageEmulateMediaResult = void;
export type PageExposeBindingParams = {
diff --git a/src/rpc/client/page.ts b/src/rpc/client/page.ts
index dce4f1983f..c6d15d81da 100644
--- a/src/rpc/client/page.ts
+++ b/src/rpc/client/page.ts
@@ -357,9 +357,12 @@ export class Page extends ChannelOwner<PageChannel, PageInitializer> {
});
}
- async emulateMedia(options: { media?: types.MediaType, colorScheme?: types.ColorScheme }) {
+ async emulateMedia(options: { media?: types.MediaType | null, colorScheme?: types.ColorScheme | null }) {
return this._wrapApiCall('page.emulateMedia', async () => {
- await this._channel.emulateMedia(options);
+ await this._channel.emulateMedia({
+ media: options.media === null ? 'reset' : options.media,
+ colorScheme: options.colorScheme === null ? 'reset' : options.colorScheme,
+ });
});
}
diff --git a/src/rpc/protocol.pdl b/src/rpc/protocol.pdl
index 83e9d774b0..4b0d375ff6 100644
--- a/src/rpc/protocol.pdl
+++ b/src/rpc/protocol.pdl
@@ -527,10 +527,14 @@ interface Page
media?: enum
screen
print
+ # Reset emulated value to the system default.
+ reset
colorScheme?: enum
dark
light
no-preference
+ # Reset emulated value to the system default.
+ reset
command exposeBinding
parameters
diff --git a/src/rpc/server/pageDispatcher.ts b/src/rpc/server/pageDispatcher.ts
index 5b10fa8b9f..5d132812cc 100644
--- a/src/rpc/server/pageDispatcher.ts
+++ b/src/rpc/server/pageDispatcher.ts
@@ -20,7 +20,7 @@ import { Frame } from '../../frames';
import { Request } from '../../network';
import { Page, Worker } from '../../page';
import * as types from '../../types';
-import { BindingCallChannel, BindingCallInitializer, ElementHandleChannel, PageChannel, PageInitializer, ResponseChannel, WorkerInitializer, WorkerChannel, JSHandleChannel, Binary, SerializedArgument, PagePdfParams, SerializedError, PageAccessibilitySnapshotResult, SerializedValue } from '../channels';
+import { BindingCallChannel, BindingCallInitializer, ElementHandleChannel, PageChannel, PageInitializer, ResponseChannel, WorkerInitializer, WorkerChannel, JSHandleChannel, Binary, SerializedArgument, PagePdfParams, SerializedError, PageAccessibilitySnapshotResult, SerializedValue, PageEmulateMediaParams } from '../channels';
import { Dispatcher, DispatcherScope, lookupDispatcher, lookupNullableDispatcher } from './dispatcher';
import { parseError, serializeError, headersArrayToObject, axNodeToProtocol } from '../serializers';
import { ConsoleMessageDispatcher } from './consoleMessageDispatcher';
@@ -106,8 +106,11 @@ export class PageDispatcher extends Dispatcher<Page, PageInitializer> implements
return { response: lookupNullableDispatcher<ResponseDispatcher>(await this._page.goForward(params)) };
}
- async emulateMedia(params: { media?: 'screen' | 'print', colorScheme?: 'dark' | 'light' | 'no-preference' }): Promise<void> {
- await this._page.emulateMedia(params);
+ async emulateMedia(params: PageEmulateMediaParams): Promise<void> {
+ await this._page.emulateMedia({
+ media: params.media === 'reset' ? null : params.media,
+ colorScheme: params.colorScheme === 'reset' ? null : params.colorScheme,
+ });
}
async setViewportSize(params: { viewportSize: types.Size }): Promise<void> {
diff --git a/test/emulation.jest.js b/test/emulation.jest.js
index 33c6bd3a28..75eebd8443 100644
--- a/test/emulation.jest.js
+++ b/test/emulation.jest.js
@@ -241,7 +241,7 @@ describe('Page.emulateMedia type', function() {
await page.emulateMedia({});
expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(false);
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(true);
- await page.emulateMedia({ media: '' });
+ await page.emulateMedia({ media: null });
expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(true);
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(false);
});