Implementation:Microsoft Playwright Generate Channels
| Knowledge Sources | |
|---|---|
| Domains | Code Generation, Protocol Definition |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for generating TypeScript channel interfaces from the protocol YAML definition provided by the Playwright library.
Description
The `generate_channels.js` script is a build-time code generator that reads the Playwright protocol definition (from `protocol.yml`) and generates TypeScript interfaces and validation schemas for the client-server RPC channel. It parses the YAML protocol specification which defines channels (Android, Browser, BrowserContext, Page, Frame, etc.) with their methods, events, and type definitions. The script processes type definitions including primitives (string, boolean, int, float), arrays, enums, objects, channel references (with derived class support), and optional fields. It generates TypeScript type declarations for channel interfaces, initializer types, event types, parameter types, and result types, along with runtime validation schemas using a custom type system (`tString`, `tBoolean`, `tChannel`, etc.). The output files are consumed by both the client and server sides of the protocol.
Usage
Use this script during the build process to regenerate the TypeScript protocol types after modifying `protocol.yml`. Run it via the build system to keep the generated channel types in sync with the protocol definition.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: utils/generate_channels.js
Signature
// Node.js script - no exported classes
// Main functions:
function raise(item) { /* throws on invalid protocol items */ }
function titleCase(name) { /* converts to TitleCase */ }
function inlineType(type, indent, wrapEnums) { /* converts YAML type to TS + schema */ }
// Entry point reads protocol.yml and generates:
// - packages/playwright-core/src/protocol/channels.ts (TypeScript interfaces)
// - packages/playwright-core/src/protocol/validator.ts (runtime validators)
Import
// This is a standalone CLI script, not imported as a module
// Usage: node utils/generate_channels.js
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| protocol.yml | YAML file | Yes | Protocol definition with channels, methods, events, and types |
Outputs
| Name | Type | Description |
|---|---|---|
| channels.ts | TypeScript file | Generated channel interfaces, parameter types, result types, and event types |
| validator.ts | TypeScript file | Generated runtime validation schemas for protocol messages |
Usage Examples
// Run the generator from the repo root
// node utils/generate_channels.js
// Protocol YAML format example:
// Browser:
// type: interface
// methods:
// newContext:
// parameters:
// options: BrowserContextOptions?
// returns:
// context: BrowserContext
// events:
// close:
// Generates TypeScript like:
// export interface BrowserChannel {
// newContext(params: BrowserNewContextParams): Promise<BrowserNewContextResult>;
// }
// export type BrowserNewContextParams = { options?: BrowserContextOptions };
// export type BrowserNewContextResult = { context: BrowserContextChannel };