Skip to content

Commit

Permalink
chore: fix ToT roll (#1562)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Oct 16, 2024
1 parent fff4a1d commit aefc8f8
Show file tree
Hide file tree
Showing 27 changed files with 639 additions and 60 deletions.
43 changes: 43 additions & 0 deletions dotnet/docs/api/class-locator.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,49 @@ var button = page.GetByRole(AriaRole.Button).And(page.GetByTitle("Subscribe"));

---

### AriaSnapshotAsync {#locator-aria-snapshot}

<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.49</font><x-search>locator.AriaSnapshotAsync</x-search>

Captures the aria snapshot of the given element. See [Expect(Locator).ToMatchAriaSnapshotAsync()](/api/class-locatorassertions.mdx#locator-assertions-to-match-aria-snapshot) for the corresponding assertion.

**Usage**

```csharp
await page.GetByRole(AriaRole.Link).AriaSnapshotAsync();
```

**Returns**
- [string]<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="locator-aria-snapshot-return"/><a href="#locator-aria-snapshot-return" class="list-anchor">#</a>

**Details**

This method captures the aria snapshot of the given element. The snapshot is a string that represents the state of the element and its children. The snapshot can be used to assert the state of the element in the test, or to compare it to state in the future.

The ARIA snapshot is represented using [YAML](https://yaml.org/spec/1.2.2/) markup language:
* The keys of the objects are the roles and optional accessible names of the elements.
* The values are either text content or an array of child elements.
* Generic static text can be represented with the `text` key.

Below is the HTML markup and the respective ARIA snapshot:

```html
<ul aria-label="Links">
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<ul>
```

```yml
- list "Links":
- listitem:
- link "Home"
- listitem:
- link "About"
```
---
### BlurAsync {#locator-blur}
<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.28</font><x-search>locator.BlurAsync</x-search>
Expand Down
24 changes: 24 additions & 0 deletions dotnet/docs/api/class-locatorassertions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,30 @@ await Expect(locator).ToHaveValuesAsync(new Regex[] { new Regex("R"), new Regex(

---

### ToMatchAriaSnapshotAsync {#locator-assertions-to-match-aria-snapshot}

<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.49</font><x-search>locatorAssertions.ToMatchAriaSnapshotAsync</x-search>

Asserts that the target element matches the given accessibility snapshot.

**Usage**

```csharp
await page.GotoAsync("https://demo.playwright.dev/todomvc/");
await Expect(page.Locator("body")).ToMatchAriaSnapshotAsync(@"
- heading ""todos""
- textbox ""What needs to be done?""
");
```

**Arguments**
- `expected` [string]<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="locator-assertions-to-match-aria-snapshot-option-expected"/><a href="#locator-assertions-to-match-aria-snapshot-option-expected" class="list-anchor">#</a>

**Returns**
- [void]<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="locator-assertions-to-match-aria-snapshot-return"/><a href="#locator-assertions-to-match-aria-snapshot-return" class="list-anchor">#</a>

---

## Properties

### Not {#locator-assertions-not}
Expand Down
40 changes: 27 additions & 13 deletions dotnet/docs/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
2 changes: 1 addition & 1 deletion dotnet/docs/locators.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ var button = page.GetByRole(AriaRole.Button).And(page.GetByTitle("Subscribe"));

### Matching one of the two alternative locators

If you'd like to target one of the two or more elements, and you don't know which one it will be, use [Locator.Or()](/api/class-locator.mdx#locator-or) to create a locator that matches all of the alternatives.
If you'd like to target one of the two or more elements, and you don't know which one it will be, use [Locator.Or()](/api/class-locator.mdx#locator-or) to create a locator that matches any one or both of the alternatives.

For example, consider a scenario where you'd like to click on a "New email" button, but sometimes a security settings dialog shows up instead. In this case, you can wait for either a "New email" button, or a dialog and act accordingly.

Expand Down
29 changes: 29 additions & 0 deletions dotnet/docs/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/docs/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
43 changes: 43 additions & 0 deletions java/docs/api/class-locator.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,49 @@ Locator button = page.getByRole(AriaRole.BUTTON).and(page.getByTitle("Subscribe"

---

### ariaSnapshot {#locator-aria-snapshot}

<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.49</font><x-search>locator.ariaSnapshot</x-search>

Captures the aria snapshot of the given element. See [assertThat(locator).matchesAriaSnapshot()](/api/class-locatorassertions.mdx#locator-assertions-to-match-aria-snapshot) for the corresponding assertion.

**Usage**

```java
page.getByRole(AriaRole.LINK).ariaSnapshot();
```

**Returns**
- [String]<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="locator-aria-snapshot-return"/><a href="#locator-aria-snapshot-return" class="list-anchor">#</a>

**Details**

This method captures the aria snapshot of the given element. The snapshot is a string that represents the state of the element and its children. The snapshot can be used to assert the state of the element in the test, or to compare it to state in the future.

The ARIA snapshot is represented using [YAML](https://yaml.org/spec/1.2.2/) markup language:
* The keys of the objects are the roles and optional accessible names of the elements.
* The values are either text content or an array of child elements.
* Generic static text can be represented with the `text` key.

Below is the HTML markup and the respective ARIA snapshot:

```html
<ul aria-label="Links">
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<ul>
```

```yml
- list "Links":
- listitem:
- link "Home"
- listitem:
- link "About"
```
---
### blur {#locator-blur}
<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.28</font><x-search>locator.blur</x-search>
Expand Down
24 changes: 24 additions & 0 deletions java/docs/api/class-locatorassertions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,30 @@ assertThat(

---

### matchesAriaSnapshot {#locator-assertions-to-match-aria-snapshot}

<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.49</font><x-search>locatorAssertions.matchesAriaSnapshot</x-search>

Asserts that the target element matches the given accessibility snapshot.

**Usage**

```java
page.navigate("https://demo.playwright.dev/todomvc/");
assertThat(page.locator("body")).matchesAriaSnapshot("""
- heading "todos"
- textbox "What needs to be done?"
""");
```

**Arguments**
- `expected` [String]<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="locator-assertions-to-match-aria-snapshot-option-expected"/><a href="#locator-assertions-to-match-aria-snapshot-option-expected" class="list-anchor">#</a>

**Returns**
- [void]<a aria-hidden="true" tabIndex="-1" class="list-anchor-link" id="locator-assertions-to-match-aria-snapshot-return"/><a href="#locator-assertions-to-match-aria-snapshot-return" class="list-anchor">#</a>

---

## Properties

### not() {#locator-assertions-not}
Expand Down
22 changes: 17 additions & 5 deletions java/docs/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
2 changes: 1 addition & 1 deletion java/docs/locators.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ Locator button = page.getByRole(AriaRole.BUTTON).and(page.getByTitle("Subscribe"

### Matching one of the two alternative locators

If you'd like to target one of the two or more elements, and you don't know which one it will be, use [Locator.or()](/api/class-locator.mdx#locator-or) to create a locator that matches all of the alternatives.
If you'd like to target one of the two or more elements, and you don't know which one it will be, use [Locator.or()](/api/class-locator.mdx#locator-or) to create a locator that matches any one or both of the alternatives.

For example, consider a scenario where you'd like to click on a "New email" button, but sometimes a security settings dialog shows up instead. In this case, you can wait for either a "New email" button, or a dialog and act accordingly.

Expand Down
Loading

0 comments on commit aefc8f8

Please sign in to comment.