Implementation:Microsoft Playwright CrExecutionContext
| Knowledge Sources | |
|---|---|
| Domains | Chromium, JavaScript |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for evaluating JavaScript expressions within a Chromium execution context via CDP provided by the Playwright library.
Description
The `CRExecutionContext` class implements the `js.ExecutionContextDelegate` interface for Chrome DevTools Protocol. It wraps a `CRSession` and a CDP execution context ID. The class provides `rawEvaluateJSON` for expression evaluation with `returnByValue: true`, `rawEvaluateHandle` for obtaining remote object handles, and `evaluateWithArguments` for calling functions via `Runtime.callFunctionOn` with serialized arguments and handle references. It uses CDP helper functions (`getExceptionMessage`, `releaseObject`) for error handling and object lifecycle management. The companion `createHandle` function converts CDP `RemoteObject` instances into Playwright `JSHandle` or `ElementHandle` objects.
Usage
Use CrExecutionContext when Playwright evaluates JavaScript in Chromium page frames, web workers, or service workers through the CDP protocol.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/server/chromium/crExecutionContext.ts
Signature
export class CRExecutionContext implements js.ExecutionContextDelegate {
_client: CRSession;
_contextId: number;
constructor(client: CRSession, contextPayload: Protocol.Runtime.ExecutionContextDescription);
async rawEvaluateJSON(expression: string): Promise<any>;
async rawEvaluateHandle(context: js.ExecutionContext, expression: string): Promise<js.JSHandle>;
async evaluateWithArguments(expression: string, returnByValue: boolean, utilityScript: js.JSHandle, values: any[], handles: js.JSHandle[]): Promise<any>;
}
export function createHandle(context: js.ExecutionContext, remoteObject: Protocol.Runtime.RemoteObject): js.JSHandle;
Import
import { CRExecutionContext, createHandle } from '../server/chromium/crExecutionContext';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| client | CRSession | Yes | CDP session for the target |
| contextPayload | Protocol.Runtime.ExecutionContextDescription | Yes | CDP execution context descriptor with ID |
| expression | string | Yes | JavaScript expression or function body to evaluate |
Outputs
| Name | Type | Description |
|---|---|---|
| result | any | Deserialized evaluation result value |
| handle | js.JSHandle | Remote handle referencing the evaluation result |
Usage Examples
import { CRExecutionContext } from '../server/chromium/crExecutionContext';
const execContext = new CRExecutionContext(crSession, { id: 1, origin: '', name: '' });
const title = await execContext.rawEvaluateJSON('document.title');
const bodyHandle = await execContext.rawEvaluateHandle(context, 'document.body');