Skip to content

Commit

Permalink
feat(roll): roll to 1.48 Playwright (#1563)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
playwrightmachine and github-actions[bot] authored Oct 16, 2024
1 parent aefc8f8 commit 0a30d1a
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 55 deletions.
40 changes: 27 additions & 13 deletions dotnet/versioned_docs/version-stable/api/class-websocketroute.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Whenever a [`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSoc
By default, the routed WebSocket will not connect to the server. This way, you can mock entire communcation over the WebSocket. Here is an example that responds to a `"request"` with a `"response"`.

```csharp
await page.RouteWebSocketAsync("/ws", ws => {
await page.RouteWebSocketAsync("wss://example.com/ws", ws => {
ws.OnMessage(message => {
if (message == "request")
ws.Send("response");
Expand All @@ -24,6 +24,23 @@ await page.RouteWebSocketAsync("/ws", ws => {

Since we do not call [WebSocketRoute.ConnectToServer](/api/class-websocketroute.mdx#web-socket-route-connect-to-server) inside the WebSocket route handler, Playwright assumes that WebSocket will be mocked, and opens the WebSocket inside the page automatically.

Here is another example that handles JSON messages:

```csharp
await page.RouteWebSocketAsync("wss://example.com/ws", ws => {
ws.OnMessage(message => {
using var jsonDoc = JsonDocument.Parse(message);
JsonElement root = jsonDoc.RootElement;
if (root.TryGetProperty("request", out JsonElement requestElement) && requestElement.GetString() == "question")
{
var response = new Dictionary<string, string> { ["response"] = "answer" };
string jsonResponse = JsonSerializer.Serialize(response);
ws.Send(jsonResponse);
}
});
});
```

**Intercepting**

Alternatively, you may want to connect to the actual server, but intercept messages in-between and modify or block them. Calling [WebSocketRoute.ConnectToServer](/api/class-websocketroute.mdx#web-socket-route-connect-to-server) returns a server-side `WebSocketRoute` instance that you can send messages to, or handle incoming messages.
Expand All @@ -44,11 +61,11 @@ await page.RouteWebSocketAsync("/ws", ws => {

After connecting to the server, all **messages are forwarded** between the page and the server by default.

However, if you call [WebSocketRoute.OnMessageAsync()](/api/class-websocketroute.mdx#web-socket-route-on-message) on the original route, messages from the page to the server **will not be forwarded** anymore, but should instead be handled by the [handler](/api/class-websocketroute.mdx#web-socket-route-on-message-option-handler).
However, if you call [WebSocketRoute.OnMessage()](/api/class-websocketroute.mdx#web-socket-route-on-message) on the original route, messages from the page to the server **will not be forwarded** anymore, but should instead be handled by the [handler](/api/class-websocketroute.mdx#web-socket-route-on-message-option-handler).

Similarly, calling [WebSocketRoute.OnMessageAsync()](/api/class-websocketroute.mdx#web-socket-route-on-message) on the server-side WebSocket will **stop forwarding messages** from the server to the page, and [handler](/api/class-websocketroute.mdx#web-socket-route-on-message-option-handler) should take care of them.
Similarly, calling [WebSocketRoute.OnMessage()](/api/class-websocketroute.mdx#web-socket-route-on-message) on the server-side WebSocket will **stop forwarding messages** from the server to the page, and [handler](/api/class-websocketroute.mdx#web-socket-route-on-message-option-handler) should take care of them.

The following example blocks some messages in both directions. Since it calls [WebSocketRoute.OnMessageAsync()](/api/class-websocketroute.mdx#web-socket-route-on-message) in both directions, there is no automatic forwarding at all.
The following example blocks some messages in both directions. Since it calls [WebSocketRoute.OnMessage()](/api/class-websocketroute.mdx#web-socket-route-on-message) in both directions, there is no automatic forwarding at all.

```csharp
await page.RouteWebSocketAsync("/ws", ws => {
Expand Down Expand Up @@ -102,8 +119,8 @@ await WebSocketRoute.CloseAsync(options);
By default, routed WebSocket does not connect to the server, so you can mock entire WebSocket communication. This method connects to the actual WebSocket server, and returns the server-side [WebSocketRoute] instance, giving the ability to send and receive messages from the server.

Once connected to the server:
* Messages received from the server will be **automatically forwarded** to the WebSocket in the page, unless [WebSocketRoute.OnMessageAsync()](/api/class-websocketroute.mdx#web-socket-route-on-message) is called on the server-side `WebSocketRoute`.
* Messages sent by the [`WebSocket.send()`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send) call in the page will be **automatically forwarded** to the server, unless [WebSocketRoute.OnMessageAsync()](/api/class-websocketroute.mdx#web-socket-route-on-message) is called on the original `WebSocketRoute`.
* Messages received from the server will be **automatically forwarded** to the WebSocket in the page, unless [WebSocketRoute.OnMessage()](/api/class-websocketroute.mdx#web-socket-route-on-message) is called on the server-side `WebSocketRoute`.
* Messages sent by the [`WebSocket.send()`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send) call in the page will be **automatically forwarded** to the server, unless [WebSocketRoute.OnMessage()](/api/class-websocketroute.mdx#web-socket-route-on-message) is called on the original `WebSocketRoute`.

See examples at the top for more details.

Expand Down Expand Up @@ -133,15 +150,15 @@ WebSocketRoute.OnClose(handler);
```

**Arguments**
- `handler` [Action]&lt;[number]?&gt;<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="web-socket-route-on-close-option-handler"/><a href="#web-socket-route-on-close-option-handler" class="list-anchor">#</a>
- `handler` Action&lt;int?, string&gt;<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="web-socket-route-on-close-option-handler"/><a href="#web-socket-route-on-close-option-handler" class="list-anchor">#</a>

Function that will handle WebSocket closure. Received an optional [close code](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close#code) and an optional [close reason](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close#reason).

---

### OnMessageAsync {#web-socket-route-on-message}
### OnMessage {#web-socket-route-on-message}

<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.48</font><x-search>webSocketRoute.OnMessageAsync</x-search>
<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.48</font><x-search>webSocketRoute.OnMessage</x-search>

This method allows to handle messages that are sent by the WebSocket, either from the page or from the server.

Expand All @@ -154,17 +171,14 @@ Calling this method again will override the handler with a new one.
**Usage**

```csharp
await WebSocketRoute.OnMessageAsync(handler);
WebSocketRoute.OnMessage(handler);
```

**Arguments**
- `handler` [Action]&lt;[WebSocketFrame]&gt;<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="web-socket-route-on-message-option-handler"/><a href="#web-socket-route-on-message-option-handler" class="list-anchor">#</a>

Function that will handle messages.

**Returns**
- [void]<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="web-socket-route-on-message-return"/><a href="#web-socket-route-on-message-return" class="list-anchor">#</a>

---

### Send {#web-socket-route-send}
Expand Down
29 changes: 29 additions & 0 deletions dotnet/versioned_docs/version-stable/mock.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,35 @@ pwsh bin/Debug/netX/playwright.ps1 open --save-har=example.har --save-har-glob="

Read more about [advanced networking](./network.mdx).

## Mock WebSockets

The following code will intercept WebSocket connections and mock entire communcation over the WebSocket, instead of connecting to the server. This example responds to a `"request"` with a `"response"`.

```csharp
await page.RouteWebSocketAsync("wss://example.com/ws", ws => {
ws.OnMessage(message => {
if (message == "request")
ws.Send("response");
});
});
```

Alternatively, you may want to connect to the actual server, but intercept messages in-between and modify or block them. Here is an example that modifies some of the messages sent by the page to the server, and leaves the rest unmodified.

```csharp
await page.RouteWebSocketAsync("wss://example.com/ws", ws => {
var server = ws.ConnectToServer();
ws.OnMessage(message => {
if (message == "request")
server.Send("request2");
else
server.Send(message);
});
});
```

For more details, see [WebSocketRoute].


[Accessibility]: /api/class-accessibility.mdx "Accessibility"
[APIRequest]: /api/class-apirequest.mdx "APIRequest"
Expand Down
6 changes: 4 additions & 2 deletions dotnet/versioned_docs/version-stable/network.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Playwright provides APIs to **monitor** and **modify** browser network traffic,

## Mock APIs

Check out our [API mocking guide](./mock.mdx) to learn more on how to
Check out our [API mocking guide](./mock.mdx) to learn more on how to
- mock API requests and never hit the API
- perform the API request and modify the response
- use HAR files to mock network requests.
Expand Down Expand Up @@ -201,7 +201,9 @@ Important notes:

## WebSockets

Playwright supports [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) inspection out of the box. Every time a WebSocket is created, the [Page.WebSocket](/api/class-page.mdx#page-event-web-socket) event is fired. This event contains the [WebSocket] instance for further web socket frames inspection:
Playwright supports [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) inspection, mocking and modifying out of the box. See our [API mocking guide](./mock.mdx#mock-websockets) to learn how to mock WebSockets.

Every time a WebSocket is created, the [Page.WebSocket](/api/class-page.mdx#page-event-web-socket) event is fired. This event contains the [WebSocket] instance for further web socket frames inspection:

```csharp
page.WebSocket += (_, ws) =>
Expand Down
22 changes: 17 additions & 5 deletions java/versioned_docs/version-stable/api/class-websocketroute.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Whenever a [`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSoc
By default, the routed WebSocket will not connect to the server. This way, you can mock entire communcation over the WebSocket. Here is an example that responds to a `"request"` with a `"response"`.

```java
page.routeWebSocket("/ws", ws -> {
page.routeWebSocket("wss://example.com/ws", ws -> {
ws.onMessage(message -> {
if ("request".equals(message))
ws.send("response");
Expand All @@ -24,6 +24,21 @@ page.routeWebSocket("/ws", ws -> {

Since we do not call [WebSocketRoute.connectToServer()](/api/class-websocketroute.mdx#web-socket-route-connect-to-server) inside the WebSocket route handler, Playwright assumes that WebSocket will be mocked, and opens the WebSocket inside the page automatically.

Here is another example that handles JSON messages:

```java
page.routeWebSocket("wss://example.com/ws", ws -> {
ws.onMessage(message -> {
JsonObject json = new JsonParser().parse(message).getAsJsonObject();
if ("question".equals(json.get("request").getAsString())) {
Map<String, String> result = new HashMap();
result.put("response", "answer");
ws.send(gson.toJson(result));
}
});
});
```

**Intercepting**

Alternatively, you may want to connect to the actual server, but intercept messages in-between and modify or block them. Calling [WebSocketRoute.connectToServer()](/api/class-websocketroute.mdx#web-socket-route-connect-to-server) returns a server-side `WebSocketRoute` instance that you can send messages to, or handle incoming messages.
Expand Down Expand Up @@ -134,7 +149,7 @@ WebSocketRoute.onClose(handler);
```

**Arguments**
- `handler` [Consumer]&lt;[null] | [number]&gt;<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="web-socket-route-on-close-option-handler"/><a href="#web-socket-route-on-close-option-handler" class="list-anchor">#</a>
- `handler` [Consumer]&lt;[null] | [int]&gt;<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="web-socket-route-on-close-option-handler"/><a href="#web-socket-route-on-close-option-handler" class="list-anchor">#</a>

Function that will handle WebSocket closure. Received an optional [close code](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close#code) and an optional [close reason](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close#reason).

Expand Down Expand Up @@ -163,9 +178,6 @@ WebSocketRoute.onMessage(handler);

Function that will handle messages.

**Returns**
- [void]<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="web-socket-route-on-message-return"/><a href="#web-socket-route-on-message-return" class="list-anchor">#</a>

---

### send {#web-socket-route-send}
Expand Down
29 changes: 29 additions & 0 deletions java/versioned_docs/version-stable/mock.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,35 @@ mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="op

Read more about [advanced networking](./network.mdx).

## Mock WebSockets

The following code will intercept WebSocket connections and mock entire communcation over the WebSocket, instead of connecting to the server. This example responds to a `"request"` with a `"response"`.

```java
page.routeWebSocket("wss://example.com/ws", ws -> {
ws.onMessage(message -> {
if ("request".equals(message))
ws.send("response");
});
});
```

Alternatively, you may want to connect to the actual server, but intercept messages in-between and modify or block them. Here is an example that modifies some of the messages sent by the page to the server, and leaves the rest unmodified.

```java
page.routeWebSocket("wss://example.com/ws", ws -> {
WebSocketRoute server = ws.connectToServer();
ws.onMessage(message -> {
if ("request".equals(message))
server.send("request2");
else
server.send(message);
});
});
```

For more details, see [WebSocketRoute].


[APIRequest]: /api/class-apirequest.mdx "APIRequest"
[APIRequestContext]: /api/class-apirequestcontext.mdx "APIRequestContext"
Expand Down
6 changes: 4 additions & 2 deletions java/versioned_docs/version-stable/network.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Playwright provides APIs to **monitor** and **modify** browser network traffic,

## Mock APIs

Check out our [API mocking guide](./mock.mdx) to learn more on how to
Check out our [API mocking guide](./mock.mdx) to learn more on how to
- mock API requests and never hit the API
- perform the API request and modify the response
- use HAR files to mock network requests.
Expand Down Expand Up @@ -196,7 +196,9 @@ Important notes:

## WebSockets

Playwright supports [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) inspection out of the box. Every time a WebSocket is created, the [Page.onWebSocket(handler)](/api/class-page.mdx#page-event-web-socket) event is fired. This event contains the [WebSocket] instance for further web socket frames inspection:
Playwright supports [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) inspection, mocking and modifying out of the box. See our [API mocking guide](./mock.mdx#mock-websockets) to learn how to mock WebSockets.

Every time a WebSocket is created, the [Page.onWebSocket(handler)](/api/class-page.mdx#page-event-web-socket) event is fired. This event contains the [WebSocket] instance for further web socket frames inspection:

```java
page.onWebSocket(ws -> {
Expand Down
19 changes: 14 additions & 5 deletions nodejs/versioned_docs/version-stable/api/class-websocketroute.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Whenever a [`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSoc
By default, the routed WebSocket will not connect to the server. This way, you can mock entire communcation over the WebSocket. Here is an example that responds to a `"request"` with a `"response"`.

```js
await page.routeWebSocket('/ws', ws => {
await page.routeWebSocket('wss://example.com/ws', ws => {
ws.onMessage(message => {
if (message === 'request')
ws.send('response');
Expand All @@ -24,6 +24,18 @@ await page.routeWebSocket('/ws', ws => {

Since we do not call [webSocketRoute.connectToServer()](/api/class-websocketroute.mdx#web-socket-route-connect-to-server) inside the WebSocket route handler, Playwright assumes that WebSocket will be mocked, and opens the WebSocket inside the page automatically.

Here is another example that handles JSON messages:

```js
await page.routeWebSocket('wss://example.com/ws', ws => {
ws.onMessage(message => {
const json = JSON.parse(message);
if (json.request === 'question')
ws.send(JSON.stringify({ response: 'answer' }));
});
});
```

**Intercepting**

Alternatively, you may want to connect to the actual server, but intercept messages in-between and modify or block them. Calling [webSocketRoute.connectToServer()](/api/class-websocketroute.mdx#web-socket-route-connect-to-server) returns a server-side `WebSocketRoute` instance that you can send messages to, or handle incoming messages.
Expand Down Expand Up @@ -155,17 +167,14 @@ Calling this method again will override the handler with a new one.
**Usage**

```js
await webSocketRoute.onMessage(handler);
webSocketRoute.onMessage(handler);
```

**Arguments**
- `handler` [function]\([string]\):[Promise]&lt;[Object]&gt; | [Object]<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="web-socket-route-on-message-option-handler"/><a href="#web-socket-route-on-message-option-handler" class="list-anchor">#</a>

Function that will handle messages.

**Returns**
- [Promise]&lt;[void]&gt;<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="web-socket-route-on-message-return"/><a href="#web-socket-route-on-message-return" class="list-anchor">#</a>

---

### send {#web-socket-route-send}
Expand Down
20 changes: 10 additions & 10 deletions nodejs/versioned_docs/version-stable/ci.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jobs:
name: 'Playwright Tests'
runs-on: ubuntu-latest
container:
image: mcr.microsoft.com/playwright:v1.48.0-jammy
image: mcr.microsoft.com/playwright:v1.48.1-jammy
options: --user 1001
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -340,7 +340,7 @@ trigger:
pool:
vmImage: ubuntu-latest
container: mcr.microsoft.com/playwright:v1.48.0-noble
container: mcr.microsoft.com/playwright:v1.48.1-noble
steps:
- task: NodeTool@0
Expand All @@ -364,7 +364,7 @@ Running Playwright on CircleCI is very similar to running on GitHub Actions. In
executors:
pw-jammy-development:
docker:
- image: mcr.microsoft.com/playwright:v1.48.0-noble
- image: mcr.microsoft.com/playwright:v1.48.1-noble
```
Note: When using the docker agent definition, you are specifying the resource class of where playwright runs to the 'medium' tier [here](https://circleci.com/docs/configuration-reference?#docker-execution-environment). The default behavior of Playwright is to set the number of workers to the detected core count (2 in the case of the medium tier). Overriding the number of workers to greater than this number will cause unnecessary timeouts and failures.
Expand All @@ -387,7 +387,7 @@ Jenkins supports Docker agents for pipelines. Use the [Playwright Docker image](
```groovy
pipeline {
agent { docker { image 'mcr.microsoft.com/playwright:v1.48.0-noble' } }
agent { docker { image 'mcr.microsoft.com/playwright:v1.48.1-noble' } }
stages {
stage('e2e-tests') {
steps {
Expand All @@ -404,7 +404,7 @@ pipeline {
Bitbucket Pipelines can use public [Docker images as build environments](https://confluence.atlassian.com/bitbucket/use-docker-images-as-build-environments-792298897.html). To run Playwright tests on Bitbucket, use our public Docker image ([see Dockerfile](./docker.mdx)).
```yml
image: mcr.microsoft.com/playwright:v1.48.0-noble
image: mcr.microsoft.com/playwright:v1.48.1-noble
```
### GitLab CI
Expand All @@ -417,7 +417,7 @@ stages:
tests:
stage: test
image: mcr.microsoft.com/playwright:v1.48.0-noble
image: mcr.microsoft.com/playwright:v1.48.1-noble
script:
...
```
Expand All @@ -432,7 +432,7 @@ stages:
tests:
stage: test
image: mcr.microsoft.com/playwright:v1.48.0-noble
image: mcr.microsoft.com/playwright:v1.48.1-noble
parallel: 7
script:
- npm ci
Expand All @@ -447,7 +447,7 @@ stages:
tests:
stage: test
image: mcr.microsoft.com/playwright:v1.48.0-noble
image: mcr.microsoft.com/playwright:v1.48.1-noble
parallel:
matrix:
- PROJECT: ['chromium', 'webkit']
Expand All @@ -463,7 +463,7 @@ To run Playwright tests on Google Cloud Build, use our public Docker image ([see
```yml
steps:
- name: mcr.microsoft.com/playwright:v1.48.0-noble
- name: mcr.microsoft.com/playwright:v1.48.1-noble
script:
...
env:
Expand All @@ -481,7 +481,7 @@ type: docker
steps:
- name: test
image: mcr.microsoft.com/playwright:v1.48.0-jammy
image: mcr.microsoft.com/playwright:v1.48.1-jammy
commands:
- npx playwright test
```
Expand Down
Loading

0 comments on commit 0a30d1a

Please sign in to comment.