Skip to content

Commit 0fd84ab

Browse files
committed
check screencast parameters
1 parent b81a350 commit 0fd84ab

5 files changed

Lines changed: 50 additions & 7 deletions

File tree

docs/src/api/class-inspector.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* since: v1.59
33
* langs: js
44

5-
The `Inspector` object provides access to the Playwright inspector's screencast capabilities, allowing you to capture live JPEG frames from the page as it renders.
5+
The `Inspector` object provides access to the Playwright inspector's capabilities.
66

77
**Usage**
88

packages/playwright-client/types/types.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20437,8 +20437,7 @@ export interface FrameLocator {
2043720437
}
2043820438

2043920439
/**
20440-
* The `Inspector` object provides access to the Playwright inspector's screencast capabilities, allowing you to
20441-
* capture live JPEG frames from the page as it renders.
20440+
* The `Inspector` object provides access to the Playwright inspector's capabilities.
2044220441
*
2044320442
* **Usage**
2044420443
*

packages/playwright-core/src/server/screencast.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class Screencast {
2929
private _videoRecorder: VideoRecorder | null = null;
3030
private _videoId: string | null = null;
3131
private _screencastClients = new Set<unknown>();
32+
private _screencastOptions: { width: number, height: number, quality: number } | null = null;
3233

3334
// Aiming at 25 fps by default - each frame is 40ms, but we give some slack with 35ms.
3435
// When throttling for tracing, 200ms between frames, except for 10 frames around the action.
@@ -140,8 +141,13 @@ export class Screencast {
140141
}
141142

142143
async startScreencast(client: unknown, options: { width: number, height: number, quality: number }) {
144+
if (this._screencastOptions) {
145+
if (options.width !== this._screencastOptions.width || options.height !== this._screencastOptions.height || options.quality !== this._screencastOptions.quality)
146+
throw new Error(`Screencast is already running with different options (${this._screencastOptions.width}x${this._screencastOptions.height} quality=${this._screencastOptions.quality})`);
147+
}
143148
this._screencastClients.add(client);
144149
if (this._screencastClients.size === 1) {
150+
this._screencastOptions = options;
145151
await this._page.delegate.startScreencast({
146152
width: options.width,
147153
height: options.height,
@@ -152,8 +158,10 @@ export class Screencast {
152158

153159
async stopScreencast(client: unknown) {
154160
this._screencastClients.delete(client);
155-
if (!this._screencastClients.size)
161+
if (!this._screencastClients.size) {
162+
this._screencastOptions = null;
156163
await this._page.delegate.stopScreencast();
164+
}
157165
}
158166
}
159167

packages/playwright-core/types/types.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20437,8 +20437,7 @@ export interface FrameLocator {
2043720437
}
2043820438

2043920439
/**
20440-
* The `Inspector` object provides access to the Playwright inspector's screencast capabilities, allowing you to
20441-
* capture live JPEG frames from the page as it renders.
20440+
* The `Inspector` object provides access to the Playwright inspector's capabilities.
2044220441
*
2044320442
* **Usage**
2044420443
*

tests/library/video.spec.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ it.describe('screencast', () => {
901901
it('inspector.startScreencast emits screencastframe events', async ({ browser, server }) => {
902902
const size = { width: 500, height: 400 };
903903
const context = await browser.newContext({ viewport: size });
904-
const page = await context.newPage();
904+
const page = await context.newPage();
905905

906906
const frames: Buffer[] = [];
907907
page.inspector().on('screencastframe', (data: Buffer) => frames.push(data));
@@ -921,6 +921,43 @@ it.describe('screencast', () => {
921921

922922
await context.close();
923923
});
924+
925+
it('startScreencast throws when called with different options while running', async ({ browser }) => {
926+
const size = { width: 500, height: 400 };
927+
const context = await browser.newContext({ viewport: size });
928+
const page = await context.newPage();
929+
930+
await page.inspector().startScreencast({ size });
931+
await expect(page.inspector().startScreencast({ size: { width: 320, height: 240 } })).rejects.toThrow('Screencast is already running with different options');
932+
933+
await page.inspector().stopScreencast();
934+
await context.close();
935+
});
936+
937+
it('startScreencast allows restart with different options after stop', async ({ browser }) => {
938+
const context = await browser.newContext({ viewport: { width: 500, height: 400 } });
939+
const page = await context.newPage();
940+
941+
await page.inspector().startScreencast({ size: { width: 500, height: 400 } });
942+
await page.inspector().stopScreencast();
943+
// Different options should succeed once the previous screencast is stopped.
944+
await expect(page.inspector().startScreencast({ size: { width: 320, height: 240 } })).resolves.toBeUndefined();
945+
946+
await page.inspector().stopScreencast();
947+
await context.close();
948+
});
949+
950+
it('startScreencast throws when video recording is running with different params', async ({ browser }) => {
951+
const videoSize = { width: 500, height: 400 };
952+
const context = await browser.newContext({ viewport: videoSize });
953+
const page = await context.newPage();
954+
955+
await page.video().start({ size: videoSize });
956+
await expect(page.inspector().startScreencast({ size: { width: 320, height: 240 } })).rejects.toThrow('Screencast is already running with different options');
957+
958+
await page.video().stop();
959+
await context.close();
960+
});
924961
});
925962

926963
it('should saveAs video', async ({ browser }, testInfo) => {

0 commit comments

Comments
 (0)