Skip to content

Commit

Permalink
docs(websocket): add web socket examples (#5927)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Mar 24, 2021
1 parent 1b80233 commit cc265fe
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
15 changes: 8 additions & 7 deletions docs/src/events.md
Expand Up @@ -3,18 +3,18 @@ id: events
title: "Events"
---

Playwright allows listening to various types of events happening in the browser, such
Playwright allows listening to various types of events happening in the web page, such
as network requests, creation of child pages, dedicated workers etc. There are several
ways to subscribe to such events:

<!-- TOC -->

## Waiting for event

There is a number of methods which wait for a particular condition or an event happening
in the page. They are usually the preferred choice. Here is a few examples:
Most of the time, scripts will need to wait for a particular event to happen. Below are some of the typical event
awaiting patterns.

Waiting for a request with a specified url:
Wait for a request with the specified url:

```js
const [request] = await Promise.all([
Expand Down Expand Up @@ -46,7 +46,7 @@ with page.expect_request("**/*logo*.png") as first:
print(first.value.url)
```

Waiting for popup window:
Wait for popup window:

```js
const [popup] = await Promise.all([
Expand Down Expand Up @@ -80,7 +80,8 @@ popup.value.goto("https://wikipedia.org")

## Adding/removing event listener

Playwright supports traditional language mechanisms to subscribe and unsubscribe from events:
Sometimes, events happen in random time and instead of waiting for them, they need to be handled.
Playwright supports traditional language mechanisms for subscribing and unsubscribing from the events:

```js
page.on('request', request => console.log(`Request sent: ${request.url()}`));
Expand Down Expand Up @@ -135,7 +136,7 @@ page.goto("https://www.openstreetmap.org/")

## Adding one-off listeners

If only next event needs to be handled, there is a convinience API for some of the events:
If certain event needs to be handled once, there is a convenience API for that:

```js
page.once('dialog', dialog => dialog.accept("2021"));
Expand Down
45 changes: 44 additions & 1 deletion docs/src/network.md
Expand Up @@ -468,4 +468,47 @@ page.route("**/*", lambda route: route.abort() if route.request.resource_type ==
- [`method: BrowserContext.route`]
- [`method: Route.abort`]

<br/>
<br/>

## WebSockets

Playwright supports [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) inspection out of the
box. Every time WebSocket is created, [`event: Page.webSocket`] event is fired. This event contains the [WebSocket]
instance for further web socket frames inspection:

```js
page.on('websocket', ws => {
console.log(`WebSocket opened: ${ws.url()}>`);
ws.on('framesent', event => console.log(event.payload));
ws.on('framereceived', event => console.log(event.payload));
ws.on('close', () => console.log('WebSocket closed'));
});
```

```java
page.onWebSocket(ws -> {
log("WebSocket opened: " + ws.url());
ws.onFrameSent(frameData -> log(frameData.text()));
ws.onFrameReceived(frameData -> log(frameData.text()));
ws.onClose(ws1 -> log("WebSocket closed"));
});
```

```python
def on_web_socket(ws):
print(f"WebSocket opened: {ws.url}")
ws.on("framesent", lambda payload: print(payload))
ws.on("framereceived", lambda payload: print(payload))
ws.on("close", lambda payload: print("WebSocket closed"))

page.on("websocket", on_web_socket)
```

### API reference
- [WebSocket]
- [`event: Page.webSocket`]
- [`event: WebSocket.frameSent`]
- [`event: WebSocket.frameReceived`]
- [`event: WebSocket.close`]

<br/>

0 comments on commit cc265fe

Please sign in to comment.