Skip to content

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

fix(test runner): forward the screen option from device descriptors (#42681)

Pavel Feldman3c8362531dcd
diff --git a/docs/src/test-api/class-testoptions.md b/docs/src/test-api/class-testoptions.md
index ef2f439d60..ac587752dc 100644
--- a/docs/src/test-api/class-testoptions.md
+++ b/docs/src/test-api/class-testoptions.md
@@ -561,6 +561,27 @@ export default defineConfig({
});
```
+## property: TestOptions.screen
+* since: v1.64
+- type: ?<[Object]>
+ - `width` <[int]> page width in pixels.
+ - `height` <[int]> page height in pixels.
+
+Emulates consistent window screen size available inside web page via `window.screen`. Is only used when the [`property: TestOptions.viewport`] is set.
+
+**Usage**
+
+```js title="playwright.config.ts"
+import { defineConfig } from '@playwright/test';
+
+export default defineConfig({
+ use: {
+ viewport: { width: 390, height: 664 },
+ screen: { width: 390, height: 844 },
+ },
+});
+```
+
## property: TestOptions.screenshot
* since: v1.10
- type: <[Object]|[ScreenshotMode]<"off"|"on"|"only-on-failure"|"on-first-failure">>
diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.ts
index b1e14b7810..d7860be83c 100644
--- a/packages/playwright/src/index.ts
+++ b/packages/playwright/src/index.ts
@@ -279,6 +279,7 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures, UtilityTestFixt
permissions: [({ contextOptions }, use) => use(contextOptions.permissions), { 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 }],
+ screen: [({ contextOptions }, use) => use(contextOptions.screen), { 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 }],
@@ -311,6 +312,7 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures, UtilityTestFixt
permissions,
proxy,
reducedMotion,
+ screen,
storageState,
viewport,
timezoneId,
@@ -356,6 +358,8 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures, UtilityTestFixt
options.proxy = proxy;
if (reducedMotion !== undefined)
options.reducedMotion = reducedMotion;
+ if (screen !== undefined)
+ options.screen = screen;
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 e23a076665..9253f8e45c 100644
--- a/packages/playwright/types/test.d.ts
+++ b/packages/playwright/types/test.d.ts
@@ -7721,6 +7721,26 @@ export interface PlaywrightTestOptions {
*
*/
reducedMotion: ReducedMotion;
+ /**
+ * Emulates consistent window screen size available inside web page via `window.screen`. Is only used when the
+ * [testOptions.viewport](https://playwright.dev/docs/api/class-testoptions#test-options-viewport) is set.
+ *
+ * **Usage**
+ *
+ * ```js
+ * // playwright.config.ts
+ * import { defineConfig } from '@playwright/test';
+ *
+ * export default defineConfig({
+ * use: {
+ * viewport: { width: 390, height: 664 },
+ * screen: { width: 390, height: 844 },
+ * },
+ * });
+ * ```
+ *
+ */
+ screen: ViewportSize | undefined;
/**
* Learn more about [storage state and auth](https://playwright.dev/docs/auth).
*
diff --git a/tests/playwright-test/playwright.spec.ts b/tests/playwright-test/playwright.spec.ts
index 29508fa69a..853f7ed7ae 100644
--- a/tests/playwright-test/playwright.spec.ts
+++ b/tests/playwright-test/playwright.spec.ts
@@ -38,6 +38,32 @@ export class VideoPlayer {
}
}
+test('should respect screen option', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'playwright.config.ts': `
+ module.exports = { use: { viewport: { width: 500, height: 500 }, screen: { width: 1000, height: 1000 } } };
+ `,
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('pass', async ({ page, screen }) => {
+ expect(screen).toEqual({ width: 1000, height: 1000 });
+ expect(await page.evaluate(() => [window.screen.width, window.screen.height])).toEqual([1000, 1000]);
+ });
+ `,
+ 'b.test.ts': `
+ import { test, expect, devices } from '@playwright/test';
+ test.use({ ...devices['iPhone 13'] });
+ test('pass', async ({ page, screen }) => {
+ expect(screen).toEqual({ width: 390, height: 844 });
+ expect(await page.evaluate(() => [window.screen.width, window.screen.height])).toEqual([390, 844]);
+ });
+ `,
+ }, { workers: 1 });
+
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(2);
+});
+
test('should respect viewport option', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': `
diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts
index b95c661759..3f6f556804 100644
--- a/utils/generate_types/overrides-test.d.ts
+++ b/utils/generate_types/overrides-test.d.ts
@@ -299,6 +299,7 @@ export interface PlaywrightTestOptions {
permissions: string[] | undefined;
proxy: Proxy | undefined;
reducedMotion: ReducedMotion;
+ screen: ViewportSize | undefined;
storageState: StorageState | undefined;
timezoneId: string | undefined;
userAgent: string | undefined;