Skip to content

WebSocket ​

Added in: v1.8

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 ​

web_socket.expect_event() ​

Added in: v1.8

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.

Arguments

  • event string

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

  • options Object (optional)

    • predicate function() (optional)

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

    • timeout float (optional)

      Maximum time to wait for in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout. The default value can be changed by using the browser_context.set_default_timeout().

Returns

  • EventContextManager

web_socket.is_closed() ​

Added in: v1.8

Indicates that the web socket has been closed.

Returns

  • boolean

web_socket.url() ​

Added in: v1.8

Contains the URL of the WebSocket.

Returns

  • string

web_socket.wait_for_event() ​

Added in: v1.8

INFO

In most cases, you should use web_socket.expect_event().

Waits for given event to fire. If predicate is provided, it passes event's value into the predicate function and waits for predicate(event) to return a truthy value. Will throw an error if the socket is closed before the event is fired.

Arguments

  • event string

    Event name, same one typically passed into *.on(event).

  • options Object (optional)

    • predicate function() (optional)

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

    • timeout float (optional)

      Maximum time to wait for in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout. The default value can be changed by using the browser_context.set_default_timeout().

Returns

  • any

Events ​

webSocket.on("close") ​

Added in: v1.8

Fired when the websocket closes.

Event data

  • WebSocket

webSocket.on("frameReceived") ​

Added in: v1.9

Fired when the websocket receives a frame.

Event data

  • string | Buffer

webSocket.on("frameSent") ​

Added in: v1.9

Fired when the websocket sends a frame.

Event data

  • string | Buffer

webSocket.on("socketError") ​

Added in: v1.9

Fired when the websocket has an error.

Event data

  • string

Released under the Apache 2.0 License.