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
feat(cli): add --all option to console and requests (#43526)
Pavel Feldman69c4b33ddc7d
diff --git a/packages/playwright-core/src/tools/backend/console.ts b/packages/playwright-core/src/tools/backend/console.tsindex 59a1cf274d..f24b8d9cbc 100644--- a/packages/playwright-core/src/tools/backend/console.ts+++ b/packages/playwright-core/src/tools/backend/console.ts@@ -31,7 +31,7 @@ const console = defineTabTool({type: 'readOnly',},handle: async (tab, params, response) => {- const count = await tab.consoleMessageCount();+ const count = await tab.consoleMessageCount(params.all);const header = [`Total messages: ${count.total} (Errors: ${count.errors}, Warnings: ${count.warnings})`];const messages = await tab.consoleMessages(params.level, params.all);if (messages.length !== count.total)diff --git a/packages/playwright-core/src/tools/backend/network.ts b/packages/playwright-core/src/tools/backend/network.tsindex a5bf1b4454..12144b5408 100644--- a/packages/playwright-core/src/tools/backend/network.ts+++ b/packages/playwright-core/src/tools/backend/network.ts@@ -37,18 +37,18 @@ const requests = defineTabTool({inputSchema: z.object({static: z.boolean().default(false).describe('Whether to include successful static resources like images, fonts, scripts, etc. Defaults to false.'),filter: z.string().optional().refine(v => !v || isRegexString(v), { message: 'Invalid regular expression' }).describe('Only return requests whose URL matches this regexp (e.g. "/api/.*user").'),+ all: z.boolean().optional().describe('Return all network requests since the beginning of the session, not just since the last navigation. Defaults to false.'),filename: z.string().optional().describe('File name to save the network requests to. Relative file names are resolved against the workspace root. If not provided, requests are returned as text.'),}),type: 'readOnly',},handle: async (tab, params, response) => {- const allRequests = await tab.requests();+ const entries = await tab.requests(params.all);const filter = params.filter ? new RegExp(params.filter) : undefined;const lines: string[] = [];let hiddenStaticCount = 0;- for (let i = 0; i < allRequests.length; i++) {- const request = allRequests[i];+ for (const { index, request } of entries) {if (!params.static && !isFetch(request) && isSuccessfulResponse(request)) {hiddenStaticCount++;continue;@@ -58,7 +58,7 @@ const requests = defineTabTool({if (!filter.test(request.url()))continue;}- lines.push(`${i + 1}. ${renderRequestLine(request)}`);+ lines.push(`${index}. ${renderRequestLine(request)}`);}if (hiddenStaticCount > 0) {const optionName = tab.context.config.skillMode ? '--static' : '"static"';@@ -87,8 +87,7 @@ const request = defineTabTool({},handle: async (tab, params, response) => {- const allRequests = await tab.requests();- const request = allRequests[params.index - 1];+ const request = await tab.request(params.index);if (!request) {response.addError(`Request #${params.index} not found. Use browser_network_requests to see available indexes.`);return;diff --git a/packages/playwright-core/src/tools/backend/tab.ts b/packages/playwright-core/src/tools/backend/tab.tsindex 14eda321de..27ba69e620 100644--- a/packages/playwright-core/src/tools/backend/tab.ts+++ b/packages/playwright-core/src/tools/backend/tab.ts@@ -74,6 +74,14 @@ type RequestLogEntry = {type EventEntry = ConsoleLogEntry | DownloadStartLogEntry | DownloadFinishLogEntry | RequestLogEntry;+type RequestEntry = {+ index: number;+ request: playwright.Request;+};++// Avoid unbounded memory growth.+const kMaxEarlierNavigationRequests = 200;+export type TabHeader = {title: string;@@ -100,7 +108,9 @@ export class Tab extends EventEmitter<TabEventsInterface> {readonly isolatedContext: string | undefined;private _lastHeader: TabHeader = { title: 'about:blank', url: 'about:blank', current: false, crashed: false, console: { total: 0, warnings: 0, errors: 0 } };private _downloads: Download[] = [];- private _requests: playwright.Request[] = [];+ private _requests: RequestEntry[] = [];+ private _lastRequestIndex = 0;+ private _navigationRequestIndex = 0;private _mainDocumentStatus: { status: number, statusText: string } | undefined;private _onPageClose: (tab: Tab) => void;crashed = false;@@ -183,7 +193,7 @@ export class Tab extends EventEmitter<TabEventsInterface> {this._handleConsoleMessage(message);const requests = await this.page.requests().catch(() => []);for (const request of requests.filter(r => r.existingResponse() || r.failure()))- this._requests.push(request);+ this._addRequest(request);for (const initPage of this.context.config.browser?.initPage || []) {try {const { default: func } = require(initPage);@@ -241,7 +251,7 @@ export class Tab extends EventEmitter<TabEventsInterface> {private _clearCollectedArtifacts() {this._webmcpTools = undefined;this._downloads.length = 0;- this._requests.length = 0;+ this._markRequestsNavigation();this._mainDocumentStatus = undefined;this._recentEventEntries.length = 0;this._resetLogs();@@ -253,8 +263,19 @@ export class Tab extends EventEmitter<TabEventsInterface> {this._consoleLog = new LogFile(this.context, wallTime, 'console', 'Console');}+ private _addRequest(request: playwright.Request) {+ this._requests.push({ index: ++this._lastRequestIndex, request });+ }++ private _markRequestsNavigation() {+ const earlierRequestCount = this._requests.length;+ if (earlierRequestCount > kMaxEarlierNavigationRequests)+ this._requests.splice(0, earlierRequestCount - kMaxEarlierNavigationRequests);+ this._navigationRequestIndex = this._lastRequestIndex;+ }+private _handleRequest(request: playwright.Request) {- this._requests.push(request);+ this._addRequest(request);// TODO: request start time is not available for fetch() before the// response is received, so we use Date.now() as a fallback.const wallTime = request.timing().startTime || Date.now();@@ -399,10 +420,11 @@ export class Tab extends EventEmitter<TabEventsInterface> {}- async consoleMessageCount(): Promise<{ total: number, errors: number, warnings: number }> {+ async consoleMessageCount(all?: boolean): Promise<{ total: number, errors: number, warnings: number }> {await this._initializedPromise;- const messages = await this.page.consoleMessages({ filter: 'since-navigation' });- const pageErrors = await this.page.pageErrors({ filter: 'since-navigation' });+ const filter = all ? 'all' : 'since-navigation';+ const messages = await this.page.consoleMessages({ filter });+ const pageErrors = await this.page.pageErrors({ filter });let errors = pageErrors.length;let warnings = 0;for (const message of messages) {@@ -439,9 +461,16 @@ export class Tab extends EventEmitter<TabEventsInterface> {]);}- async requests(): Promise<playwright.Request[]> {+ async requests(all?: boolean): Promise<RequestEntry[]> {+ await this._initializedPromise;+ if (all)+ return this._requests;+ return this._requests.filter(entry => entry.index > this._navigationRequestIndex);+ }++ async request(index: number): Promise<playwright.Request | undefined> {await this._initializedPromise;- return this._requests;+ return this._requests.find(entry => entry.index === index)?.request;}async clearRequests() {diff --git a/packages/playwright-core/src/tools/cli-daemon/commands.ts b/packages/playwright-core/src/tools/cli-daemon/commands.tsindex b0dfb347e2..a7dc13c96d 100644--- a/packages/playwright-core/src/tools/cli-daemon/commands.ts+++ b/packages/playwright-core/src/tools/cli-daemon/commands.ts@@ -947,10 +947,11 @@ const consoleList = declareCommand({['min-level']: z.string().optional().describe('Level of the console messages to return. Each level includes the messages of more severe levels. Defaults to "info".'),}),options: z.object({+ all: z.boolean().optional().describe('Whether to return all console messages since the beginning of the session, not just since the last navigation'),clear: z.boolean().optional().describe('Whether to clear the console list'),}),toolName: ({ clear }) => clear ? 'browser_console_clear' : 'browser_console_messages',- toolParams: ({ ['min-level']: level, clear }) => clear ? ({}) : ({ level }),+ toolParams: ({ ['min-level']: level, all, clear }) => clear ? ({}) : ({ level, all }),});const networkRequests = declareCommand({@@ -961,10 +962,11 @@ const networkRequests = declareCommand({options: z.object({static: z.boolean().optional().describe('Whether to include successful static resources like images, fonts, scripts, etc. Defaults to false.'),filter: z.string().optional().describe('Only return requests whose URL matches this regexp (e.g. "/api/.*user").'),+ all: z.boolean().optional().describe('Whether to return all network requests since the beginning of the session, not just since the last navigation'),clear: z.boolean().optional().describe('Whether to clear the network list'),}),toolName: ({ clear }) => clear ? 'browser_network_clear' : 'browser_network_requests',- toolParams: ({ static: s, filter, clear }) => clear ? ({}) : ({ static: s, filter }),+ toolParams: ({ static: s, filter, all, clear }) => clear ? ({}) : ({ static: s, filter, all }),});const filenameOption = z.string().optional().describe('Filename to save the result to. If not provided, output is returned as text.');diff --git a/tests/mcp/cli-devtools.spec.ts b/tests/mcp/cli-devtools.spec.tsindex 07605bea8c..24a9f62bc5 100644--- a/tests/mcp/cli-devtools.spec.ts+++ b/tests/mcp/cli-devtools.spec.ts@@ -47,6 +47,16 @@ test('console --clear', async ({ cli, server }) => {expect(output).not.toContain('log-level');});+test('console --all', async ({ cli, server }) => {+ await cli('open', server.PREFIX);+ await cli('eval', 'console.log("before-navigation")');+ await cli('goto', server.PREFIX);+ const { output: defaultOutput } = await cli('console');+ expect(defaultOutput).not.toContain('before-navigation');+ const { output } = await cli('console', '--all');+ expect(output).toContain('before-navigation');+});+test('requests', async ({ cli, server }) => {await cli('open', server.PREFIX);await cli('eval', '() => fetch("/hello-world")');@@ -83,6 +93,16 @@ test('requests --clear', async ({ cli, server }) => {expect(output).not.toContain(`${server.PREFIX}/hello-world`);});+test('requests --all', async ({ cli, server }) => {+ await cli('open', server.PREFIX);+ await cli('eval', '() => fetch("/hello-world")');+ await cli('goto', server.PREFIX);+ const { output: defaultOutput } = await cli('requests');+ expect(defaultOutput).not.toContain(`${server.PREFIX}/hello-world`);+ const { output } = await cli('requests', '--all');+ expect(output).toMatch(new RegExp(String.raw`^\d+\. \[GET\] ${escapeRegExp(`${server.PREFIX}/hello-world`)} => \[200\] OK$`, 'm'));+});+test('request shows full request and response details', async ({ cli, server }) => {server.setContent('/', `<button onclick="fetch('/api', { method: 'POST', headers: { 'X-Custom-Header': 'test-value' }, body: JSON.stringify({ key: 'value' }) })">Click me</button>diff --git a/tests/mcp/console.spec.ts b/tests/mcp/console.spec.tsindex ee3e030301..109874a040 100644--- a/tests/mcp/console.spec.ts+++ b/tests/mcp/console.spec.ts@@ -428,6 +428,7 @@ test('browser_console_messages all option', async ({ client, server }) => {name: 'browser_console_messages',arguments: { all: true },}));+ expect(allResponse.result).toContain('Total messages: 2 (Errors: 0, Warnings: 0)');expect(allResponse.result).toContain('page1 message');expect(allResponse.result).toContain('page2 message');});diff --git a/tests/mcp/network.spec.ts b/tests/mcp/network.spec.tsindex e68dd9a476..251d294be0 100644--- a/tests/mcp/network.spec.ts+++ b/tests/mcp/network.spec.ts@@ -137,6 +137,35 @@ test('browser_network_requests numbers requests with stable indexes', async ({ cexpect(lines).toHaveLength(3);});+test('browser_network_requests all option', async ({ client, server }) => {+ server.setContent('/api/page1', 'page1', 'text/plain');+ server.setContent('/api/page2', 'page2', 'text/plain');++ await client.callTool({ name: 'browser_navigate', arguments: { url: server.EMPTY_PAGE } });+ await client.callTool({ name: 'browser_evaluate', arguments: { function: `async () => { await fetch('/api/page1'); }` } });+ await client.callTool({ name: 'browser_navigate', arguments: { url: server.EMPTY_PAGE } });+ await client.callTool({ name: 'browser_evaluate', arguments: { function: `async () => { await fetch('/api/page2'); }` } });++ const defaultResponse = parseResponse(await client.callTool({ name: 'browser_network_requests' }));+ expect(defaultResponse.result).not.toContain('/api/page1');++ const allResponse = parseResponse(await client.callTool({+ name: 'browser_network_requests',+ arguments: { all: true },+ }));+ const page1Index = allResponse.result.match(new RegExp(String.raw`^(\d+)\. \[GET\] ${escapeRegExp(`${server.PREFIX}/api/page1`)} => \[200\] OK$`, 'm'))?.[1];+ expect(page1Index).toBeTruthy();+ // Requests keep their numbers across navigations.+ const page2Line = allResponse.result.split('\n').find(line => line.includes('/api/page2'));+ expect(defaultResponse.result).toContain(page2Line);++ const details = parseResponse(await client.callTool({+ name: 'browser_network_request',+ arguments: { index: Number(page1Index) },+ }));+ expect(details.result).toContain(`#${page1Index} [GET] ${server.PREFIX}/api/page1`);+});+test('browser_network_request shows full request and response details', async ({ client, server }) => {server.setContent('/', `<button onclick="fetch('/api', { method: 'POST', headers: { 'X-Custom-Header': 'test-value' }, body: JSON.stringify({ key: 'value' }) })">Click me</button>