Implementation:Microsoft Playwright Client ConsoleMessage
| Knowledge Sources | |
|---|---|
| Domains | Browser Automation, Debugging |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for representing browser console messages captured during page execution provided by the Playwright library.
Description
The ConsoleMessage class implements the api.ConsoleMessage interface and wraps console output events from browser contexts or Electron applications. It stores references to the originating Page and Worker (both nullable), and the raw channel event data. The class provides accessor methods for the console message type (log, error, warning, etc.), the text content, the source location (URL, line, column), the timestamp, and the arguments as JSHandle instances. It also supports Node.js custom inspection via platform.inspectCustom.
Usage
Use ConsoleMessage to inspect console output captured during test execution. Obtain instances by listening to the 'console' event on a Page, BrowserContext, or ElectronApplication.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File:
packages/playwright-core/src/client/consoleMessage.ts
Signature
export class ConsoleMessage implements api.ConsoleMessage {
constructor(
platform: Platform,
event: channels.BrowserContextConsoleEvent | channels.ElectronApplicationConsoleEvent,
page: Page | null,
worker: Worker | null
);
worker(): Worker | null;
page(): Page | null;
type(): string;
text(): string;
args(): JSHandle[];
location(): ConsoleMessageLocation;
timestamp(): number;
}
Import
import { ConsoleMessage } from 'playwright-core/src/client/consoleMessage';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| platform | Platform |
Yes | Platform abstraction for custom inspection support |
| event | ElectronApplicationConsoleEvent | Yes | The raw console event data from the channel |
| page | null | Yes | The page that produced the console message, if available |
| worker | null | Yes | The worker that produced the console message, if available |
Outputs
| Name | Type | Description |
|---|---|---|
| type() | string |
The console message type (e.g., 'log', 'error', 'warning', 'info') |
| text() | string |
The text content of the console message |
| args() | JSHandle[] |
The message arguments as JSHandle instances |
| location() | ConsoleMessageLocation |
Source location with url, lineNumber, columnNumber |
| timestamp() | number |
The timestamp when the message was produced |
Usage Examples
page.on('console', (msg) => {
console.log(`[${msg.type()}] ${msg.text()}`);
console.log('Location:', msg.location());
console.log('Page:', msg.page()?.url());
// Access arguments
for (const arg of msg.args()) {
console.log('Arg:', arg.toString());
}
});
await page.evaluate(() => console.log('Hello from browser'));