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: make cli video-start/video-stop record for all pages (#39130)
Dmitry Gozmane13deeb58215
diff --git a/packages/playwright/src/mcp/browser/context.ts b/packages/playwright/src/mcp/browser/context.tsindex 89361e1adc..e0dbd03e89 100644--- a/packages/playwright/src/mcp/browser/context.ts+++ b/packages/playwright/src/mcp/browser/context.ts@@ -58,6 +58,8 @@ export type FilenameTemplate = {date?: Date;};+type VideoParams = NonNullable<Parameters<playwright.Video['start']>[0]>;+export class Context {readonly config: FullConfig;readonly sessionLog: SessionLog | undefined;@@ -68,6 +70,10 @@ export class Context {private _currentTab: Tab | undefined;private _clientInfo: ClientInfo;private _routes: RouteEntry[] = [];+ private _video: {+ allVideos: Set<playwright.Video>;+ listener: (page: playwright.Page) => void;+ } | undefined;private static _allContexts: Set<Context> = new Set();private _closeBrowserContextPromise: Promise<void> | undefined;@@ -146,6 +152,33 @@ export class Context {return await outputFile(this.config, this._clientInfo, baseName, options);}+ async startVideoRecording(params: VideoParams) {+ if (this._video)+ throw new Error('Video recording has already been started.');+ const listener = (page: playwright.Page) => {+ this._video?.allVideos.add(page.video());+ page.video().start(params).catch(() => {});+ };+ this._video = { allVideos: new Set(), listener };+ const browserContext = await this.ensureBrowserContext();+ browserContext.pages().forEach(listener);+ browserContext.on('page', listener);+ }++ async stopVideoRecording() {+ if (!this._video)+ throw new Error('Video recording has not been started.');+ const video = this._video;+ if (this._browserContextPromise) {+ const { browserContext } = await this._browserContextPromise;+ browserContext.off('page', video.listener);+ for (const page of browserContext.pages())+ await page.video().stop().catch(() => {});+ }+ this._video = undefined;+ return video.allVideos;+ }+private _onPageCreated(page: playwright.Page) {const tab = new Tab(this, page, tab => this._onPageClosed(tab));this._tabs.push(tab);diff --git a/packages/playwright/src/mcp/browser/tools/video.ts b/packages/playwright/src/mcp/browser/tools/video.tsindex 69b4bfcadb..e08c53c4e5 100644--- a/packages/playwright/src/mcp/browser/tools/video.ts+++ b/packages/playwright/src/mcp/browser/tools/video.ts@@ -14,10 +14,11 @@* limitations under the License.*/+import path from 'path';import { z } from 'playwright-core/lib/mcpBundle';-import { defineTabTool } from './tool';+import { defineTool } from './tool';-const startVideo = defineTabTool({+const startVideo = defineTool({capability: 'devtools',schema: {@@ -33,13 +34,13 @@ const startVideo = defineTabTool({type: 'readOnly',},- handle: async (tab, params, response) => {- await tab.page.video().start({ size: params.size });+ handle: async (context, params, response) => {+ await context.startVideoRecording({ size: params.size });response.addTextResult('Video recording started.');},});-const stopVideo = defineTabTool({+const stopVideo = defineTool({capability: 'devtools',schema: {@@ -52,11 +53,23 @@ const stopVideo = defineTabTool({type: 'readOnly',},- handle: async (tab, params, response) => {- const resolvedFile = await response.resolveClientFile({ prefix: 'video', ext: 'webm', suggestedFilename: params.filename }, 'Video');- await tab.page.video().stop();- await tab.page.video().saveAs(resolvedFile.fileName);- await response.addFileResult(resolvedFile, null);+ handle: async (context, params, response) => {+ const videos = await context.stopVideoRecording();+ if (!videos.size) {+ response.addTextResult('No videos were recorded.');+ return;+ }+ for (const [index, video] of [...videos].entries()) {+ const suffix = index ? `-${index}` : '';+ let suggestedFilename = params.filename;+ if (suggestedFilename && suffix) {+ const ext = path.extname(suggestedFilename);+ suggestedFilename = path.basename(suggestedFilename, ext) + suffix + ext;+ }+ const resolvedFile = await response.resolveClientFile({ prefix: 'video' + suffix, ext: 'webm', suggestedFilename }, 'Video');+ await video.saveAs(resolvedFile.fileName);+ await response.addFileResult(resolvedFile, null);+ }},});diff --git a/tests/mcp/cli-devtools.spec.ts b/tests/mcp/cli-devtools.spec.tsindex ab9703651e..357e713275 100644--- a/tests/mcp/cli-devtools.spec.ts+++ b/tests/mcp/cli-devtools.spec.ts@@ -81,17 +81,15 @@ test('tracing-start-stop', async ({ cli, server }) => {});test('video-start-stop', async ({ cli, server }) => {- await cli('open', server.HELLO_WORLD);const { output: videoStartOutput } = await cli('video-start');expect(videoStartOutput).toContain('Video recording started.');await cli('open', server.HELLO_WORLD);- await cli('eval', `- async () => {- document.body.style.backgroundColor = "red";- for (let i = 0; i < 100; i++)- await new Promise(f => requestAnimationFrame(() => requestAnimationFrame(f)));- }- `);+ const { output: tabNewOutput } = await cli('tab-new');+ expect(tabNewOutput).toContain('1: (current) [](about:blank)');+ await cli('open', server.EMPTY_PAGE);+ await cli('tab-select', '0');+ const { output: tabCloseOutput } = await cli('tab-close');+ expect(tabCloseOutput).toContain(`0: (current) [](${server.EMPTY_PAGE})`);const { output: videoStopOutput } = await cli('video-stop', '--filename=video.webm');- expect(videoStopOutput).toContain(`### Result\n- [Video](video.webm)`);+ expect(videoStopOutput).toContain(`### Result\n- [Video](video.webm)\n- [Video](video-1.webm)`);});