Spec-Zone.ru › Playwright

WebSocket

The WebSocket class represents WebSocket connections within a page. It provides the ability to inspect and manipulate the data being transmitted and received.

If you want to intercept or modify WebSocket frames, consider using WebSocketRoute.

Methods​

isClosed​

Added before v1.9

Indicates that the web socket has been closed.

Usage

webSocket.isClosed();

Returns

  • boolean

url​

Added before v1.9

Contains the URL of the WebSocket.

Usage

webSocket.url();

Returns

  • string

waitForEvent​

Added before v1.9

Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy value. Will throw an error if the webSocket is closed before the event is fired. Returns the event data value.

Usage

await webSocket.waitForEvent(event);
await webSocket.waitForEvent(event, optionsOrPredicate, options);

Arguments

  • event string

    Event name, same one would pass into webSocket.on(event).

  • optionsOrPredicate function | Object (optional)

    • predicate function

      Receives the event data and resolves to truthy value when the waiting should resolve.

    • timeout number (optional)

      Maximum time to wait for in milliseconds. Defaults to 0 - no timeout. The default value can be changed via actionTimeout option in the config, or by using the browserContext.setDefaultTimeout() or page.setDefaultTimeout() methods.

    Either a predicate that receives an event or an options object. Optional.

  • options Object (optional)

    • predicate function (optional)

      Receives the event data and resolves to truthy value when the waiting should resolve.

    • signal AbortSignal (optional)

      Allows to cancel the waiting using an AbortSignal. If the signal is aborted, the waiting will be aborted and the operation will throw an error. Note that providing a signal does not disable the default timeout, which can be changed using browserContext.setDefaultTimeout(); pass timeout: 0 to disable the timeout entirely.

Returns

  • Promise<Object>

Events​

on('close')​

Added before v1.9

Fired when the websocket closes.

Usage

webSocket.on('close', data => {});

Event data

  • WebSocket

on('framereceived')​

Fired when the websocket receives a frame.

Usage

webSocket.on('framereceived', data => {});

Event data

  • Object
    • payload string | Buffer

      frame payload

on('framesent')​

Fired when the websocket sends a frame.

Usage

webSocket.on('framesent', data => {});

Event data

  • Object
    • payload string | Buffer

      frame payload

on('socketerror')​

Fired when the websocket has an error.

Usage

webSocket.on('socketerror', data => {});

Event data

  • string

© 2026 Microsoft
Licensed under the Apache License, Version 2.0.
https://playwright.dev/docs/api/class-websocket

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API