Skip to content

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

feat(test): add reducedMotion, forcedColors, contrast as standalone test options (#42051)

Simon Knottf4aa8802e078
diff --git a/docs/src/test-api/class-testoptions.md b/docs/src/test-api/class-testoptions.md
index 1db97d3d13..ef94ead33e 100644
--- a/docs/src/test-api/class-testoptions.md
+++ b/docs/src/test-api/class-testoptions.md
@@ -219,6 +219,21 @@ export default defineConfig({
});
```
+## property: TestOptions.contrast = %%-context-option-contrast-%%
+* since: v1.50
+
+**Usage**
+
+```js title="playwright.config.ts"
+import { defineConfig } from '@playwright/test';
+
+export default defineConfig({
+ use: {
+ contrast: 'more',
+ },
+});
+```
+
## property: TestOptions.deviceScaleFactor = %%-context-option-devicescalefactor-%%
* since: v1.10
@@ -252,6 +267,21 @@ export default defineConfig({
});
```
+## property: TestOptions.forcedColors = %%-context-option-forcedColors-%%
+* since: v1.50
+
+**Usage**
+
+```js title="playwright.config.ts"
+import { defineConfig } from '@playwright/test';
+
+export default defineConfig({
+ use: {
+ forcedColors: 'active',
+ },
+});
+```
+
## property: TestOptions.geolocation = %%-context-option-geolocation-%%
* since: v1.10
@@ -495,6 +525,21 @@ export default defineConfig({
});
```
+## property: TestOptions.reducedMotion = %%-context-option-reducedMotion-%%
+* since: v1.50
+
+**Usage**
+
+```js title="playwright.config.ts"
+import { defineConfig } from '@playwright/test';
+
+export default defineConfig({
+ use: {
+ reducedMotion: 'reduce',
+ },
+});
+```
+
## property: TestOptions.reuseContext
* since: v1.62
* discouraged: This option trades test isolation for speed and is intended for component tests that drive a story gallery. Leave it unset for end-to-end tests - a fresh browser context per test is one of the core guarantees of Playwright Test.
diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.ts
index c0ac03305b..fd4ac59061 100644
--- a/packages/playwright/src/index.ts
+++ b/packages/playwright/src/index.ts
@@ -272,8 +272,10 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures, UtilityTestFixt
acceptDownloads: [({ contextOptions }, use) => use(contextOptions.acceptDownloads ?? true), { option: true, box: true }],
bypassCSP: [({ contextOptions }, use) => use(contextOptions.bypassCSP ?? false), { option: true, box: true }],
colorScheme: [({ contextOptions }, use) => use(contextOptions.colorScheme === undefined ? 'light' : contextOptions.colorScheme), { option: true, box: true }],
+ contrast: [({ contextOptions }, use) => use(contextOptions.contrast === undefined ? 'no-preference' : contextOptions.contrast), { option: true, box: true }],
deviceScaleFactor: [({ contextOptions }, use) => use(contextOptions.deviceScaleFactor), { option: true, box: true }],
extraHTTPHeaders: [({ contextOptions }, use) => use(contextOptions.extraHTTPHeaders), { option: true, box: true }],
+ forcedColors: [({ contextOptions }, use) => use(contextOptions.forcedColors === undefined ? 'none' : contextOptions.forcedColors), { option: true, box: true }],
geolocation: [({ contextOptions }, use) => use(contextOptions.geolocation), { option: true, box: true }],
hasTouch: [({ contextOptions }, use) => use(contextOptions.hasTouch ?? false), { option: true, box: true }],
httpCredentials: [({ contextOptions }, use) => use(contextOptions.httpCredentials), { option: true, box: true }],
@@ -285,6 +287,7 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures, UtilityTestFixt
permissions: [({ contextOptions }, use) => use(contextOptions.permissions), { option: true, box: true }],
pierceFrames: [({ contextOptions }, use) => use(contextOptions.pierceFrames ?? false), { option: true, box: true }],
proxy: [({ contextOptions }, use) => use(contextOptions.proxy), { option: true, box: true }],
+ reducedMotion: [({ contextOptions }, use) => use(contextOptions.reducedMotion === undefined ? 'no-preference' : contextOptions.reducedMotion), { option: true, box: true }],
storageState: [({ contextOptions }, use) => use(contextOptions.storageState), { option: true, box: true }],
clientCertificates: [({ contextOptions }, use) => use(contextOptions.clientCertificates), { option: true, box: true }],
timezoneId: [({ contextOptions }, use) => use(contextOptions.timezoneId), { option: true, box: true }],
@@ -302,8 +305,10 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures, UtilityTestFixt
bypassCSP,
clientCertificates,
colorScheme,
+ contrast,
deviceScaleFactor,
extraHTTPHeaders,
+ forcedColors,
hasTouch,
geolocation,
httpCredentials,
@@ -315,6 +320,7 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures, UtilityTestFixt
permissions,
pierceFrames,
proxy,
+ reducedMotion,
storageState,
viewport,
timezoneId,
@@ -330,10 +336,14 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures, UtilityTestFixt
options.bypassCSP = bypassCSP;
if (colorScheme !== undefined)
options.colorScheme = colorScheme;
+ if (contrast !== undefined)
+ options.contrast = contrast;
if (deviceScaleFactor !== undefined)
options.deviceScaleFactor = deviceScaleFactor;
if (extraHTTPHeaders !== undefined)
options.extraHTTPHeaders = extraHTTPHeaders;
+ if (forcedColors !== undefined)
+ options.forcedColors = forcedColors;
if (geolocation !== undefined)
options.geolocation = geolocation;
if (hasTouch !== undefined)
@@ -356,6 +366,8 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures, UtilityTestFixt
options.pierceFrames = pierceFrames;
if (proxy !== undefined)
options.proxy = proxy;
+ if (reducedMotion !== undefined)
+ options.reducedMotion = reducedMotion;
if (storageState !== undefined)
options.storageState = storageState;
if (clientCertificates?.length)
diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts
index cfa014bbca..1280c1dd3d 100644
--- a/packages/playwright/types/test.d.ts
+++ b/packages/playwright/types/test.d.ts
@@ -6873,6 +6873,9 @@ export type Fixtures<T extends {} = {}, W extends {} = {}, PT extends {} = {}, P
type BrowserName = 'chromium' | 'firefox' | 'webkit';
type BrowserChannel = Exclude<LaunchOptions['channel'], undefined>;
type ColorScheme = Exclude<BrowserContextOptions['colorScheme'], undefined>;
+type Contrast = Exclude<BrowserContextOptions['contrast'], undefined>;
+type ForcedColors = Exclude<BrowserContextOptions['forcedColors'], undefined>;
+type ReducedMotion = Exclude<BrowserContextOptions['reducedMotion'], undefined>;
type ClientCertificate = Exclude<BrowserContextOptions['clientCertificates'], undefined>[0];
type ExtraHTTPHeaders = Exclude<BrowserContextOptions['extraHTTPHeaders'], undefined>;
type Proxy = Exclude<BrowserContextOptions['proxy'], undefined>;
@@ -7332,6 +7335,26 @@ export interface PlaywrightTestOptions {
*
*/
colorScheme: ColorScheme;
+ /**
+ * Emulates `'prefers-contrast'` media feature, supported values are `'no-preference'`, `'more'`. See
+ * [page.emulateMedia([options])](https://playwright.dev/docs/api/class-page#page-emulate-media) for more details.
+ * Passing `null` resets emulation to system defaults. Defaults to `'no-preference'`.
+ *
+ * **Usage**
+ *
+ * ```js
+ * // playwright.config.ts
+ * import { defineConfig } from '@playwright/test';
+ *
+ * export default defineConfig({
+ * use: {
+ * contrast: 'more',
+ * },
+ * });
+ * ```
+ *
+ */
+ contrast: Contrast;
/**
* TLS Client Authentication allows the server to request a client certificate and verify it.
*
@@ -7409,6 +7432,26 @@ export interface PlaywrightTestOptions {
*
*/
extraHTTPHeaders: ExtraHTTPHeaders | undefined;
+ /**
+ * Emulates `'forced-colors'` media feature, supported values are `'active'`, `'none'`. See
+ * [page.emulateMedia([options])](https://playwright.dev/docs/api/class-page#page-emulate-media) for more details.
+ * Passing `null` resets emulation to system defaults. Defaults to `'none'`.
+ *
+ * **Usage**
+ *
+ * ```js
+ * // playwright.config.ts
+ * import { defineConfig } from '@playwright/test';
+ *
+ * export default defineConfig({
+ * use: {
+ * forcedColors: 'active',
+ * },
+ * });
+ * ```
+ *
+ */
+ forcedColors: ForcedColors;
/**
* **Usage**
*
@@ -7627,6 +7670,26 @@ export interface PlaywrightTestOptions {
*
*/
proxy: Proxy | undefined;
+ /**
+ * Emulates `'prefers-reduced-motion'` media feature, supported values are `'reduce'`, `'no-preference'`. See
+ * [page.emulateMedia([options])](https://playwright.dev/docs/api/class-page#page-emulate-media) for more details.
+ * Passing `null` resets emulation to system defaults. Defaults to `'no-preference'`.
+ *
+ * **Usage**
+ *
+ * ```js
+ * // playwright.config.ts
+ * import { defineConfig } from '@playwright/test';
+ *
+ * export default defineConfig({
+ * use: {
+ * reducedMotion: 'reduce',
+ * },
+ * });
+ * ```
+ *
+ */
+ reducedMotion: ReducedMotion;
/**
* Learn more about [storage state and auth](https://playwright.dev/docs/auth).
*
diff --git a/tests/playwright-test/playwright.config.spec.ts b/tests/playwright-test/playwright.config.spec.ts
index a168f8983b..025a160e34 100644
--- a/tests/playwright-test/playwright.config.spec.ts
+++ b/tests/playwright-test/playwright.config.spec.ts
@@ -127,8 +127,10 @@ test('should override contextOptions', async ({ runInlineTest }) => {
acceptDownloads: false,
bypassCSP: true,
colorScheme: 'dark',
+ contrast: 'more',
deviceScaleFactor: 2,
extraHTTPHeaders: {'foo': 'bar'},
+ forcedColors: 'active',
hasTouch: true,
ignoreHTTPSErrors: true,
isMobile: true,
@@ -136,6 +138,7 @@ test('should override contextOptions', async ({ runInlineTest }) => {
locale: 'fr-FR',
offline: true,
permissions: ['geolocation'],
+ reducedMotion: 'reduce',
timezoneId: 'TIMEZONE',
userAgent: 'UA',
viewport: null,
@@ -143,8 +146,10 @@ test('should override contextOptions', async ({ runInlineTest }) => {
acceptDownloads: true,
bypassCSP: false,
colorScheme: 'light',
+ contrast: 'no-preference',
deviceScaleFactor: 1,
extraHTTPHeaders: {'foo': 'bar2'},
+ forcedColors: 'none',
hasTouch: false,
ignoreHTTPSErrors: false,
isMobile: false,
@@ -152,6 +157,7 @@ test('should override contextOptions', async ({ runInlineTest }) => {
locale: 'en-US',
offline: false,
permissions: [],
+ reducedMotion: 'no-preference',
timezoneId: 'TIMEZONE 2',
userAgent: 'UA 2',
viewport: { width: 500, height: 500 }
@@ -161,12 +167,14 @@ test('should override contextOptions', async ({ runInlineTest }) => {
`,
'a.test.ts': `
import { test, expect } from '@playwright/test';
- test('pass', async ({ acceptDownloads, bypassCSP, colorScheme, deviceScaleFactor, extraHTTPHeaders, hasTouch, ignoreHTTPSErrors, isMobile, javaScriptEnabled, locale, offline, permissions, timezoneId, userAgent, viewport }) => {
+ test('pass', async ({ acceptDownloads, bypassCSP, colorScheme, contrast, deviceScaleFactor, extraHTTPHeaders, forcedColors, hasTouch, ignoreHTTPSErrors, isMobile, javaScriptEnabled, locale, offline, permissions, reducedMotion, timezoneId, userAgent, viewport }) => {
expect.soft(acceptDownloads).toBe(false);
expect.soft(bypassCSP).toBe(true);
expect.soft(colorScheme).toBe('dark');
+ expect.soft(contrast).toBe('more');
expect.soft(deviceScaleFactor).toBe(2);
expect.soft(extraHTTPHeaders).toEqual({'foo': 'bar'});
+ expect.soft(forcedColors).toBe('active');
expect.soft(hasTouch).toBe(true);
expect.soft(ignoreHTTPSErrors).toBe(true);
expect.soft(isMobile).toBe(true);
@@ -174,6 +182,7 @@ test('should override contextOptions', async ({ runInlineTest }) => {
expect.soft(locale).toBe('fr-FR');
expect.soft(offline).toBe(true);
expect.soft(permissions).toEqual(['geolocation']);
+ expect.soft(reducedMotion).toBe('reduce');
expect.soft(timezoneId).toBe('TIMEZONE');
expect.soft(userAgent).toBe('UA');
expect.soft(viewport).toBe(null);
diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts
index 8244dc4037..e81e81d448 100644
--- a/utils/generate_types/overrides-test.d.ts
+++ b/utils/generate_types/overrides-test.d.ts
@@ -220,6 +220,9 @@ export type Fixtures<T extends {} = {}, W extends {} = {}, PT extends {} = {}, P
type BrowserName = 'chromium' | 'firefox' | 'webkit';
type BrowserChannel = Exclude<LaunchOptions['channel'], undefined>;
type ColorScheme = Exclude<BrowserContextOptions['colorScheme'], undefined>;
+type Contrast = Exclude<BrowserContextOptions['contrast'], undefined>;
+type ForcedColors = Exclude<BrowserContextOptions['forcedColors'], undefined>;
+type ReducedMotion = Exclude<BrowserContextOptions['reducedMotion'], undefined>;
type ClientCertificate = Exclude<BrowserContextOptions['clientCertificates'], undefined>[0];
type ExtraHTTPHeaders = Exclude<BrowserContextOptions['extraHTTPHeaders'], undefined>;
type Proxy = Exclude<BrowserContextOptions['proxy'], undefined>;
@@ -278,9 +281,11 @@ export interface PlaywrightTestOptions {
acceptDownloads: boolean;
bypassCSP: boolean;
colorScheme: ColorScheme;
+ contrast: Contrast;
clientCertificates: ClientCertificate[] | undefined;
deviceScaleFactor: number | undefined;
extraHTTPHeaders: ExtraHTTPHeaders | undefined;
+ forcedColors: ForcedColors;
geolocation: Geolocation | undefined;
hasTouch: boolean;
httpCredentials: HTTPCredentials | HTTPCredentials[] | undefined;
@@ -292,6 +297,7 @@ export interface PlaywrightTestOptions {
permissions: string[] | undefined;
pierceFrames: boolean;
proxy: Proxy | undefined;
+ reducedMotion: ReducedMotion;
storageState: StorageState | undefined;
timezoneId: string | undefined;
userAgent: string | undefined;