Skip to content

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

chore: merge screencast start/startRecording (#39937)

Pavel Feldmanf72a639f6221
diff --git a/docs/src/api/class-screencast.md b/docs/src/api/class-screencast.md
index f3722cf691..3c201b5f45 100644
--- a/docs/src/api/class-screencast.md
+++ b/docs/src/api/class-screencast.md
@@ -1,91 +1,68 @@
# class: Screencast
* since: v1.59
-* langs: js
Interface for capturing screencast frames from a page.
## async method: Screencast.start
* since: v1.59
-* langs: js
- returns: <[Disposable]>
-Starts capturing screencast frames.
+Starts the screencast. When [`option: Screencast.start.path`] is provided, it saves video recording to the specified file.
+When [`option: Screencast.start.onFrame`] is provided, delivers JPEG-encoded frames to the callback. Both can be used together.
**Usage**
```js
-await page.screencast.start(({ data }) => {
- console.log(`frame size: ${data.length}`);
-}, { size: { width: 800, height: 600 } });
+// Record video
+await page.screencast.start({ path: 'video.webm', size: { width: 1280, height: 800 } });
+// ... perform actions ...
+await page.screencast.stop();
+```
+
+```js
+// Capture frames
+await page.screencast.start({
+ onFrame: ({ data }) => console.log(`frame size: ${data.length}`),
+ size: { width: 800, height: 600 },
+});
// ... perform actions ...
await page.screencast.stop();
```
-### param: Screencast.start.onFrame
+### option: Screencast.start.onFrame
* since: v1.59
-* langs: js
- `onFrame` <[function]\([Object]\): [Promise]>
- `data` <[Buffer]> JPEG-encoded frame data.
Callback that receives JPEG-encoded frame data.
-### option: Screencast.start.size
+### option: Screencast.start.path
* since: v1.59
-* langs: js
-- `size` ?<[Object=ScreencastSize]>
- - `width` <[int]> Max frame width in pixels.
- - `height` <[int]> Max frame height in pixels.
+- `path` <[path]>
-Specifies the dimensions of screencast frames. The actual frame is scaled to preserve the page’s aspect ratio and may be smaller than these bounds.
-If a screencast is already active (e.g. started by tracing or video recording), the existing configuration takes precedence and the frame size may exceed these bounds or this option may be ignored.
-If not specified the size will be equal to page viewport scaled down to fit into 800×800.
+Path where the video should be saved when the screencast is stopped. When provided, video recording is started.
### option: Screencast.start.quality
* since: v1.59
-* langs: js
- `quality` <[int]>
The quality of the image, between 0-100.
-## async method: Screencast.stop
+### option: Screencast.start.size
* since: v1.59
* langs: js
-
-Stops the screencast started with [`method: Screencast.start`].
-
-## async method: Screencast.startRecording
-* since: v1.59
-- returns: <[Disposable]>
-
-Starts video recording. This method is mutually exclusive with the `recordVideo` context option.
-
-### param: Screencast.startRecording.path
-* since: v1.59
-- `path` <[path]>
-
-Path where the video should be saved when the recording is stopped.
-
-### option: Screencast.startRecording.size
-* since: v1.59
- `size` ?<[Object=ScreencastSize]>
- - `width` <[int]> Video frame width.
- - `height` <[int]> Video frame height.
-
-Optional dimensions of the recorded video. If not specified the size will be equal to page viewport scaled down to fit into 800×800. Actual picture of the page will be scaled down if necessary to fit the specified size.
-
-### option: Screencast.startRecording.annotate
-* since: v1.59
-- `annotate` ?<[Object=ScreencastAnnotation]>
- - `duration` ?<[float]> How long each annotation is displayed in milliseconds. Defaults to `500`.
- - `position` ?<[AnnotatePosition]<"top-left"|"top"|"top-right"|"bottom-left"|"bottom"|"bottom-right">> Position of the action title overlay. Defaults to `"top-right"`.
- - `fontSize` ?<[int]> Font size of the action title in pixels. Defaults to `24`.
+ - `width` <[int]> Max frame width in pixels.
+ - `height` <[int]> Max frame height in pixels.
-If specified, enables visual annotations on interacted elements during video recording. Interacted elements are highlighted with a semi-transparent blue box and click points are shown as red circles.
+Specifies the dimensions of screencast frames. The actual frame is scaled to preserve the page's aspect ratio and may be smaller than these bounds.
+If a screencast is already active (e.g. started by tracing or video recording), the existing configuration takes precedence and the frame size may exceed these bounds or this option may be ignored.
+If not specified the size will be equal to page viewport scaled down to fit into 800×800.
-## async method: Screencast.stopRecording
+## async method: Screencast.stop
* since: v1.59
-Stops video recording started with [`method: Screencast.startRecording`].
+Stops the screencast and video recording if active. If a video was being recorded, saves it to the path specified in [`method: Screencast.start`].
## async method: Screencast.showOverlay
* since: v1.59
diff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.ts
index 06b9a192ac..afd509576b 100644
--- a/packages/playwright-client/types/types.d.ts
+++ b/packages/playwright-client/types/types.d.ts
@@ -16185,22 +16185,35 @@ export interface WebSocketRoute {
*/
export interface Screencast {
/**
- * Starts capturing screencast frames.
+ * Starts the screencast. When [`path`](https://playwright.dev/docs/api/class-screencast#screencast-start-option-path)
+ * is provided, it saves video recording to the specified file. When
+ * [`onFrame`](https://playwright.dev/docs/api/class-screencast#screencast-start-option-on-frame) is provided,
+ * delivers JPEG-encoded frames to the callback. Both can be used together.
*
* **Usage**
*
* ```js
- * await page.screencast.start(({ data }) => {
- * console.log(`frame size: ${data.length}`);
- * }, { size: { width: 800, height: 600 } });
+ * // Record video
+ * await page.screencast.start({ path: 'video.webm', size: { width: 1280, height: 800 } });
+ * // ... perform actions ...
+ * await page.screencast.stop();
+ * ```
+ *
+ * ```js
+ * // Capture frames
+ * await page.screencast.start({
+ * onFrame: ({ data }) => console.log(`frame size: ${data.length}`),
+ * size: { width: 800, height: 600 },
+ * });
* // ... perform actions ...
* await page.screencast.stop();
* ```
*
- * @param onFrame Callback that receives JPEG-encoded frame data.
* @param options
*/
- start(onFrame: ((frame: { data: Buffer }) => Promise<any>|any), options?: {
+ start(options?: {
+ onFrame?: (frame: { data: Buffer }) => Promise<any>|any;
+ path?: string;
size?: {
width: number;
height: number;
@@ -16255,60 +16268,10 @@ export interface Screencast {
showOverlays(): Promise<void>;
/**
- * Starts video recording. This method is mutually exclusive with the `recordVideo` context option.
- * @param path Path where the video should be saved when the recording is stopped.
- * @param options
- */
- startRecording(path: string, options?: {
- /**
- * If specified, enables visual annotations on interacted elements during video recording. Interacted elements are
- * highlighted with a semi-transparent blue box and click points are shown as red circles.
- */
- annotate?: {
- /**
- * How long each annotation is displayed in milliseconds. Defaults to `500`.
- */
- duration?: number;
-
- /**
- * Position of the action title overlay. Defaults to `"top-right"`.
- */
- position?: "top-left"|"top"|"top-right"|"bottom-left"|"bottom"|"bottom-right";
-
- /**
- * Font size of the action title in pixels. Defaults to `24`.
- */
- fontSize?: number;
- };
-
- /**
- * Optional dimensions of the recorded video. If not specified the size will be equal to page viewport scaled down to
- * fit into 800×800. Actual picture of the page will be scaled down if necessary to fit the specified size.
- */
- size?: {
- /**
- * Video frame width.
- */
- width: number;
-
- /**
- * Video frame height.
- */
- height: number;
- };
- }): Promise<Disposable>;
-
- /**
- * Stops the screencast started with
- * [screencast.start(onFrame[, options])](https://playwright.dev/docs/api/class-screencast#screencast-start).
+ * Stops the screencast and video recording if active. If a video was being recorded, saves it to the path specified
+ * in [screencast.start([options])](https://playwright.dev/docs/api/class-screencast#screencast-start).
*/
stop(): Promise<void>;
-
- /**
- * Stops video recording started with
- * [screencast.startRecording(path[, options])](https://playwright.dev/docs/api/class-screencast#screencast-start-recording).
- */
- stopRecording(): Promise<void>;
}
type DeviceDescriptor = {
diff --git a/packages/playwright-core/src/client/screencast.ts b/packages/playwright-core/src/client/screencast.ts
index 0888483e3e..e85203b292 100644
--- a/packages/playwright-core/src/client/screencast.ts
+++ b/packages/playwright-core/src/client/screencast.ts
@@ -23,6 +23,7 @@ import type { AnnotateOptions } from './types';
export class Screencast implements api.Screencast {
private _page: Page;
+ private _started = false;
private _savePath: string | undefined;
private _onFrame: ((frame: { data: Buffer }) => Promise<any>) | null = null;
private _artifact: Artifact | undefined;
@@ -34,31 +35,35 @@ export class Screencast implements api.Screencast {
});
}
- async start(onFrame: (frame: { data: Buffer }) => Promise<any>|any, options: { size?: { width: number, height: number }, quality?: number } = {}): Promise<DisposableStub> {
- if (this._onFrame)
+ async start(options: { onFrame?: (frame: { data: Buffer }) => Promise<any>|any, path?: string, size?: { width: number, height: number }, quality?: number, annotate?: AnnotateOptions } = {}): Promise<DisposableStub> {
+ if (this._started)
throw new Error('Screencast is already started');
- this._onFrame = onFrame;
- await this._page._channel.startScreencast(options);
+ this._started = true;
+ if (options.onFrame)
+ this._onFrame = options.onFrame;
+ const result = await this._page._channel.screencastStart({
+ size: options.size,
+ quality: options.quality,
+ sendFrames: !!options.onFrame,
+ record: !!options.path,
+ annotate: options.annotate,
+ });
+ if (result.artifact) {
+ this._artifact = Artifact.from(result.artifact);
+ this._savePath = options.path;
+ }
return new DisposableStub(() => this.stop());
}
async stop(): Promise<void> {
- this._onFrame = null;
- await this._page._channel.stopScreencast();
- }
-
- async startRecording(path: string, options: { size?: { width: number, height: number }, annotate?: AnnotateOptions } = {}) {
- const result = await this._page._channel.videoStart({ size: options.size, annotate: options.annotate });
- this._artifact = Artifact.from(result.artifact);
- this._savePath = path;
- return new DisposableStub(() => this.stopRecording());
- }
-
- async stopRecording(): Promise<void> {
await this._page._wrapApiCall(async () => {
- await this._page._channel.videoStop();
+ this._started = false;
+ this._onFrame = null;
+ await this._page._channel.screencastStop();
if (this._savePath)
await this._artifact?.saveAs(this._savePath);
+ this._artifact = undefined;
+ this._savePath = undefined;
});
}
diff --git a/packages/playwright-core/src/protocol/validator.ts b/packages/playwright-core/src/protocol/validator.ts
index 39d345b576..d34aeb7424 100644
--- a/packages/playwright-core/src/protocol/validator.ts
+++ b/packages/playwright-core/src/protocol/validator.ts
@@ -1569,28 +1569,21 @@ scheme.PageOverlaySetVisibleParams = tObject({
visible: tBoolean,
});
scheme.PageOverlaySetVisibleResult = tOptional(tObject({}));
-scheme.PageStartScreencastParams = tObject({
+scheme.PageScreencastStartParams = tObject({
size: tOptional(tObject({
width: tInt,
height: tInt,
})),
quality: tOptional(tInt),
-});
-scheme.PageStartScreencastResult = tOptional(tObject({}));
-scheme.PageStopScreencastParams = tOptional(tObject({}));
-scheme.PageStopScreencastResult = tOptional(tObject({}));
-scheme.PageVideoStartParams = tObject({
- size: tOptional(tObject({
- width: tInt,
- height: tInt,
- })),
+ sendFrames: tOptional(tBoolean),
+ record: tOptional(tBoolean),
annotate: tOptional(tType('AnnotateOptions')),
});
-scheme.PageVideoStartResult = tObject({
- artifact: tChannel(['Artifact']),
+scheme.PageScreencastStartResult = tObject({
+ artifact: tOptional(tChannel(['Artifact'])),
});
-scheme.PageVideoStopParams = tOptional(tObject({}));
-scheme.PageVideoStopResult = tOptional(tObject({}));
+scheme.PageScreencastStopParams = tOptional(tObject({}));
+scheme.PageScreencastStopResult = tOptional(tObject({}));
scheme.PageUpdateSubscriptionParams = tObject({
event: tEnum(['console', 'dialog', 'fileChooser', 'request', 'response', 'requestFinished', 'requestFailed']),
enabled: tBoolean,
diff --git a/packages/playwright-core/src/server/browserContext.ts b/packages/playwright-core/src/server/browserContext.ts
index 7878baf3b2..caa5520653 100644
--- a/packages/playwright-core/src/server/browserContext.ts
+++ b/packages/playwright-core/src/server/browserContext.ts
@@ -530,9 +530,7 @@ export abstract class BrowserContext<EM extends EventMap = EventMap> extends Sdk
for (const harRecorder of this._harRecorders.values())
await harRecorder.flush();
await this.tracing.flush();
-
- // Cleanup.
- const promises: Promise<void>[] = this.pages().map(page => page.screencast.handlePageOrContextClose());
+ await Promise.all(this.pages().map(page => page.screencast.handlePageOrContextClose()));
if (this._customCloseHandler) {
await this._customCloseHandler();
@@ -543,6 +541,7 @@ export abstract class BrowserContext<EM extends EventMap = EventMap> extends Sdk
// We delete downloads after context closure
// so that browser does not write to the download file anymore.
+ const promises: Promise<void>[] = [];
promises.push(this._deleteAllDownloads());
promises.push(this._deleteAllTempDirs());
await Promise.all(promises);
diff --git a/packages/playwright-core/src/server/dispatchers/pageDispatcher.ts b/packages/playwright-core/src/server/dispatchers/pageDispatcher.ts
index 2cbdadee61..949eca0b66 100644
--- a/packages/playwright-core/src/server/dispatchers/pageDispatcher.ts
+++ b/packages/playwright-core/src/server/dispatchers/pageDispatcher.ts
@@ -62,8 +62,8 @@ export class PageDispatcher extends Dispatcher<Page, channels.PageChannel, Brows
private _locatorHandlers = new Set<number>();
private _jsCoverageActive = false;
private _cssCoverageActive = false;
- private _screencastClient: ScreencastClient | null = null;
- private _videoRecorder: VideoRecorder | null = null;
+ private _screencastClient: ScreencastClient | undefined;
+ private _videoRecorder: VideoRecorder | undefined;
static from(parentScope: BrowserContextDispatcher, page: Page): PageDispatcher {
return PageDispatcher.fromNullable(parentScope, page)!;
@@ -374,47 +374,42 @@ export class PageDispatcher extends Dispatcher<Page, channels.PageChannel, Brows
await this._page.overlay.setVisible(params.visible);
}
- async startScreencast(params: channels.PageStartScreencastParams, progress?: Progress): Promise<channels.PageStartScreencastResult> {
- if (this._screencastClient)
+ async screencastStart(params: channels.PageScreencastStartParams, progress?: Progress): Promise<channels.PageScreencastStartResult> {
+ if (this._screencastClient || this._videoRecorder)
throw new Error('Screencast is already running');
- this._screencastClient = {
- onFrame: (frame: ScreencastFrame) => {
- this._dispatchEvent('screencastFrame', { data: frame.buffer });
- },
- dispose: () => {},
- size: params.size,
- quality: params.quality,
- };
- this._page.screencast.addClient(this._screencastClient);
- }
+ if (params.sendFrames) {
+ this._screencastClient = {
+ onFrame: (frame: ScreencastFrame) => {
+ this._dispatchEvent('screencastFrame', { data: frame.buffer });
+ },
+ dispose: () => {},
+ size: params.size,
+ quality: params.quality,
+ };
+ this._page.screencast.addClient(this._screencastClient);
+ }
- async stopScreencast(params: channels.PageStopScreencastParams, progress?: Progress): Promise<channels.PageStopScreencastResult> {
- await this._stopScreencast();
+ let artifact: Artifact | undefined;
+ if (params.record) {
+ this._videoRecorder = new VideoRecorder(this._page.screencast);
+ artifact = this._videoRecorder.start(params);
+ }
+ return { artifact: artifact ? createVideoDispatcher(this.parentScope(), artifact) : undefined };
}
- private async _stopScreencast() {
+ async screencastStop(params: channels.PageScreencastStopParams, progress?: Progress): Promise<channels.PageScreencastStopResult> {
+ if (this._videoRecorder) {
+ await this._videoRecorder.stop();
+ this._videoRecorder = undefined;
+ }
+
const client = this._screencastClient;
- this._screencastClient = null;
+ this._screencastClient = undefined;
if (client)
this._page.screencast.removeClient(client);
}
- async videoStart(params: channels.PageVideoStartParams, progress: Progress): Promise<channels.PageVideoStartResult> {
- if (this._videoRecorder)
- throw new Error('Video is already being recorded');
- this._videoRecorder = new VideoRecorder(this._page.screencast);
- const artifact = this._videoRecorder.start(params);
- return { artifact: createVideoDispatcher(this.parentScope(), artifact) };
- }
-
- async videoStop(params: channels.PageVideoStopParams, progress: Progress): Promise<channels.PageVideoStopResult> {
- if (!this._videoRecorder)
- throw new Error('Video is not being recorded');
- await this._videoRecorder.stop();
- this._videoRecorder = null;
- }
-
async startJSCoverage(params: channels.PageStartJSCoverageParams, progress: Progress): Promise<void> {
const coverage = this._page.coverage as CRCoverage;
await coverage.startJSCoverage(progress, params);
@@ -469,7 +464,7 @@ export class PageDispatcher extends Dispatcher<Page, channels.PageChannel, Brows
if (this._cssCoverageActive)
(this._page.coverage as CRCoverage).stopCSSCoverage().catch(() => {});
this._cssCoverageActive = false;
- this._stopScreencast().catch(() => {});
+ this.screencastStop({}, undefined).catch(() => {});
}
async setDockTile(params: channels.PageSetDockTileParams): Promise<void> {
diff --git a/packages/playwright-core/src/server/page.ts b/packages/playwright-core/src/server/page.ts
index e617f4d938..37ca556ead 100644
--- a/packages/playwright-core/src/server/page.ts
+++ b/packages/playwright-core/src/server/page.ts
@@ -790,9 +790,14 @@ export class Page extends SdkObject<PageEventMap> {
async close(options: { runBeforeUnload?: boolean, reason?: string } = {}) {
if (this._closedState === 'closed')
return;
+
if (options.reason)
this._closeReason = options.reason;
const runBeforeUnload = !!options.runBeforeUnload;
+
+ if (!runBeforeUnload)
+ await this.screencast.handlePageOrContextClose();
+
if (this._closedState !== 'closing') {
// If runBeforeUnload is true, we don't know if we will close, so don't modify the state
if (!runBeforeUnload)
@@ -801,7 +806,6 @@ export class Page extends SdkObject<PageEventMap> {
// while we are trying to close the page.
await this.delegate.closePage(runBeforeUnload).catch(e => debugLogger.log('error', e));
}
- await this.screencast.handlePageOrContextClose();
if (!runBeforeUnload)
await this.closedPromise;
}
diff --git a/packages/playwright-core/src/server/screencast.ts b/packages/playwright-core/src/server/screencast.ts
index 13f06a1aa1..4944b52689 100644
--- a/packages/playwright-core/src/server/screencast.ts
+++ b/packages/playwright-core/src/server/screencast.ts
@@ -42,11 +42,12 @@ export class Screencast implements InstrumentationListener {
}
async handlePageOrContextClose() {
- for (const clients of this._clients) {
- if (clients.gracefulClose)
- await clients.gracefulClose();
- }
+ const clients = [...this._clients];
this._clients.clear();
+ for (const client of clients) {
+ if (client.gracefulClose)
+ await client.gracefulClose();
+ }
}
dispose() {
diff --git a/packages/playwright-core/src/server/trace/recorder/tracing.ts b/packages/playwright-core/src/server/trace/recorder/tracing.ts
index 67c1d74157..7b0239d533 100644
--- a/packages/playwright-core/src/server/trace/recorder/tracing.ts
+++ b/packages/playwright-core/src/server/trace/recorder/tracing.ts
@@ -722,10 +722,8 @@ function createAfterActionTraceEvent(metadata: CallMetadata): trace.AfterActionT
};
}
-// Aiming at 25 fps by default - each frame is 40ms, but we give some slack with 35ms.
-// When an action occurs, throttling is disabled for 200ms to capture the action.
-const throttledRate = 35;
-const unthrottleDuration = 200;
+const throttledRate = 200;
+const unthrottleDuration = 500;
class ScreencastTracingRecorder {
private _screencast: Screencast;
diff --git a/packages/playwright-core/src/server/videoRecorder.ts b/packages/playwright-core/src/server/videoRecorder.ts
index 1ba950e01b..4702d8d297 100644
--- a/packages/playwright-core/src/server/videoRecorder.ts
+++ b/packages/playwright-core/src/server/videoRecorder.ts
@@ -74,7 +74,7 @@ export class VideoRecorder {
this._screencast.removeClient(client);
await videoRecorder.stop();
- artifact.reportFinished();
+ await artifact.reportFinished();
}
}
diff --git a/packages/playwright-core/src/tools/backend/context.ts b/packages/playwright-core/src/tools/backend/context.ts
index 80ee03a6bd..91eee5813f 100644
--- a/packages/playwright-core/src/tools/backend/context.ts
+++ b/packages/playwright-core/src/tools/backend/context.ts
@@ -196,7 +196,7 @@ export class Context {
return [];
const video = this._video;
for (const page of this._rawBrowserContext.pages())
- await page.screencast.stopRecording();
+ await page.screencast.stop();
this._video = undefined;
return [...video.fileNames];
}
@@ -211,7 +211,7 @@ export class Context {
fileName = path.basename(fileName, ext) + suffix + ext;
}
this._video.fileNames.push(fileName);
- await page.screencast.startRecording(fileName, this._video.params);
+ await page.screencast.start({ path: fileName, ...this._video.params });
}
private _onPageCreated(page: playwright.Page) {
diff --git a/packages/playwright-core/src/tools/cli-client/skill/references/video-recording.md b/packages/playwright-core/src/tools/cli-client/skill/references/video-recording.md
index e92e3a9b97..9c04afb94f 100644
--- a/packages/playwright-core/src/tools/cli-client/skill/references/video-recording.md
+++ b/packages/playwright-core/src/tools/cli-client/skill/references/video-recording.md
@@ -50,7 +50,7 @@ It allows pulling appropriate pauses between the actions and annotating the vide
```js
async page => {
- await page.screencast.startRecording('video.webm', { size: { width: 1280, height: 800 } });
+ await page.screencast.start({ path: 'video.webm', size: { width: 1280, height: 800 } });
await page.goto('https://demo.playwright.dev/todomvc');
// Show a chapter card — blurs the page and shows a dialog.
@@ -113,7 +113,7 @@ async page => {
</div>
`, { duration: 2000 });
- await page.screencast.stopRecording();
+ await page.screencast.stop();
}
```
diff --git a/packages/playwright-core/src/tools/dashboard/dashboardController.ts b/packages/playwright-core/src/tools/dashboard/dashboardController.ts
index 1c854fc67e..d2b07474f3 100644
--- a/packages/playwright-core/src/tools/dashboard/dashboardController.ts
+++ b/packages/playwright-core/src/tools/dashboard/dashboardController.ts
@@ -202,10 +202,10 @@ export class DashboardConnection implements Transport, DashboardChannel {
);
const size = { width: 1280, height: 800 };
- await page.screencast.start(
- ({ data }) => this._writeFrame(data, page.viewportSize()?.width ?? 0, page.viewportSize()?.height ?? 0),
- { size },
- );
+ await page.screencast.start({
+ onFrame: ({ data }: { data: Buffer }) => this._writeFrame(data, page.viewportSize()?.width ?? 0, page.viewportSize()?.height ?? 0),
+ size,
+ });
}
private _deselectPage() {
diff --git a/packages/playwright-core/src/utils/isomorphic/protocolMetainfo.ts b/packages/playwright-core/src/utils/isomorphic/protocolMetainfo.ts
index bdbf457ede..1795985a38 100644
--- a/packages/playwright-core/src/utils/isomorphic/protocolMetainfo.ts
+++ b/packages/playwright-core/src/utils/isomorphic/protocolMetainfo.ts
@@ -152,10 +152,8 @@ export const methodMetainfo = new Map<string, MethodMetainfo>([
['Page.overlayRemove', { title: 'Remove overlay', group: 'configuration', }],
['Page.overlayChapter', { title: 'Show chapter overlay', group: 'configuration', }],
['Page.overlaySetVisible', { title: 'Set overlay visibility', group: 'configuration', }],
- ['Page.startScreencast', { title: 'Start screencast', group: 'configuration', }],
- ['Page.stopScreencast', { title: 'Stop screencast', group: 'configuration', }],
- ['Page.videoStart', { title: 'Start video recording', group: 'configuration', }],
- ['Page.videoStop', { title: 'Stop video recording', group: 'configuration', }],
+ ['Page.screencastStart', { title: 'Start screencast', group: 'configuration', }],
+ ['Page.screencastStop', { title: 'Stop screencast', group: 'configuration', }],
['Page.updateSubscription', { internal: true, }],
['Page.setDockTile', { internal: true, }],
['Frame.evalOnSelector', { title: 'Evaluate', snapshot: true, pause: true, }],
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index 06b9a192ac..afd509576b 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -16185,22 +16185,35 @@ export interface WebSocketRoute {
*/
export interface Screencast {
/**
- * Starts capturing screencast frames.
+ * Starts the screencast. When [`path`](https://playwright.dev/docs/api/class-screencast#screencast-start-option-path)
+ * is provided, it saves video recording to the specified file. When
+ * [`onFrame`](https://playwright.dev/docs/api/class-screencast#screencast-start-option-on-frame) is provided,
+ * delivers JPEG-encoded frames to the callback. Both can be used together.
*
* **Usage**
*
* ```js
- * await page.screencast.start(({ data }) => {
- * console.log(`frame size: ${data.length}`);
- * }, { size: { width: 800, height: 600 } });
+ * // Record video
+ * await page.screencast.start({ path: 'video.webm', size: { width: 1280, height: 800 } });
+ * // ... perform actions ...
+ * await page.screencast.stop();
+ * ```
+ *
+ * ```js
+ * // Capture frames
+ * await page.screencast.start({
+ * onFrame: ({ data }) => console.log(`frame size: ${data.length}`),
+ * size: { width: 800, height: 600 },
+ * });
* // ... perform actions ...
* await page.screencast.stop();
* ```
*
- * @param onFrame Callback that receives JPEG-encoded frame data.
* @param options
*/
- start(onFrame: ((frame: { data: Buffer }) => Promise<any>|any), options?: {
+ start(options?: {
+ onFrame?: (frame: { data: Buffer }) => Promise<any>|any;
+ path?: string;
size?: {
width: number;
height: number;
@@ -16255,60 +16268,10 @@ export interface Screencast {
showOverlays(): Promise<void>;
/**
- * Starts video recording. This method is mutually exclusive with the `recordVideo` context option.
- * @param path Path where the video should be saved when the recording is stopped.
- * @param options
- */
- startRecording(path: string, options?: {
- /**
- * If specified, enables visual annotations on interacted elements during video recording. Interacted elements are
- * highlighted with a semi-transparent blue box and click points are shown as red circles.
- */
- annotate?: {
- /**
- * How long each annotation is displayed in milliseconds. Defaults to `500`.
- */
- duration?: number;
-
- /**
- * Position of the action title overlay. Defaults to `"top-right"`.
- */
- position?: "top-left"|"top"|"top-right"|"bottom-left"|"bottom"|"bottom-right";
-
- /**
- * Font size of the action title in pixels. Defaults to `24`.
- */
- fontSize?: number;
- };
-
- /**
- * Optional dimensions of the recorded video. If not specified the size will be equal to page viewport scaled down to
- * fit into 800×800. Actual picture of the page will be scaled down if necessary to fit the specified size.
- */
- size?: {
- /**
- * Video frame width.
- */
- width: number;
-
- /**
- * Video frame height.
- */
- height: number;
- };
- }): Promise<Disposable>;
-
- /**
- * Stops the screencast started with
- * [screencast.start(onFrame[, options])](https://playwright.dev/docs/api/class-screencast#screencast-start).
+ * Stops the screencast and video recording if active. If a video was being recorded, saves it to the path specified
+ * in [screencast.start([options])](https://playwright.dev/docs/api/class-screencast#screencast-start).
*/
stop(): Promise<void>;
-
- /**
- * Stops video recording started with
- * [screencast.startRecording(path[, options])](https://playwright.dev/docs/api/class-screencast#screencast-start-recording).
- */
- stopRecording(): Promise<void>;
}
type DeviceDescriptor = {
diff --git a/packages/protocol/src/channels.d.ts b/packages/protocol/src/channels.d.ts
index c99e774029..b895f41528 100644
--- a/packages/protocol/src/channels.d.ts
+++ b/packages/protocol/src/channels.d.ts
@@ -2164,10 +2164,8 @@ export interface PageChannel extends PageEventTarget, EventTargetChannel {
overlayRemove(params: PageOverlayRemoveParams, progress?: Progress): Promise<PageOverlayRemoveResult>;
overlayChapter(params: PageOverlayChapterParams, progress?: Progress): Promise<PageOverlayChapterResult>;
overlaySetVisible(params: PageOverlaySetVisibleParams, progress?: Progress): Promise<PageOverlaySetVisibleResult>;
- startScreencast(params: PageStartScreencastParams, progress?: Progress): Promise<PageStartScreencastResult>;
- stopScreencast(params?: PageStopScreencastParams, progress?: Progress): Promise<PageStopScreencastResult>;
- videoStart(params: PageVideoStartParams, progress?: Progress): Promise<PageVideoStartResult>;
- videoStop(params?: PageVideoStopParams, progress?: Progress): Promise<PageVideoStopResult>;
+ screencastStart(params: PageScreencastStartParams, progress?: Progress): Promise<PageScreencastStartResult>;
+ screencastStop(params?: PageScreencastStopParams, progress?: Progress): Promise<PageScreencastStopResult>;
updateSubscription(params: PageUpdateSubscriptionParams, progress?: Progress): Promise<PageUpdateSubscriptionResult>;
setDockTile(params: PageSetDockTileParams, progress?: Progress): Promise<PageSetDockTileResult>;
}
@@ -2717,44 +2715,32 @@ export type PageOverlaySetVisibleOptions = {
};
export type PageOverlaySetVisibleResult = void;
-export type PageStartScreencastParams = {
+export type PageScreencastStartParams = {
size?: {
width: number,
height: number,
},
quality?: number,
-};
-export type PageStartScreencastOptions = {
- size?: {
- width: number,
- height: number,
- },
- quality?: number,
-};
-export type PageStartScreencastResult = void;
-export type PageStopScreencastParams = {};
-export type PageStopScreencastOptions = {};
-export type PageStopScreencastResult = void;
-export type PageVideoStartParams = {
- size?: {
- width: number,
- height: number,
- },
+ sendFrames?: boolean,
+ record?: boolean,
annotate?: AnnotateOptions,
};
-export type PageVideoStartOptions = {
+export type PageScreencastStartOptions = {
size?: {
width: number,
height: number,
},
+ quality?: number,
+ sendFrames?: boolean,
+ record?: boolean,
annotate?: AnnotateOptions,
};
-export type PageVideoStartResult = {
- artifact: ArtifactChannel,
+export type PageScreencastStartResult = {
+ artifact?: ArtifactChannel,
};
-export type PageVideoStopParams = {};
-export type PageVideoStopOptions = {};
-export type PageVideoStopResult = void;
+export type PageScreencastStopParams = {};
+export type PageScreencastStopOptions = {};
+export type PageScreencastStopResult = void;
export type PageUpdateSubscriptionParams = {
event: 'console' | 'dialog' | 'fileChooser' | 'request' | 'response' | 'requestFinished' | 'requestFailed',
enabled: boolean,
diff --git a/packages/protocol/src/protocol.yml b/packages/protocol/src/protocol.yml
index 57149667d4..9dea9482a5 100644
--- a/packages/protocol/src/protocol.yml
+++ b/packages/protocol/src/protocol.yml
@@ -2137,7 +2137,7 @@ Page:
parameters:
visible: boolean
- startScreencast:
+ screencastStart:
title: Start screencast
group: configuration
parameters:
@@ -2147,26 +2147,14 @@ Page:
width: int
height: int
quality: int?
-
- stopScreencast:
- title: Stop screencast
- group: configuration
-
- videoStart:
- title: Start video recording
- group: configuration
- parameters:
- size:
- type: object?
- properties:
- width: int
- height: int
+ sendFrames: boolean?
+ record: boolean?
annotate: AnnotateOptions?
returns:
- artifact: Artifact
+ artifact: Artifact?
- videoStop:
- title: Stop video recording
+ screencastStop:
+ title: Stop screencast
group: configuration
updateSubscription:
diff --git a/tests/library/screencast.spec.ts b/tests/library/screencast.spec.ts
index 7fd959e3cc..db57adfee5 100644
--- a/tests/library/screencast.spec.ts
+++ b/tests/library/screencast.spec.ts
@@ -14,8 +14,12 @@
* limitations under the License.
*/
+import fs from 'fs';
+import path from 'path';
import { expect, browserTest as test } from '../config/browserTest';
import { rafraf } from '../config/utils';
+import { kTargetClosedErrorMessage } from '../config/errors';
+import { VideoPlayer } from './videoPlayer';
test.skip(({ mode }) => mode !== 'default', 'screencast is not available in remote mode');
test.skip(({ video }) => video === 'on', 'conflicts with built-in video recording');
@@ -27,7 +31,7 @@ test('screencast.start delivers frames via onFrame callback', async ({ browser,
const frames: Buffer[] = [];
const size = { width: 500, height: 400 };
- await page.screencast.start(({ data }) => frames.push(data), { size });
+ await page.screencast.start({ onFrame: ({ data }) => frames.push(data), size });
await page.goto(server.EMPTY_PAGE);
await page.evaluate(() => document.body.style.backgroundColor = 'red');
await rafraf(page, 100);
@@ -51,8 +55,8 @@ test('start throws if screencast is already started', async ({ browser }) => {
const context = await browser.newContext({ viewport: { width: 500, height: 400 } });
const page = await context.newPage();
- await page.screencast.start(() => {});
- await expect(page.screencast.start(() => {})).rejects.toThrow('Screencast is already started');
+ await page.screencast.start({ onFrame: () => {} });
+ await expect(page.screencast.start({ onFrame: () => {} })).rejects.toThrow('Screencast is already started');
await page.screencast.stop();
await context.close();
@@ -64,67 +68,197 @@ test('start allows restart with different options after stop', async ({ browser,
const context = await browser.newContext({ viewport: { width: 500, height: 400 } });
const page = await context.newPage();
- await page.screencast.start(() => {}, { size: { width: 500, height: 400 } });
+ await page.screencast.start({ onFrame: () => {}, size: { width: 500, height: 400 } });
await page.screencast.stop();
// Different options should succeed once the previous screencast is stopped.
- await page.screencast.start(() => {}, { size: { width: 320, height: 240 } });
+ await page.screencast.start({ onFrame: () => {}, size: { width: 320, height: 240 } });
await page.screencast.stop();
await context.close();
});
-test('start reuses existing screencast when video recording is running', async ({ browser, server, trace }, testInfo) => {
- test.skip(trace === 'on', 'trace=on enables screencast with different options');
-
- const videoSize = { width: 500, height: 400 };
- const context = await browser.newContext({ viewport: videoSize });
+test('start returns a disposable that stops screencast', async ({ browser, server, trace }) => {
+ test.skip(trace === 'on', 'trace=on has different screencast image configuration');
+ const context = await browser.newContext({ viewport: { width: 500, height: 400 } });
const page = await context.newPage();
- await page.screencast.startRecording(testInfo.outputPath('video.webm'), { size: videoSize });
-
const frames: Buffer[] = [];
- await page.screencast.start(({ data }) => frames.push(data), { size: { width: 320, height: 240 } });
+ await page.screencast.start({ onFrame: ({ data }) => frames.push(data), size: { width: 500, height: 400 } });
await page.goto(server.EMPTY_PAGE);
+ await page.evaluate(() => document.body.style.backgroundColor = 'red');
await rafraf(page, 100);
await page.screencast.stop();
- expect(frames.length).toBeGreaterThan(0);
- for (const frame of frames) {
- // Each frame must be a valid JPEG (starts with FF D8)
- expect(frame[0]).toBe(0xff);
- expect(frame[1]).toBe(0xd8);
- const { width, height } = jpegDimensions(frame);
- // Frame should be scaled down to fit the maximum size.
- expect(width).toBe(videoSize.width);
- expect(height).toBe(videoSize.height);
- }
+ const frameCountAfterDispose = frames.length;
+ expect(frameCountAfterDispose).toBeGreaterThan(0);
+
+ // No more frames should arrive after dispose.
+ await page.evaluate(() => document.body.style.backgroundColor = 'blue');
+ await rafraf(page, 100);
+ expect(frames.length).toBe(frameCountAfterDispose);
- await page.screencast.stopRecording();
await context.close();
});
-test('start returns a disposable that stops screencast', async ({ browser, server, trace }) => {
- test.skip(trace === 'on', 'trace=on has different screencast image configuration');
- const context = await browser.newContext({ viewport: { width: 500, height: 400 } });
+test('start/stop twice without path creates two files in artifactsDir', async ({ browserType }, testInfo) => {
+ test.slow();
+ const artifactsDir = testInfo.outputPath('artifacts');
+ const browser = await browserType.launch({ artifactsDir });
+ const size = { width: 800, height: 800 };
+ const context = await browser.newContext({ viewport: size });
const page = await context.newPage();
- const frames: Buffer[] = [];
- await page.screencast.start(({ data }) => frames.push(data), { size: { width: 500, height: 400 } });
- await page.goto(server.EMPTY_PAGE);
+ await page.screencast.start({ path: testInfo.outputPath('video1.webm'), size });
await page.evaluate(() => document.body.style.backgroundColor = 'red');
await rafraf(page, 100);
await page.screencast.stop();
- const frameCountAfterDispose = frames.length;
- expect(frameCountAfterDispose).toBeGreaterThan(0);
+ await page.screencast.start({ path: testInfo.outputPath('video2.webm'), size });
+ await page.evaluate(() => document.body.style.backgroundColor = 'blue');
+ await rafraf(page, 100);
+ await page.screencast.stop();
- // No more frames should arrive after dispose.
+ const videoFiles = fs.readdirSync(artifactsDir).filter(f => f.endsWith('.webm'));
+ expect(videoFiles).toHaveLength(2);
+
+ await context.close();
+ await browser.close();
+});
+
+test('start should work when recordVideo is set', async ({ browser }, testInfo) => {
+ test.slow();
+ const autoDir = testInfo.outputPath('auto');
+ const manualDir = testInfo.outputPath('manual');
+ const context = await browser.newContext({
+ recordVideo: {
+ dir: autoDir,
+ },
+ });
+ const page = await context.newPage();
+
+ await page.screencast.start({ path: path.join(manualDir, 'video.webm') });
await page.evaluate(() => document.body.style.backgroundColor = 'blue');
await rafraf(page, 100);
- expect(frames.length).toBe(frameCountAfterDispose);
+ await page.screencast.stop();
+ const videoFiles1 = fs.readdirSync(manualDir).filter(f => f.endsWith('.webm'));
+ expect(videoFiles1).toHaveLength(1);
+
+ await context.close();
+ const videoFiles2 = fs.readdirSync(autoDir).filter(f => f.endsWith('.webm'));
+ expect(videoFiles2).toHaveLength(1);
+});
+
+test('start should fail when another recording is in progress', async ({ page, trace }, testInfo) => {
+ test.skip(trace === 'on', 'trace=on has different screencast image configuration');
+ await page.screencast.start({ path: testInfo.outputPath('video.webm') });
+ const error = await page.screencast.start({ path: testInfo.outputPath('video2.webm') }).catch(e => e);
+ expect(error.message).toContain('Screencast is already started');
+});
+
+test('stop should not fail when no recording is in progress', async ({ browser }) => {
+ const context = await browser.newContext();
+ const page = await context.newPage();
+ // stop() is a no-op when screencast is not started.
+ await page.screencast.stop();
+ await context.close();
+});
+
+test('start should finish when page is closed', async ({ browser }, testInfo) => {
+ const context = await browser.newContext();
+ const page = await context.newPage();
+ const videoPath = testInfo.outputPath('video.webm');
+ await page.screencast.start({ path: videoPath, size: { width: 800, height: 800 } });
+ await page.evaluate(() => document.body.style.backgroundColor = 'red');
+ await rafraf(page, 100);
+ await page.close();
+ const error = await page.screencast.stop().catch(e => e);
+ expect(error.message).toContain(kTargetClosedErrorMessage);
+ await context.close();
+});
+
+test('empty video', async ({ browser }, testInfo) => {
+ test.slow();
+ const size = { width: 800, height: 800 };
+ const context = await browser.newContext({ viewport: size });
+ const page = await context.newPage();
+ const videoPath = testInfo.outputPath('empty-video.webm');
+ await page.screencast.start({ path: videoPath, size });
+ await page.screencast.stop();
+ await context.close();
+ expectFrames(videoPath, size, isAlmostWhite);
+});
+test('start dispose stops recording', async ({ browser }, testInfo) => {
+ test.slow();
+ const size = { width: 800, height: 800 };
+ const context = await browser.newContext({ viewport: size });
+ const page = await context.newPage();
+ const videoPath = testInfo.outputPath('dispose-video.webm');
+ const disposable = await page.screencast.start({ path: videoPath, size });
+ await page.evaluate(() => document.body.style.backgroundColor = 'red');
+ await rafraf(page, 100);
+ await disposable.dispose();
+ expectRedFrames(videoPath, size);
await context.close();
});
+type Pixel = { r: number, g: number, b: number, alpha: number };
+type PixelPredicate = (pixel: Pixel) => boolean;
+
+function isAlmostWhite({ r, g, b, alpha }: Pixel): boolean {
+ return r > 185 && g > 185 && b > 185 && alpha === 255;
+}
+
+function isAlmostRed({ r, g, b, alpha }: Pixel): boolean {
+ return r > 185 && g < 70 && b < 70 && alpha === 255;
+}
+
+function findPixel(pixels: Buffer, pixelPredicate: PixelPredicate): Pixel|undefined {
+ for (let i = 0, n = pixels.length; i < n; i += 4) {
+ const pixel = {
+ r: pixels[i],
+ g: pixels[i + 1],
+ b: pixels[i + 2],
+ alpha: pixels[i + 3],
+ };
+ if (pixelPredicate(pixel))
+ return pixel;
+ }
+ return undefined;
+}
+
+function expectAll(pixels: Buffer, pixelPredicate: PixelPredicate) {
+ const badPixel = findPixel(pixels, pixel => !pixelPredicate(pixel));
+ if (!badPixel)
+ return;
+ const rgba = [badPixel.r, badPixel.g, badPixel.b, badPixel.alpha].join(', ');
+ throw new Error([
+ `Expected all pixels to satisfy ${pixelPredicate.name}, found bad pixel (${rgba})`,
+ `Actual pixels=[${pixels.join(',')}]`,
+ ].join('\n'));
+}
+
+function expectRedFrames(videoFile: string, size: { width: number, height: number }) {
+ expectFrames(videoFile, size, isAlmostRed);
+}
+
+function expectFrames(videoFile: string, size: { width: number, height: number }, pixelPredicate: PixelPredicate) {
+ const videoPlayer = new VideoPlayer(videoFile);
+ const duration = videoPlayer.duration;
+ expect(duration).toBeGreaterThan(0);
+
+ expect(videoPlayer.videoWidth).toBe(size.width);
+ expect(videoPlayer.videoHeight).toBe(size.height);
+
+ {
+ const pixels = videoPlayer.seekLastFrame().data;
+ expectAll(pixels, pixelPredicate);
+ }
+ {
+ const pixels = videoPlayer.seekLastFrame({ x: size.width - 20, y: 10 }).data;
+ expectAll(pixels, pixelPredicate);
+ }
+}
+
function jpegDimensions(buffer: Buffer): { width: number, height: number } {
let i = 2; // skip SOI marker (FF D8)
while (i < buffer.length - 8) {
diff --git a/tests/library/video.spec.ts b/tests/library/video.spec.ts
index 45700bf6dd..2027f82cb5 100644
--- a/tests/library/video.spec.ts
+++ b/tests/library/video.spec.ts
@@ -14,78 +14,16 @@
* limitations under the License.
*/
-import { spawnSync } from 'child_process';
import fs from 'fs';
import path from 'path';
-import { PNG, jpegjs } from 'playwright-core/lib/utilsBundle';
-import { registry } from '../../packages/playwright-core/lib/server';
+import { jpegjs } from 'playwright-core/lib/utilsBundle';
import { expect, browserTest as it } from '../config/browserTest';
import { parseTraceRaw, rafraf } from '../config/utils';
-import { kTargetClosedErrorMessage } from '../config/errors';
-
-export class VideoPlayer {
- fileName: string;
- output: string;
- duration: number;
- frames: number;
- videoWidth: number;
- videoHeight: number;
- cache = new Map<number, any>();
-
- constructor(fileName: string) {
- this.fileName = fileName;
- const ffmpeg = registry.findExecutable('ffmpeg')!.executablePathOrDie('javascript');
- // Force output frame rate to 25 fps as otherwise it would produce one image per timebase unit
- // which is 1 / (25 * 1000).
- this.output = spawnSync(ffmpeg, ['-i', this.fileName, '-r', '25', `${this.fileName}-%04d.png`]).stderr.toString();
-
- const lines = this.output.split('\n');
- let framesLine = lines.find(l => l.startsWith('frame='))!;
- if (!framesLine)
- throw new Error(`No frame data in the output:\n${this.output}`);
- framesLine = framesLine.substring(framesLine.lastIndexOf('frame='));
- const framesMatch = framesLine.match(/frame=\s+(\d+)/);
- const streamLine = lines.find(l => l.trim().startsWith('Stream #0:0'));
- const resolutionMatch = streamLine.match(/, (\d+)x(\d+),/);
- const durationMatch = lines.find(l => l.trim().startsWith('Duration'))!.match(/Duration: (\d+):(\d\d):(\d\d.\d\d)/);
- this.duration = (((parseInt(durationMatch![1], 10) * 60) + parseInt(durationMatch![2], 10)) * 60 + parseFloat(durationMatch![3])) * 1000;
- this.frames = parseInt(framesMatch![1], 10);
- this.videoWidth = parseInt(resolutionMatch![1], 10);
- this.videoHeight = parseInt(resolutionMatch![2], 10);
- }
-
- findFrame(framePredicate: (pixels: Buffer) => boolean, offset?: { x: number, y: number }): any |undefined {
- for (let f = 1; f <= this.frames; ++f) {
- const frame = this.frame(f, offset);
- if (framePredicate(frame.data))
- return frame;
- }
- }
-
- seekLastFrame(offset?: { x: number, y: number }): any {
- return this.frame(this.frames, offset);
- }
-
- frame(frame: number, offset = { x: 10, y: 10 }): any {
- if (!this.cache.has(frame)) {
- const gap = '0'.repeat(4 - String(frame).length);
- const buffer = fs.readFileSync(`${this.fileName}-${gap}${frame}.png`);
- this.cache.set(frame, PNG.sync.read(buffer));
- }
- const decoded = this.cache.get(frame);
- const dst = new PNG({ width: 10, height: 10 });
- PNG.bitblt(decoded, dst, offset.x, offset.y, 10, 10, 0, 0);
- return dst;
- }
-}
+import { VideoPlayer } from './videoPlayer';
type Pixel = { r: number, g: number, b: number, alpha: number };
type PixelPredicate = (pixel: Pixel) => boolean;
-function isAlmostWhite({ r, g, b, alpha }: Pixel): boolean {
- return r > 185 && g > 185 && b > 185 && alpha === 255;
-}
-
function isAlmostRed({ r, g, b, alpha }: Pixel): boolean {
return r > 185 && g < 70 && b < 70 && alpha === 255;
}
@@ -825,126 +763,6 @@ it.describe('screencast', () => {
expect(isAlmostRed(pixel)).toBe(true);
});
- it('video.start/stop twice', async ({ browser }, testInfo) => {
- const size = { width: 800, height: 800 };
- const context = await browser.newContext({ viewport: size });
- const page = await context.newPage();
-
- const videoPath1 = testInfo.outputPath('video1.webm');
- await page.screencast.startRecording(videoPath1, { size });
- await page.evaluate(() => document.body.style.backgroundColor = 'red');
- await rafraf(page, 100);
- await page.screencast.stopRecording();
- expectRedFrames(videoPath1, size);
-
- const videoPath2 = testInfo.outputPath('video2.webm');
- await page.screencast.startRecording(videoPath2, { size });
- await page.evaluate(() => document.body.style.backgroundColor = 'rgb(100,100,100)');
- await rafraf(page, 100);
- await page.screencast.stopRecording();
- expectFrames(videoPath2, size, isAlmostGray);
-
- await context.close();
- });
-
- it('video.start/stop twice without path creates two files in artifactsDir', async ({ browserType }, testInfo) => {
- const artifactsDir = testInfo.outputPath('artifacts');
- const browser = await browserType.launch({ artifactsDir });
- const size = { width: 800, height: 800 };
- const context = await browser.newContext({ viewport: size });
- const page = await context.newPage();
-
- await page.screencast.startRecording(testInfo.outputPath('video1.webm'), { size });
- await page.evaluate(() => document.body.style.backgroundColor = 'red');
- await rafraf(page, 100);
- await page.screencast.stopRecording();
-
- await page.screencast.startRecording(testInfo.outputPath('video2.webm'), { size });
- await page.evaluate(() => document.body.style.backgroundColor = 'blue');
- await rafraf(page, 100);
- await page.screencast.stopRecording();
-
- const videoFiles = fs.readdirSync(artifactsDir).filter(f => f.endsWith('.webm'));
- expect(videoFiles).toHaveLength(2);
-
- await context.close();
- await browser.close();
- });
-
- it('video.start should work when recordVideo is set', async ({ browser }, testInfo) => {
- const autoDir = testInfo.outputPath('auto');
- const manualDir = testInfo.outputPath('manual');
- const context = await browser.newContext({
- recordVideo: {
- dir: autoDir,
- },
- });
- const page = await context.newPage();
-
- await page.screencast.startRecording(path.join(manualDir, 'video.webm'));
- await page.evaluate(() => document.body.style.backgroundColor = 'blue');
- await rafraf(page, 100);
- await page.screencast.stopRecording();
- const videoFiles1 = fs.readdirSync(manualDir).filter(f => f.endsWith('.webm'));
- expect(videoFiles1).toHaveLength(1);
-
- await context.close();
- const videoFiles2 = fs.readdirSync(autoDir).filter(f => f.endsWith('.webm'));
- expect(videoFiles2).toHaveLength(1);
- });
-
- it('video.start should fail when another recording is in progress', async ({ page, trace }, testInfo) => {
- it.skip(trace === 'on', 'trace=on has different screencast image configuration');
- await page.screencast.startRecording(testInfo.outputPath('video.webm'));
- const error = await page.screencast.startRecording(testInfo.outputPath('video2.webm')).catch(e => e);
- expect(error.message).toContain('Video is already being recorded');
- });
-
- it('video.stop should fail when no recording is in progress', async ({ browser }, testInfo) => {
- const context = await browser.newContext();
- const page = await context.newPage();
- const error = await page.screencast.stopRecording().catch(e => e);
- expect(error.message).toContain('Video is not being recorded');
- await context.close();
- });
-
- it('video.start should finish when page is closed', async ({ browser }, testInfo) => {
- const context = await browser.newContext();
- const page = await context.newPage();
- const videoPath = testInfo.outputPath('video.webm');
- await page.screencast.startRecording(videoPath, { size: { width: 800, height: 800 } });
- await page.evaluate(() => document.body.style.backgroundColor = 'red');
- await rafraf(page, 100);
- await page.close();
- const error = await page.screencast.stopRecording().catch(e => e);
- expect(error.message).toContain(kTargetClosedErrorMessage);
- await context.close();
- });
-
- it('empty video', async ({ browser }, testInfo) => {
- const size = { width: 800, height: 800 };
- const context = await browser.newContext({ viewport: size });
- const page = await context.newPage();
- const videoPath = testInfo.outputPath('empty-video.webm');
- await page.screencast.startRecording(videoPath, { size });
- await page.screencast.stopRecording();
- await context.close();
- expectFrames(videoPath, size, isAlmostWhite);
- });
-
- it('video.start dispose stops recording', async ({ browser }, testInfo) => {
- const size = { width: 800, height: 800 };
- const context = await browser.newContext({ viewport: size });
- const page = await context.newPage();
- const videoPath = testInfo.outputPath('dispose-video.webm');
- const disposable = await page.screencast.startRecording(videoPath, { size });
- await page.evaluate(() => document.body.style.backgroundColor = 'red');
- await rafraf(page, 100);
- await disposable.dispose();
- expectRedFrames(videoPath, size);
- await context.close();
- });
-
});
it('should saveAs video', async ({ browser }, testInfo) => {
diff --git a/tests/library/videoPlayer.ts b/tests/library/videoPlayer.ts
new file mode 100644
index 0000000000..a75f7ff888
--- /dev/null
+++ b/tests/library/videoPlayer.ts
@@ -0,0 +1,76 @@
+/**
+ * Copyright Microsoft Corporation. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { spawnSync } from 'child_process';
+import fs from 'fs';
+import { PNG } from 'playwright-core/lib/utilsBundle';
+import { registry } from '../../packages/playwright-core/lib/server';
+
+export class VideoPlayer {
+ fileName: string;
+ output: string;
+ duration: number;
+ frames: number;
+ videoWidth: number;
+ videoHeight: number;
+ cache = new Map<number, any>();
+
+ constructor(fileName: string) {
+ this.fileName = fileName;
+ const ffmpeg = registry.findExecutable('ffmpeg')!.executablePathOrDie('javascript');
+ // Force output frame rate to 25 fps as otherwise it would produce one image per timebase unit
+ // which is 1 / (25 * 1000).
+ this.output = spawnSync(ffmpeg, ['-i', this.fileName, '-r', '25', `${this.fileName}-%04d.png`]).stderr.toString();
+
+ const lines = this.output.split('\n');
+ let framesLine = lines.find(l => l.startsWith('frame='))!;
+ if (!framesLine)
+ throw new Error(`No frame data in the output:\n${this.output}`);
+ framesLine = framesLine.substring(framesLine.lastIndexOf('frame='));
+ const framesMatch = framesLine.match(/frame=\s+(\d+)/);
+ const streamLine = lines.find(l => l.trim().startsWith('Stream #0:0'));
+ const resolutionMatch = streamLine.match(/, (\d+)x(\d+),/);
+ const durationMatch = lines.find(l => l.trim().startsWith('Duration'))!.match(/Duration: (\d+):(\d\d):(\d\d.\d\d)/);
+ this.duration = (((parseInt(durationMatch![1], 10) * 60) + parseInt(durationMatch![2], 10)) * 60 + parseFloat(durationMatch![3])) * 1000;
+ this.frames = parseInt(framesMatch![1], 10);
+ this.videoWidth = parseInt(resolutionMatch![1], 10);
+ this.videoHeight = parseInt(resolutionMatch![2], 10);
+ }
+
+ findFrame(framePredicate: (pixels: Buffer) => boolean, offset?: { x: number, y: number }): any |undefined {
+ for (let f = 1; f <= this.frames; ++f) {
+ const frame = this.frame(f, offset);
+ if (framePredicate(frame.data))
+ return frame;
+ }
+ }
+
+ seekLastFrame(offset?: { x: number, y: number }): any {
+ return this.frame(this.frames, offset);
+ }
+
+ frame(frame: number, offset = { x: 10, y: 10 }): any {
+ if (!this.cache.has(frame)) {
+ const gap = '0'.repeat(4 - String(frame).length);
+ const buffer = fs.readFileSync(`${this.fileName}-${gap}${frame}.png`);
+ this.cache.set(frame, PNG.sync.read(buffer));
+ }
+ const decoded = this.cache.get(frame);
+ const dst = new PNG({ width: 10, height: 10 });
+ PNG.bitblt(decoded, dst, offset.x, offset.y, 10, 10, 0, 0);
+ return dst;
+ }
+}
diff --git a/utils/generate_types/overrides.d.ts b/utils/generate_types/overrides.d.ts
index d5b9397ccf..7d86dd472f 100644
--- a/utils/generate_types/overrides.d.ts
+++ b/utils/generate_types/overrides.d.ts
@@ -243,7 +243,9 @@ export interface WebSocketRoute {
}
export interface Screencast {
- start(onFrame: ((frame: { data: Buffer }) => Promise<any>|any), options?: {
+ start(options?: {
+ onFrame?: (frame: { data: Buffer }) => Promise<any>|any;
+ path?: string;
size?: {
width: number;
height: number;