Implementation:Microsoft Playwright Client Platform
| Knowledge Sources | |
|---|---|
| Domains | Browser Automation, Platform Abstraction |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for abstracting platform-specific functionality (Node.js, web, or empty) used by the Playwright client provided by the Playwright library.
Description
This module defines the Platform type and the Zone type, along with an emptyPlatform default implementation. The Platform type is an interface that abstracts all environment-dependent operations:
- name: Identifies the platform as
'node','web', or'empty'. - File system:
fs()andpath()provide access to Node.js fs and path modules. - Crypto:
calculateSha1()for hashing,createGuid()for UUID generation. - Logging:
log(),isLogEnabled(),colorsfor colored output. - Debug:
isDebugMode(),isJSDebuggerAttached(),isUnderTest(),showInternalStackFrames(). - Streams:
streamFile(),streamReadable(),streamWritable()for I/O streaming. - Zones: A zone system (
Zonetype) for tracking async context with push/pop/run/data operations. - Environment:
envfor environment variables,boxedStackPrefixes()for stack trace filtering.
The emptyPlatform provides a no-op/throwing implementation for all methods, used as a fallback when no platform is configured.
Usage
The Platform type is used throughout the Playwright client to abstract away Node.js-specific APIs, enabling the client code to potentially run in non-Node environments (e.g., browsers). Concrete implementations are provided elsewhere (e.g., nodePlatform).
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File:
packages/playwright-core/src/client/platform.ts
Signature
export type Zone = {
push(data: unknown): Zone;
pop(): Zone;
run<R>(func: () => R): R;
data<T>(): T | undefined;
};
export type Platform = {
name: 'node' | 'web' | 'empty';
boxedStackPrefixes: () => string[];
calculateSha1: (text: string) => Promise<string>;
colors: Colors;
coreDir?: string;
createGuid: () => string;
defaultMaxListeners: () => number;
env: Record<string, string | undefined>;
fs: () => typeof fs;
inspectCustom: symbol | undefined;
isDebugMode: () => boolean;
isJSDebuggerAttached: () => boolean;
isLogEnabled: (name: 'api' | 'channel') => boolean;
isUnderTest: () => boolean;
log: (name: 'api' | 'channel', message: string | Error | object) => void;
path: () => typeof path;
pathSeparator: string;
showInternalStackFrames: () => boolean;
streamFile: (path: string, writable: Writable) => Promise<void>;
streamReadable: (channel: channels.StreamChannel) => Readable;
streamWritable: (channel: channels.WritableStreamChannel) => Writable;
zodToJsonSchema: (schema: any) => any;
zones: { empty: Zone; current: () => Zone };
};
export const emptyPlatform: Platform;
Import
import type { Platform, Zone } from 'playwright-core/src/client/platform';
import { emptyPlatform } from 'playwright-core/src/client/platform';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (none) | N/A | N/A | Platform is a type definition and constant; no construction inputs |
Outputs
| Name | Type | Description |
|---|---|---|
| emptyPlatform | Platform |
A default Platform implementation where all methods either no-op or throw 'Not implemented' |
Usage Examples
import type { Platform } from 'playwright-core/src/client/platform';
// Platform is used internally by ChannelOwner and other classes:
class MyChannelOwner extends ChannelOwner {
doSomething() {
// Access platform-specific functionality
const guid = this._platform.createGuid();
this._platform.log('api', 'Something happened');
if (this._platform.isDebugMode()) {
// Extra debug logging
}
// Zone-based context tracking
const zone = this._platform.zones.current();
const data = zone.data<MyContext>();
}
}