playwright_console_logs
Retrieve and filter browser console logs during automation testing to identify errors, warnings, and debug information with search and limit options.
Instructions
Retrieve console logs from the browser with filtering options
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| type | No | Type of logs to retrieve (all, error, warning, log, info, debug, exception) | |
| search | No | Text to search for in logs (handles text with square brackets) | |
| limit | No | Maximum number of logs to return | |
| clear | No | Whether to clear logs after retrieval (default: false) |
Implementation Reference
- src/tools/browser/console.ts:26-83 (handler)The execute method of ConsoleLogsTool that filters stored console logs by type, search term, limit, and optionally clears them. Saves the filtered logs to a temporary file, registers it as a resource, and returns the result.async execute(args: any, _context: ToolContext): Promise<ToolResponse> { // No need to use safeExecute here as we don't need to interact with the page // We're just filtering and returning logs that are already stored let logs = [...this.consoleLogs]; // Filter by type if specified if (args.type && args.type !== "all") { logs = logs.filter((log) => log.startsWith(`[${args.type}]`)); } // Filter by search text if specified if (args.search) { logs = logs.filter((log) => log.includes(args.search)); } // Limit the number of logs if specified if (args.limit && args.limit > 0) { logs = logs.slice(-args.limit); } // Clear logs if requested if (args.clear) { this.consoleLogs = []; } // Format the response if (logs.length === 0) { return createSuccessResponse("No console logs matching the criteria"); } else { let savedLocation: string | undefined; let resourceLink: Awaited<ReturnType<typeof registerFileResource>> | undefined; try { const timestamp = new Date().toISOString().replace(/[:.]/g, "-"); const filename = `console-logs-${timestamp}.txt`; const tempPath = path.join(process.cwd(), filename); await fs.writeFile(tempPath, logs.join("\n"), "utf-8"); resourceLink = await registerFileResource({ filePath: tempPath, name: filename, mimeType: "text/plain", server: this.server, }); savedLocation = resourceLink?.uri ?? tempPath; await fs.unlink(tempPath).catch(() => {}); } catch (_error) { // If resource registration fails, just include inline logs savedLocation = undefined; } return { ...createSuccessResponse( savedLocation ? [`Retrieved ${logs.length} console log(s). Download: ${savedLocation}`, ...logs] : logs, ), ...(resourceLink ? { resourceLinks: [resourceLink] } : {}), }; } }
- src/tools.ts:238-263 (schema)Defines the tool's name, description, and input schema for parameters: type (enum), search (string), limit (number), clear (boolean).name: "playwright_console_logs", description: "Retrieve console logs from the browser with filtering options", inputSchema: { type: "object", properties: { type: { type: "string", description: "Type of logs to retrieve (all, error, warning, log, info, debug, exception)", enum: ["all", "error", "warning", "log", "info", "debug", "exception"], }, search: { type: "string", description: "Text to search for in logs (handles text with square brackets)", }, limit: { type: "number", description: "Maximum number of logs to return", }, clear: { type: "boolean", description: "Whether to clear logs after retrieval (default: false)", }, }, required: [], }, },
- src/toolHandler.ts:582-583 (registration)Switch case in handleToolCall that dispatches the tool call to the consoleLogsTool instance's execute method.case "playwright_console_logs": return await consoleLogsTool.execute(args, context);
- src/toolHandler.ts:391-391 (registration)Instantiates the ConsoleLogsTool instance during tool initialization if not already created.if (!consoleLogsTool) consoleLogsTool = new ConsoleLogsTool(server);
- src/tools/browser/console.ts:10-98 (helper)ConsoleLogsTool class definition including methods to register, get, and clear console logs.export class ConsoleLogsTool extends BrowserToolBase { private consoleLogs: string[] = []; /** * Register a console message * @param type The type of console message * @param text The text content of the message */ registerConsoleMessage(type: string, text: string): void { const logEntry = `[${type}] ${text}`; this.consoleLogs.push(logEntry); } /** * Execute the console logs tool */ async execute(args: any, _context: ToolContext): Promise<ToolResponse> { // No need to use safeExecute here as we don't need to interact with the page // We're just filtering and returning logs that are already stored let logs = [...this.consoleLogs]; // Filter by type if specified if (args.type && args.type !== "all") { logs = logs.filter((log) => log.startsWith(`[${args.type}]`)); } // Filter by search text if specified if (args.search) { logs = logs.filter((log) => log.includes(args.search)); } // Limit the number of logs if specified if (args.limit && args.limit > 0) { logs = logs.slice(-args.limit); } // Clear logs if requested if (args.clear) { this.consoleLogs = []; } // Format the response if (logs.length === 0) { return createSuccessResponse("No console logs matching the criteria"); } else { let savedLocation: string | undefined; let resourceLink: Awaited<ReturnType<typeof registerFileResource>> | undefined; try { const timestamp = new Date().toISOString().replace(/[:.]/g, "-"); const filename = `console-logs-${timestamp}.txt`; const tempPath = path.join(process.cwd(), filename); await fs.writeFile(tempPath, logs.join("\n"), "utf-8"); resourceLink = await registerFileResource({ filePath: tempPath, name: filename, mimeType: "text/plain", server: this.server, }); savedLocation = resourceLink?.uri ?? tempPath; await fs.unlink(tempPath).catch(() => {}); } catch (_error) { // If resource registration fails, just include inline logs savedLocation = undefined; } return { ...createSuccessResponse( savedLocation ? [`Retrieved ${logs.length} console log(s). Download: ${savedLocation}`, ...logs] : logs, ), ...(resourceLink ? { resourceLinks: [resourceLink] } : {}), }; } } /** * Get all console logs */ getConsoleLogs(): string[] { return this.consoleLogs; } /** * Clear all console logs */ clearConsoleLogs(): void { this.consoleLogs = []; } }