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: more output formatting (#38894)
Pavel Feldmanc3f92d56d46a
diff --git a/packages/playwright/src/mcp/browser/response.ts b/packages/playwright/src/mcp/browser/response.tsindex dcbda0ab69..a91c3b6840 100644--- a/packages/playwright/src/mcp/browser/response.ts+++ b/packages/playwright/src/mcp/browser/response.ts@@ -106,17 +106,22 @@ export class Response {async build(): Promise<{ content: (TextContent | ImageContent)[], isError?: boolean }> {const rootPath = this._context.firstRootPath();- const text: string[] = [];+ const sections: { title: string, content: string[] }[] = [];+ const addSection = (title: string): string[] => {+ const section = { title, content: [] as string[] };+ sections.push(section);+ return section.content;+ };if (this._errors.length) {+ const text = addSection('Error');text.push('### Error');text.push(this._errors.join('\n'));- text.push('');}// Resultsif (this._results.length) {- text.push('### Result');+ const text = addSection('Result');for (const result of this._results) {if (result.filename) {text.push(`- [${result.title}](${rootPath ? path.relative(rootPath, result.filename) : result.filename})`);@@ -128,49 +133,31 @@ export class Response {text.push(result.text);}}- text.push('');}// Codeif (this._context.config.codegen !== 'none' && this._code.length) {- text.push('### Ran Playwright code');+ const text = addSection('Ran Playwright code');text.push(...this._code);- text.push('');}// Render tab titles upon changes or when more than one tab.const tabSnapshot = this._context.currentTab() ? await this._context.currentTabOrDie().captureSnapshot() : undefined;const tabHeaders = await Promise.all(this._context.tabs().map(tab => tab.headerSnapshot()));if (tabHeaders.some(header => header.changed)) {- if (tabHeaders.length !== 1)- text.push('### Open tabs');- else if (this._context.config.outputMode !== 'file')- text.push('### Page');- text.push(...renderTabsMarkdown(tabHeaders));- text.push('');+ if (tabHeaders.length !== 1) {+ const text = addSection('Open tabs');+ text.push(...renderTabsMarkdown(tabHeaders));+ }++ const text = addSection('Page');+ text.push(...renderTabMarkdown(tabHeaders[0]));}// Handle modal states.if (tabSnapshot?.modalStates.length) {- text.push('### Modal state');+ const text = addSection('Modal state');text.push(...renderModalStates(tabSnapshot.modalStates));- text.push('');- }-- // Handle tab log- if (tabSnapshot?.events.filter(event => event.type !== 'request').length) {- text.push('### Events');- for (const event of tabSnapshot.events) {- if (event.type === 'console') {- if (shouldIncludeMessage(this._context.config.console.level, event.message.type))- text.push(`- ${trimMiddle(event.message.toString(), 100)}`);- } else if (event.type === 'download-start') {- text.push(`- Downloading file ${event.download.download.suggestedFilename()} ...`);- } else if (event.type === 'download-finish') {- text.push(`- Downloaded file ${event.download.download.suggestedFilename()} to "${rootPath ? path.relative(rootPath, event.download.outputFile) : event.download.outputFile}"`);- }- }- text.push('');}// Handle tab snapshot@@ -182,31 +169,53 @@ export class Response {fileName = await this._context.outputFile(`snapshot-${this._ordinal}.yml`, { origin: 'code', title: 'Saved snapshot' });if (fileName) {await fs.promises.writeFile(fileName, tabSnapshot.ariaSnapshot);- text.push(`- [Page snapshot](${rootPath ? path.relative(rootPath, fileName) : fileName})`);+ const text = addSection('Snapshot');+ text.push(`- File: ${rootPath ? path.relative(rootPath, fileName) : fileName}`);} else {- text.push(`### Snapshot`);+ const text = addSection('Snapshot');text.push('```yaml');text.push(tabSnapshot.ariaSnapshot);text.push('```');- text.push('');}}if (tabSnapshot && this._includeSnapshot === 'incremental') {- text.push(`### Snapshot`);+ const text = addSection('Snapshot');text.push('```yaml');if (tabSnapshot.ariaSnapshotDiff !== undefined)text.push(tabSnapshot.ariaSnapshotDiff);elsetext.push(tabSnapshot.ariaSnapshot);text.push('```');- text.push('');}+ // Handle tab log+ if (tabSnapshot?.events.filter(event => event.type !== 'request').length) {+ const text = addSection('Events');+ for (const event of tabSnapshot.events) {+ if (event.type === 'console') {+ if (shouldIncludeMessage(this._context.config.console.level, event.message.type))+ text.push(`- ${trimMiddle(event.message.toString(), 100)}`);+ } else if (event.type === 'download-start') {+ text.push(`- Downloading file ${event.download.download.suggestedFilename()} ...`);+ } else if (event.type === 'download-finish') {+ text.push(`- Downloaded file ${event.download.download.suggestedFilename()} to "${rootPath ? path.relative(rootPath, event.download.outputFile) : event.download.outputFile}"`);+ }+ }+ }++ const allText = sections.flatMap(section => {+ const content: string[] = [];+ content.push(`### ${section.title}`);+ content.push(...section.content);+ content.push('');+ return content;+ }).join('\n');+const content: (TextContent | ImageContent)[] = [{type: 'text',- text: this._redactText(text.join('\n'))+ text: this._redactText(allText)},];@@ -229,19 +238,18 @@ export class Response {}}+export function renderTabMarkdown(tab: TabHeader): string[] {+ const lines = [`- Page URL: ${tab.url}`];+ if (tab.title)+ lines.push(`- Page Title: ${tab.title}`);+ return lines;+}+export function renderTabsMarkdown(tabs: TabHeader[]): string[] {if (!tabs.length)return ['No open tabs. Use the "browser_navigate" tool to navigate to a page first.'];const lines: string[] = [];-- if (tabs.length === 1) {- lines.push(`- Page URL: ${tabs[0].url}`);- if (tabs[0].title)- lines.push(`- Page Title: ${tabs[0].title}`);- return lines;- }-for (let i = 0; i < tabs.length; i++) {const tab = tabs[i];const current = tab.current ? ' (current)' : '';diff --git a/tests/mcp/screenshot.spec.ts b/tests/mcp/screenshot.spec.tsindex b83ac5b9e8..ed0598f7e0 100644--- a/tests/mcp/screenshot.spec.ts+++ b/tests/mcp/screenshot.spec.ts@@ -391,7 +391,7 @@ test('browser_take_screenshot (viewport without snapshot)', async ({ startClientaction: 'list',},})).toHaveResponse({- result: `- Page URL: about:blank`,+ result: `- 0: (current) [](about:blank)`,});// This should work without requiring a snapshot since it's a viewport screenshotdiff --git a/tests/mcp/snapshot-mode.spec.ts b/tests/mcp/snapshot-mode.spec.tsindex 327a7fd991..d379da7dfe 100644--- a/tests/mcp/snapshot-mode.spec.ts+++ b/tests/mcp/snapshot-mode.spec.ts@@ -122,7 +122,7 @@ test('should respect snapshot[filename]', async ({ startClient, server }, testInarguments: {filename: 'snapshot1.yml',},- })).toHaveTextResponse(expect.stringContaining('- [Page snapshot](output' + path.sep + 'snapshot1.yml)'));+ })).toHaveTextResponse(expect.stringContaining('- File: output' + path.sep + 'snapshot1.yml'));expect(await fs.promises.readFile(path.join(outputDir, 'snapshot1.yml'), 'utf8')).toContain(`- button "Button 1" [ref=e2]`);});diff --git a/tests/mcp/tabs.spec.ts b/tests/mcp/tabs.spec.tsindex 7ad0af658a..cf10d0dca8 100644--- a/tests/mcp/tabs.spec.ts+++ b/tests/mcp/tabs.spec.ts@@ -40,7 +40,7 @@ test('list initial tabs', async ({ client }) => {action: 'list',},})).toHaveResponse({- result: `- Page URL: about:blank`,+ result: `- 0: (current) [](about:blank)`,});});diff --git a/tests/mcp/terminal.spec.ts b/tests/mcp/terminal.spec.tsindex d5438b3dc7..6b6375db1b 100644--- a/tests/mcp/terminal.spec.ts+++ b/tests/mcp/terminal.spec.ts@@ -28,9 +28,12 @@ test('terminal mode', async ({ startClient, server }) => {expect(await client.callTool({name: 'browser_navigate',arguments: { url: server.PREFIX },- })).toHaveTextResponse(`- Page URL: ${server.PREFIX}/+ })).toHaveTextResponse(`### Page+- Page URL: ${server.PREFIX}/-- [Page snapshot](output${path.sep}snapshot-1.yml)`);+### Snapshot+- File: output${path.sep}snapshot-1.yml+`);expect(await client.callTool({name: 'browser_type',@@ -66,7 +69,9 @@ test('terminal mode', async ({ startClient, server }) => {element: 'Country select',values: ['United States'],},- })).toHaveTextResponse(`- [Page snapshot](output${path.sep}snapshot-5.yml)`);+ })).toHaveTextResponse(`### Snapshot+- File: output${path.sep}snapshot-5.yml+`);expect(await client.callTool({name: 'browser_click',@@ -74,7 +79,9 @@ test('terminal mode', async ({ startClient, server }) => {ref: 'e12',element: 'Subscribe checkbox',},- })).toHaveTextResponse(`- [Page snapshot](output${path.sep}snapshot-6.yml)`);+ })).toHaveTextResponse(`### Snapshot+- File: output${path.sep}snapshot-6.yml+`);});