Skip to main content
Glama

playwright_console_logs

Retrieve and filter browser console logs with customizable options like log type, search text, and limit. Automatically clear logs post-retrieval if needed. Essential for debugging and monitoring web interactions.

Instructions

Retrieve console logs from the browser with filtering options

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
clearNoWhether to clear logs after retrieval (default: false)
limitNoMaximum number of logs to return
searchNoText to search for in logs (handles text with square brackets)
typeNoType of logs to retrieve (all, error, warning, log, info, debug, exception)

Implementation Reference

  • The execute method in ConsoleLogsTool that implements the core logic for retrieving, filtering, and optionally clearing browser console logs based on input parameters.
    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 { return createSuccessResponse([ `Retrieved ${logs.length} console log(s):`, ...logs ]); } }
  • The tool definition including name, description, and input schema for validation in createToolDefinitions().
    { 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: [], }, },
  • Switch case in handleToolCall function that registers and dispatches execution to the consoleLogsTool for the 'playwright_console_logs' tool.
    case "playwright_console_logs": return await consoleLogsTool.execute(args, context);
  • Instantiation of the ConsoleLogsTool instance in the initializeTools function.
    if (!consoleLogsTool) consoleLogsTool = new ConsoleLogsTool(server);
  • The registerConsoleMessage function that sets up Playwright page event listeners to capture and register console messages, errors, and unhandled rejections into the tool's log storage.
    async function registerConsoleMessage(page) { page.on("console", (msg) => { if (consoleLogsTool) { const type = msg.type(); const text = msg.text(); // "Unhandled Rejection In Promise" we injected if (text.startsWith("[Playwright]")) { const payload = text.replace("[Playwright]", ""); consoleLogsTool.registerConsoleMessage("exception", payload); } else { consoleLogsTool.registerConsoleMessage(type, text); } } }); // Uncaught exception page.on("pageerror", (error) => { if (consoleLogsTool) { const message = error.message; const stack = error.stack || ""; consoleLogsTool.registerConsoleMessage("exception", `${message}\n${stack}`); } }); // Unhandled rejection in promise await page.addInitScript(() => { window.addEventListener("unhandledrejection", (event) => { const reason = event.reason; const message = typeof reason === "object" && reason !== null ? reason.message || JSON.stringify(reason) : String(reason); const stack = reason?.stack || ""; // Use console.error get "Unhandled Rejection In Promise" console.error(`[Playwright][Unhandled Rejection In Promise] ${message}\n${stack}`); }); }); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/executeautomation/mcp-playwright'

If you have feedback or need assistance with the MCP directory API, please join our Discord server