Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore: encode video with ffmpeg in WebKit (#38479)
Yury Semikhatsky4c9c7eb5c6c4
diff --git a/packages/playwright-core/src/server/bidi/bidiPage.ts b/packages/playwright-core/src/server/bidi/bidiPage.tsindex 23c7f477a0..501ff620c9 100644--- a/packages/playwright-core/src/server/bidi/bidiPage.ts+++ b/packages/playwright-core/src/server/bidi/bidiPage.ts@@ -535,7 +535,10 @@ export class BidiPage implements PageDelegate {});}- async setScreencastOptions(options: { width: number, height: number, quality: number } | null): Promise<void> {+ async startScreencast(options: { width: number, height: number, quality: number }): Promise<void> {+ }++ async stopScreencast(): Promise<void> {}rafCountForStablePosition(): number {diff --git a/packages/playwright-core/src/server/chromium/crBrowser.ts b/packages/playwright-core/src/server/chromium/crBrowser.tsindex 52ac6ff7a5..edb5bf3479 100644--- a/packages/playwright-core/src/server/chromium/crBrowser.ts+++ b/packages/playwright-core/src/server/chromium/crBrowser.ts@@ -554,7 +554,7 @@ export class CRBrowserContext extends BrowserContext {}async stopVideoRecording() {- await Promise.all(this._crPages().map(crPage => crPage._mainFrameSession._stopVideoRecording()));+ await Promise.all(this._crPages().map(crPage => crPage._page.screencast.stopVideoRecording()));}onClosePersistent() {diff --git a/packages/playwright-core/src/server/chromium/crPage.ts b/packages/playwright-core/src/server/chromium/crPage.tsindex e29da41156..aa2a6b42fb 100644--- a/packages/playwright-core/src/server/chromium/crPage.ts+++ b/packages/playwright-core/src/server/chromium/crPage.ts@@ -15,10 +15,7 @@* limitations under the License.*/-import path from 'path';-import { assert } from '../../utils/isomorphic/assert';-import { createGuid } from '../utils/crypto';import { eventsHelper } from '../utils/eventsHelper';import { rewriteErrorMessage } from '../../utils/isomorphic/stackTrace';import * as dialog from '../dialog';@@ -27,7 +24,6 @@ import * as frames from '../frames';import { helper } from '../helper';import * as network from '../network';import { Page, PageBinding, Worker } from '../page';-import { registry } from '../registry';import { CRBrowserContext } from './crBrowser';import { CRCoverage } from './crCoverage';import { DragManager } from './crDragDrop';@@ -37,7 +33,6 @@ import { CRNetworkManager } from './crNetworkManager';import { CRPDF } from './crPdf';import { exceptionToError, releaseObject, toConsoleMessageLocation } from './crProtocolHelper';import { platformToFontFamilies } from './defaultFontFamilies';-import { VideoRecorder } from './videoRecorder';import { TargetClosedError } from '../errors';import { isSessionClosedError } from '../protocolError';@@ -289,17 +284,17 @@ export class CRPage implements PageDelegate {return this._sessionForHandle(handle)._scrollRectIntoViewIfNeeded(handle, rect);}- async setScreencastOptions(options: { width: number, height: number, quality: number } | null): Promise<void> {- if (options) {- await this._mainFrameSession._startScreencast(this, {- format: 'jpeg',- quality: options.quality,- maxWidth: options.width,- maxHeight: options.height- });- } else {- await this._mainFrameSession._stopScreencast(this);- }+ async startScreencast(options: { width: number; height: number; quality: number; }): Promise<void> {+ await this._mainFrameSession._client.send('Page.startScreencast', {+ format: 'jpeg',+ quality: options.quality,+ maxWidth: options.width,+ maxHeight: options.height,+ });+ }++ async stopScreencast() {+ await this._mainFrameSession._client._sendMayFail('Page.stopScreencast');}rafCountForStablePosition(): number {@@ -379,9 +374,6 @@ class FrameSession {// Marks the oopif session that remote -> local transition has happened in the parent.// See Target.detachedFromTarget handler for details.private _swappedIn = false;- private _videoRecorder: VideoRecorder | null = null;- private _screencastId: string | null = null;- private _screencastClients = new Set<any>();private _metricsOverride: Protocol.Emulation.setDeviceMetricsOverrideParameters | undefined;private _workerSessions = new Map<string, CRSession>();private _initScriptIds = new Map<InitScript, string>();@@ -446,23 +438,8 @@ class FrameSession {}let screencastOptions: types.PageScreencastOptions | undefined;- if (!this._page.isStorageStatePage && this._isMainFrame() && this._crPage._browserContext._options.recordVideo && hasUIWindow) {- const screencastId = createGuid();- const outputFile = path.join(this._crPage._browserContext._options.recordVideo.dir, screencastId + '.webm');- screencastOptions = {- // validateBrowserContextOptions ensures correct video size.- ...this._crPage._browserContext._options.recordVideo.size!,- outputFile,- };- await this._crPage._browserContext._ensureVideosPath();- // Note: it is important to start video recorder before sending Page.startScreencast,- // and it is equally important to send Page.startScreencast before sending Runtime.runIfWaitingForDebugger.- await this._createVideoRecorder(screencastId, screencastOptions);- this._crPage._page.waitForInitializedOrError().then(p => {- if (p instanceof Error)- this._stopVideoRecording().catch(() => {});- });- }+ if (!this._page.isStorageStatePage && this._isMainFrame() && hasUIWindow)+ screencastOptions = await this._crPage._page.screencast.initializeVideoRecorder();let lifecycleEventsEnabled: Promise<any>;if (!this._isMainFrame())@@ -552,7 +529,7 @@ class FrameSession {for (const initScript of this._crPage._page.allInitScripts())promises.push(this._evaluateOnNewDocument(initScript, 'main', true /* runImmediately */));if (screencastOptions)- promises.push(this._startVideoRecording(screencastOptions));+ promises.push(this._crPage._page.screencast.startVideoRecording(screencastOptions));}promises.push(this._client.send('Runtime.runIfWaitingForDebugger'));promises.push(this._firstNonInitialNavigationCommittedPromise);@@ -899,7 +876,7 @@ class FrameSession {}_onScreencastFrame(payload: Protocol.Page.screencastFramePayload) {- this._page.throttleScreencastFrameAck(() => {+ this._page.screencast.throttleFrameAck(() => {this._client.send('Page.screencastFrameAck', { sessionId: payload.sessionId }).catch(() => {});});const buffer = Buffer.from(payload.data, 'base64');@@ -911,57 +888,6 @@ class FrameSession {});}- async _createVideoRecorder(screencastId: string, options: types.PageScreencastOptions): Promise<void> {- assert(!this._screencastId);- const ffmpegPath = registry.findExecutable('ffmpeg')!.executablePathOrDie(this._page.browserContext._browser.sdkLanguage());- this._videoRecorder = await VideoRecorder.launch(this._crPage._page, ffmpegPath, options);- this._screencastId = screencastId;- }-- async _startVideoRecording(options: types.PageScreencastOptions) {- const screencastId = this._screencastId;- assert(screencastId);- this._page.once(Page.Events.Close, () => this._stopVideoRecording().catch(() => {}));- const gotFirstFrame = new Promise(f => this._client.once('Page.screencastFrame', f));- await this._startScreencast(this._videoRecorder, {- format: 'jpeg',- quality: 90,- maxWidth: options.width,- maxHeight: options.height,- });- // Wait for the first frame before reporting video to the client.- gotFirstFrame.then(() => {- this._crPage._browserContext._browser._videoStarted(this._crPage._browserContext, screencastId, options.outputFile, this._crPage._page.waitForInitializedOrError());- });- }-- async _stopVideoRecording(): Promise<void> {- if (!this._screencastId)- return;- const screencastId = this._screencastId;- this._screencastId = null;- const recorder = this._videoRecorder!;- this._videoRecorder = null;- await this._stopScreencast(recorder);- await recorder.stop().catch(() => {});- // Keep the video artifact in the map until encoding is fully finished, if the context- // starts closing before the video is fully written to disk it will wait for it.- const video = this._crPage._browserContext._browser._takeVideo(screencastId);- video?.reportFinished();- }-- async _startScreencast(client: any, options: Protocol.Page.startScreencastParameters = {}) {- this._screencastClients.add(client);- if (this._screencastClients.size === 1)- await this._client.send('Page.startScreencast', options);- }-- async _stopScreencast(client: any) {- this._screencastClients.delete(client);- if (!this._screencastClients.size)- await this._client._sendMayFail('Page.stopScreencast');- }-async _updateGeolocation(initial: boolean): Promise<void> {const geolocation = this._crPage._browserContext._options.geolocation;if (!initial || geolocation)diff --git a/packages/playwright-core/src/server/firefox/ffPage.ts b/packages/playwright-core/src/server/firefox/ffPage.tsindex c06611be98..a1a2e52ab2 100644--- a/packages/playwright-core/src/server/firefox/ffPage.ts+++ b/packages/playwright-core/src/server/firefox/ffPage.ts@@ -478,26 +478,27 @@ export class FFPage implements PageDelegate {});}- async setScreencastOptions(options: { width: number, height: number, quality: number } | null): Promise<void> {- if (options) {- const { screencastId } = await this._session.send('Page.startScreencast', options);- this._screencastId = screencastId;- } else {- await this._session.send('Page.stopScreencast');- }+ async startScreencast(options: { width: number, height: number, quality: number }): Promise<void> {+ const { screencastId } = await this._session.send('Page.startScreencast', options);+ this._screencastId = screencastId;+ }++ async stopScreencast(): Promise<void> {+ await this._session.send('Page.stopScreencast');}private _onScreencastFrame(event: Protocol.Page.screencastFramePayload) {if (!this._screencastId)return;const screencastId = this._screencastId;- this._page.throttleScreencastFrameAck(() => {+ this._page.screencast.throttleFrameAck(() => {this._session.send('Page.screencastFrameAck', { screencastId }).catch(e => debugLogger.log('error', e));});const buffer = Buffer.from(event.data, 'base64');this._page.emit(Page.Events.ScreencastFrame, {buffer,+ frameSwapWallTime: event.timestamp * 1000, // timestamp is in seconds, we need to convert to milliseconds.width: event.deviceWidth,height: event.deviceHeight,});diff --git a/packages/playwright-core/src/server/page.ts b/packages/playwright-core/src/server/page.tsindex 38b73a1f82..d89b9a6ebe 100644--- a/packages/playwright-core/src/server/page.ts+++ b/packages/playwright-core/src/server/page.ts@@ -34,6 +34,7 @@ import { ManualPromise } from '../utils/isomorphic/manualPromise';import { parseEvaluationResultValue } from '../utils/isomorphic/utilityScriptSerializers';import { compressCallLog } from './callLog';import * as rawBindingsControllerSource from '../generated/bindingsControllerSource';+import { Screencast } from './screencast';import type { Artifact } from './artifact';import type * as dom from './dom';@@ -78,7 +79,8 @@ export interface PageDelegate {getBoundingBox(handle: dom.ElementHandle): Promise<types.Rect | null>;getFrameElement(frame: frames.Frame): Promise<dom.ElementHandle>;scrollRectIntoViewIfNeeded(handle: dom.ElementHandle, rect?: types.Rect): Promise<'error:notvisible' | 'error:notconnected' | 'done'>;- setScreencastOptions(options: { width: number, height: number, quality: number } | null): Promise<void>;+ startScreencast(options: { width: number, height: number, quality: number }): Promise<void>;+ stopScreencast(): Promise<void>;pdf?: (options: channels.PagePdfParams) => Promise<Buffer>;coverage?: () => any;@@ -165,9 +167,7 @@ export class Page extends SdkObject {private _locatorHandlerRunningCounter = 0;private _networkRequests: network.Request[] = [];- // Aiming at 25 fps by default - each frame is 40ms, but we give some slack with 35ms.- // When throttling for tracing, 200ms between frames, except for 10 frames around the action.- private _frameThrottler = new FrameThrottler(10, 35, 200);+ readonly screencast: Screencast;closeReason: string | undefined;constructor(delegate: PageDelegate, browserContext: BrowserContext) {@@ -180,6 +180,7 @@ export class Page extends SdkObject {this.touchscreen = new input.Touchscreen(delegate.rawTouchscreen, this);this.screenshotter = new Screenshotter(this);this.frameManager = new frames.FrameManager(this);+ this.screencast = new Screencast(this);if (delegate.pdf)this.pdf = delegate.pdf.bind(delegate);this.coverage = delegate.coverage ? delegate.coverage() : null;@@ -256,7 +257,7 @@ export class Page extends SdkObject {_didClose() {this.frameManager.dispose();- this._frameThrottler.dispose();+ this.screencast.stopFrameThrottler();assert(this._closedState !== 'closed', 'Page closed twice');this._closedState = 'closed';this.emit(Page.Events.Close);@@ -267,7 +268,7 @@ export class Page extends SdkObject {_didCrash() {this.frameManager.dispose();- this._frameThrottler.dispose();+ this.screencast.stopFrameThrottler();this.emit(Page.Events.Crash);this._crashed = true;this.instrumentation.onPageClose(this);@@ -827,20 +828,6 @@ export class Page extends SdkObject {return this._pageBindings.get(name) || this.browserContext._pageBindings.get(name);}- setScreencastOptions(options: { width: number, height: number, quality: number } | null) {- this.delegate.setScreencastOptions(options).catch(e => debugLogger.log('error', e));- this._frameThrottler.setThrottlingEnabled(!!options);- }-- throttleScreencastFrameAck(ack: () => void) {- // Don't ack immediately, tracing has smart throttling logic that is implemented here.- this._frameThrottler.ack(ack);- }-- temporarilyDisableTracingScreencastThrottling() {- this._frameThrottler.recharge();- }-async safeNonStallingEvaluateInAllFrames(expression: string, world: types.World, options: { throwOnJSErrors?: boolean } = {}) {await Promise.all(this.frames().map(async frame => {try {@@ -972,71 +959,6 @@ export class InitScript {}}-class FrameThrottler {- private _acks: (() => void)[] = [];- private _defaultInterval: number;- private _throttlingInterval: number;- private _nonThrottledFrames: number;- private _budget: number;- private _throttlingEnabled = false;- private _timeoutId: NodeJS.Timeout | undefined;-- constructor(nonThrottledFrames: number, defaultInterval: number, throttlingInterval: number) {- this._nonThrottledFrames = nonThrottledFrames;- this._budget = nonThrottledFrames;- this._defaultInterval = defaultInterval;- this._throttlingInterval = throttlingInterval;- this._tick();- }-- dispose() {- if (this._timeoutId) {- clearTimeout(this._timeoutId);- this._timeoutId = undefined;- }- }-- setThrottlingEnabled(enabled: boolean) {- this._throttlingEnabled = enabled;- }-- recharge() {- // Send all acks, reset budget.- for (const ack of this._acks)- ack();- this._acks = [];- this._budget = this._nonThrottledFrames;- if (this._timeoutId) {- clearTimeout(this._timeoutId);- this._tick();- }- }-- ack(ack: () => void) {- if (!this._timeoutId) {- // Already disposed.- ack();- return;- }- this._acks.push(ack);- }-- private _tick() {- const ack = this._acks.shift();- if (ack) {- --this._budget;- ack();- }-- if (this._throttlingEnabled && this._budget <= 0) {- // Non-throttled frame budget is exceeded. Next ack will be throttled.- this._timeoutId = setTimeout(() => this._tick(), this._throttlingInterval);- } else {- // Either not throttling, or still under budget. Next ack will be after the default timeout.- this._timeoutId = setTimeout(() => this._tick(), this._defaultInterval);- }- }-}async function snapshotFrameForAI(progress: Progress, frame: frames.Frame, options: { track?: string }): Promise<{ full: string[], incremental?: string[] }> {// Only await the topmost navigations, inner frames will be empty when racing.diff --git a/packages/playwright-core/src/server/registry/index.ts b/packages/playwright-core/src/server/registry/index.tsindex cb34da3624..1c4777a19b 100644--- a/packages/playwright-core/src/server/registry/index.ts+++ b/packages/playwright-core/src/server/registry/index.ts@@ -1416,7 +1416,7 @@ export class Registry {faultyArguments.push(arg);elseexecutables.push(executable);- if (executable?.browserName === 'chromium')+ if (executable?.browserName)executables.push(this.findExecutable('ffmpeg')!);};diff --git a/packages/playwright-core/src/server/screencast.ts b/packages/playwright-core/src/server/screencast.tsnew file mode 100644index 0000000000..5ec7d98288--- /dev/null+++ b/packages/playwright-core/src/server/screencast.ts@@ -0,0 +1,207 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * 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 path from 'path';+import { assert, createGuid, mkdirIfNeeded } from '../utils';+import { debugLogger } from '../utils';+import { VideoRecorder } from './videoRecorder';+import { Page } from './page';+import { registry } from './registry';++import type * as types from './types';++export class Screencast {+ private _page: Page;+ private _videoRecorder: VideoRecorder | null = null;+ private _screencastId: string | null = null;+ private _screencastClients = new Set<unknown>();++ // Aiming at 25 fps by default - each frame is 40ms, but we give some slack with 35ms.+ // When throttling for tracing, 200ms between frames, except for 10 frames around the action.+ private _frameThrottler = new FrameThrottler(10, 35, 200);++ constructor(page: Page) {+ this._page = page;+ }++ stopFrameThrottler() {+ this._frameThrottler.dispose();+ }++ setOptions(options: { width: number, height: number, quality: number } | null) {+ this._setOptions(options).catch(e => debugLogger.log('error', e));+ this._frameThrottler.setThrottlingEnabled(!!options);+ }++ throttleFrameAck(ack: () => void) {+ // Don't ack immediately, tracing has smart throttling logic that is implemented here.+ this._frameThrottler.ack(ack);+ }++ temporarilyDisableThrottling() {+ this._frameThrottler.recharge();+ }++ async initializeVideoRecorder(): Promise<types.PageScreencastOptions | undefined> {+ const recordVideo = this._page.browserContext._options.recordVideo;+ if (!recordVideo)+ return undefined;+ const screencastId = createGuid();+ const outputFile = path.join(recordVideo.dir, screencastId + '.webm');+ const screencastOptions = {+ // validateBrowserContextOptions ensures correct video size.+ ...recordVideo.size!,+ outputFile,+ };+ await mkdirIfNeeded(path.join(recordVideo.dir, 'dummy'));+ // Note: it is important to start video recorder before sending Screencast.startScreencast,+ // and it is equally important to send Screencast.startScreencast before sending Target.resume.+ await this._createVideoRecorder(screencastId, screencastOptions);+ this._page.waitForInitializedOrError().then(p => {+ if (p instanceof Error)+ this.stopVideoRecording().catch(() => {});+ });+ return screencastOptions;+ }++ async startVideoRecording(options: types.PageScreencastOptions) {+ const screencastId = this._screencastId;+ assert(screencastId);+ this._page.once(Page.Events.Close, () => this.stopVideoRecording().catch(() => {}));+ const gotFirstFrame = new Promise(f => this._page.once(Page.Events.ScreencastFrame, f));+ await this._startScreencast(this._videoRecorder, {+ quality: 90,+ width: options.width,+ height: options.height,+ });+ // Wait for the first frame before reporting video to the client.+ gotFirstFrame.then(() => {+ this._page.browserContext._browser._videoStarted(this._page.browserContext, screencastId, options.outputFile, this._page.waitForInitializedOrError());+ });+ }++ async stopVideoRecording(): Promise<void> {+ if (!this._screencastId)+ return;+ const screencastId = this._screencastId;+ this._screencastId = null;+ const recorder = this._videoRecorder!;+ this._videoRecorder = null;+ await this._stopScreencast(recorder);+ await recorder.stop().catch(() => {});+ // Keep the video artifact in the map until encoding is fully finished, if the context+ // starts closing before the video is fully written to disk it will wait for it.+ const video = this._page.browserContext._browser._takeVideo(screencastId);+ video?.reportFinished();+ }++ private async _setOptions(options: { width: number, height: number, quality: number } | null): Promise<void> {+ if (options)+ await this._startScreencast(this, options);+ else+ await this._stopScreencast(this);+ }++ private async _startScreencast(client: unknown, options: { width: number, height: number, quality: number }) {+ this._screencastClients.add(client);+ if (this._screencastClients.size === 1) {+ await this._page.delegate.startScreencast({+ width: options.width,+ height: options.height,+ quality: options.quality,+ });+ }+ }++ private async _stopScreencast(client: unknown) {+ this._screencastClients.delete(client);+ if (!this._screencastClients.size)+ await this._page.delegate.stopScreencast();+ }++ private async _createVideoRecorder(screencastId: string, options: types.PageScreencastOptions): Promise<void> {+ assert(!this._screencastId);+ const ffmpegPath = registry.findExecutable('ffmpeg')!.executablePathOrDie(this._page.browserContext._browser.sdkLanguage());+ this._videoRecorder = await VideoRecorder.launch(this._page, ffmpegPath, options);+ this._screencastId = screencastId;+ }+}++class FrameThrottler {+ private _acks: (() => void)[] = [];+ private _defaultInterval: number;+ private _throttlingInterval: number;+ private _nonThrottledFrames: number;+ private _budget: number;+ private _throttlingEnabled = false;+ private _timeoutId: NodeJS.Timeout | undefined;++ constructor(nonThrottledFrames: number, defaultInterval: number, throttlingInterval: number) {+ this._nonThrottledFrames = nonThrottledFrames;+ this._budget = nonThrottledFrames;+ this._defaultInterval = defaultInterval;+ this._throttlingInterval = throttlingInterval;+ this._tick();+ }++ dispose() {+ if (this._timeoutId) {+ clearTimeout(this._timeoutId);+ this._timeoutId = undefined;+ }+ }++ setThrottlingEnabled(enabled: boolean) {+ this._throttlingEnabled = enabled;+ }++ recharge() {+ // Send all acks, reset budget.+ for (const ack of this._acks)+ ack();+ this._acks = [];+ this._budget = this._nonThrottledFrames;+ if (this._timeoutId) {+ clearTimeout(this._timeoutId);+ this._tick();+ }+ }++ ack(ack: () => void) {+ if (!this._timeoutId) {+ // Already disposed.+ ack();+ return;+ }+ this._acks.push(ack);+ }++ private _tick() {+ const ack = this._acks.shift();+ if (ack) {+ --this._budget;+ ack();+ }++ if (this._throttlingEnabled && this._budget <= 0) {+ // Non-throttled frame budget is exceeded. Next ack will be throttled.+ this._timeoutId = setTimeout(() => this._tick(), this._throttlingInterval);+ } else {+ // Either not throttling, or still under budget. Next ack will be after the default timeout.+ this._timeoutId = setTimeout(() => this._tick(), this._defaultInterval);+ }+ }+}diff --git a/packages/playwright-core/src/server/trace/recorder/tracing.ts b/packages/playwright-core/src/server/trace/recorder/tracing.tsindex d181494c44..387d8b9305 100644--- a/packages/playwright-core/src/server/trace/recorder/tracing.ts+++ b/packages/playwright-core/src/server/trace/recorder/tracing.ts@@ -281,7 +281,7 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snapsif (!(this._context instanceof BrowserContext))return;for (const page of this._context.pages())- page.setScreencastOptions(null);+ page.screencast.setOptions(null);}private _allocateNewTraceFile(state: RecordingState) {@@ -440,7 +440,7 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snapsconst event = createBeforeActionTraceEvent(metadata, this._currentGroupId());if (!event)return Promise.resolve();- sdkObject.attribution.page?.temporarilyDisableTracingScreencastThrottling();+ sdkObject.attribution.page?.screencast.temporarilyDisableThrottling();event.beforeSnapshot = `before@${metadata.id}`;this._state?.callIds.add(metadata.id);this._appendTraceEvent(event);@@ -454,7 +454,7 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snapsconst event = createInputActionTraceEvent(metadata);if (!event)return Promise.resolve();- sdkObject.attribution.page?.temporarilyDisableTracingScreencastThrottling();+ sdkObject.attribution.page?.screencast.temporarilyDisableThrottling();event.inputSnapshot = `input@${metadata.id}`;this._appendTraceEvent(event);return this._captureSnapshot(event.inputSnapshot, sdkObject, metadata);@@ -479,7 +479,7 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snapsconst event = createAfterActionTraceEvent(metadata);if (!event)return;- sdkObject.attribution.page?.temporarilyDisableTracingScreencastThrottling();+ sdkObject.attribution.page?.screencast.temporarilyDisableThrottling();event.afterSnapshot = `after@${metadata.id}`;this._appendTraceEvent(event);return this._captureSnapshot(event.afterSnapshot, sdkObject, metadata);@@ -590,7 +590,7 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps}private _startScreencastInPage(page: Page) {- page.setScreencastOptions(kScreencastOptions);+ page.screencast.setOptions(kScreencastOptions);const prefix = page.guid;this._screencastListeners.push(eventsHelper.addEventListener(page, Page.Events.ScreencastFrame, params => {diff --git a/packages/playwright-core/src/server/chromium/videoRecorder.ts b/packages/playwright-core/src/server/videoRecorder.tssimilarity index 97%rename from packages/playwright-core/src/server/chromium/videoRecorder.tsrename to packages/playwright-core/src/server/videoRecorder.tsindex 5661b38ef6..fa2d7dc424 100644--- a/packages/playwright-core/src/server/chromium/videoRecorder.ts+++ b/packages/playwright-core/src/server/videoRecorder.ts@@ -14,11 +14,11 @@* limitations under the License.*/-import { assert, debugLogger, monotonicTime } from '../../utils';-import { Page } from '../page';-import { launchProcess } from '../utils/processLauncher';+import { assert, debugLogger, monotonicTime } from '../utils';+import { Page } from './page';+import { launchProcess } from './utils/processLauncher';-import type * as types from '../types';+import type * as types from './types';import type { ChildProcess } from 'child_process';const fps = 25;diff --git a/packages/playwright-core/src/server/webkit/wkBrowser.ts b/packages/playwright-core/src/server/webkit/wkBrowser.tsindex 86b932d053..c77c40abd9 100644--- a/packages/playwright-core/src/server/webkit/wkBrowser.ts+++ b/packages/playwright-core/src/server/webkit/wkBrowser.ts@@ -374,7 +374,7 @@ export class WKBrowserContext extends BrowserContext {async doClose(reason: string | undefined) {if (!this._browserContextId) {- await Promise.all(this._wkPages().map(wkPage => wkPage._stopVideo()));+ await Promise.all(this._wkPages().map(wkPage => wkPage._page.screencast.stopVideoRecording()));// Closing persistent context should close the browser.await this._browser.close({ reason });} else {diff --git a/packages/playwright-core/src/server/webkit/wkPage.ts b/packages/playwright-core/src/server/webkit/wkPage.tsindex cd96f8d54c..63814882b1 100644--- a/packages/playwright-core/src/server/webkit/wkPage.ts+++ b/packages/playwright-core/src/server/webkit/wkPage.ts@@ -15,11 +15,8 @@* limitations under the License.*/-import path from 'path';-import { assert } from '../../utils';import { headersArrayToObject } from '../../utils/isomorphic/headers';-import { createGuid } from '../utils/crypto';import { eventsHelper } from '../utils/eventsHelper';import { hostPlatform } from '../utils/hostPlatform';import { splitErrorMessage } from '../../utils/isomorphic/stackTrace';@@ -75,7 +72,6 @@ export class WKPage implements PageDelegate {// Holds window features for the next popup being opened via window.open,// until the popup page proxy arrives.private _nextWindowOpenPopupFeatures?: string[];- private _recordingVideoFile: string | null = null;private _screencastGeneration: number = 0;constructor(browserContext: WKBrowserContext, pageProxySession: WKSession, opener: WKPage | null) {@@ -128,16 +124,7 @@ export class WKPage implements PageDelegate {for (const [key, value] of this._browserContext._permissions)promises.push(this._grantPermissions(key, value));}- if (this._browserContext._options.recordVideo) {- const outputFile = path.join(this._browserContext._options.recordVideo.dir, createGuid() + '.webm');- promises.push(this._browserContext._ensureVideosPath().then(() => {- return this._startVideo({- // validateBrowserContextOptions ensures correct video size.- ...this._browserContext._options.recordVideo!.size!,- outputFile,- });- }));- }+ promises.push(this._initializeVideoRecording());await Promise.all(promises);}@@ -817,7 +804,6 @@ export class WKPage implements PageDelegate {}async closePage(runBeforeUnload: boolean): Promise<void> {- await this._stopVideo();await this._pageProxySession.sendMayFail('Target.close', {targetId: this._session.sessionId,runBeforeUnload@@ -834,23 +820,10 @@ export class WKPage implements PageDelegate {return 0;}- private async _startVideo(options: types.PageScreencastOptions): Promise<void> {- assert(!this._recordingVideoFile);- const { screencastId } = await this._pageProxySession.send('Screencast.startVideo', {- file: this._browserContext._browser.options.channel === 'webkit-wsl' ? await translatePathToWSL(options.outputFile) : options.outputFile,- width: options.width,- height: options.height,- toolbarHeight: this._toolbarHeight()- });- this._recordingVideoFile = options.outputFile;- this._browserContext._browser._videoStarted(this._browserContext, screencastId, options.outputFile, this._page.waitForInitializedOrError());- }-- async _stopVideo(): Promise<void> {- if (!this._recordingVideoFile)- return;- await this._pageProxySession.sendMayFail('Screencast.stopVideo');- this._recordingVideoFile = null;+ private async _initializeVideoRecording() {+ const screencastOptions = await this._page.screencast.initializeVideoRecorder();+ if (screencastOptions)+ await this._page.screencast.startVideoRecording(screencastOptions);}private validateScreenshotDimension(side: number, omitDeviceScaleFactor: boolean) {@@ -927,24 +900,29 @@ export class WKPage implements PageDelegate {});}- async setScreencastOptions(options: { width: number, height: number, quality: number } | null): Promise<void> {- if (options) {- const so = { ...options, toolbarHeight: this._toolbarHeight() };- const { generation } = await this._pageProxySession.send('Screencast.startScreencast', so);- this._screencastGeneration = generation;- } else {- await this._pageProxySession.send('Screencast.stopScreencast');- }+ async startScreencast(options: { width: number, height: number, quality: number }): Promise<void> {+ const { generation } = await this._pageProxySession.send('Screencast.startScreencast', {+ quality: options.quality,+ width: options.width,+ height: options.height,+ toolbarHeight: this._toolbarHeight(),+ });+ this._screencastGeneration = generation;+ }++ async stopScreencast(): Promise<void> {+ await this._pageProxySession.sendMayFail('Screencast.stopScreencast');}private _onScreencastFrame(event: Protocol.Screencast.screencastFramePayload) {const generation = this._screencastGeneration;- this._page.throttleScreencastFrameAck(() => {+ this._page.screencast.throttleFrameAck(() => {this._pageProxySession.send('Screencast.screencastFrameAck', { generation }).catch(e => debugLogger.log('error', e));});const buffer = Buffer.from(event.data, 'base64');this._page.emit(Page.Events.ScreencastFrame, {buffer,+ frameSwapWallTime: event.timestamp * 1000, // timestamp is in seconds, we need to convert to milliseconds.width: event.deviceWidth,height: event.deviceHeight,});