Skip to content

Commit 4f0a964

Browse files
authored
docs(dotnet): add Expect(callback).ToPassAsync() (#44076)
1 parent bac1f9f commit 4f0a964

3 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# class: FunctionAssertions
2+
* since: v1.66
3+
* langs: csharp
4+
5+
The [FunctionAssertions] class provides assertion methods that retry a block of code. A new instance of [FunctionAssertions] is created by calling [`method: PlaywrightAssertions.expectFunction`]:
6+
7+
```csharp
8+
using Microsoft.Playwright;
9+
using Microsoft.Playwright.MSTest;
10+
11+
namespace PlaywrightTests;
12+
13+
[TestClass]
14+
public class ExampleTests : PageTest
15+
{
16+
[TestMethod]
17+
public async Task ApiEventuallySucceeds()
18+
{
19+
await Expect(async () =>
20+
{
21+
var response = await Page.APIRequest.GetAsync("https://api.example.com");
22+
Assert.AreEqual(200, response.Status);
23+
}).ToPassAsync();
24+
}
25+
}
26+
```
27+
28+
## async method: FunctionAssertions.toPass
29+
* since: v1.66
30+
31+
Retries the callback until it completes without throwing an exception, or until the timeout is reached. When the timeout is reached, throws a [PlaywrightAssertionException] with the last exception that the callback has thrown.
32+
33+
**Usage**
34+
35+
```csharp
36+
await Expect(async () =>
37+
{
38+
var response = await Page.APIRequest.GetAsync("https://api.example.com");
39+
Assert.AreEqual(200, response.Status);
40+
}).ToPassAsync(new()
41+
{
42+
// Probe, wait 1s, probe, wait 2s, probe, wait 10s, probe, wait 10s, probe
43+
Intervals = [1_000, 2_000, 10_000],
44+
Timeout = 60_000,
45+
});
46+
```
47+
48+
### option: FunctionAssertions.toPass.intervals
49+
* since: v1.66
50+
- `intervals` <[Array]<[int]>>
51+
52+
Time to wait between the retries in milliseconds. The last interval is used for all the following retries. Defaults to `[100, 250, 500, 1000]`.
53+
54+
### option: FunctionAssertions.toPass.timeout
55+
* since: v1.66
56+
- `timeout` <[float]>
57+
58+
Time to retry the callback for in milliseconds. Defaults to `30000`, change the default with `SetDefaultExpectToPassTimeout`. Pass `0` to disable the timeout.

‎docs/src/api/class-playwrightassertions.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,30 @@ PlaywrightAssertions.assertThat(response).isOK();
9595

9696
[APIResponse] object to use for assertions.
9797

98+
## method: PlaywrightAssertions.expectFunction
99+
* since: v1.66
100+
* langs: csharp
101+
- alias-csharp: Expect
102+
- returns: <[FunctionAssertions]>
103+
104+
Creates a [FunctionAssertions] object for the given callback.
105+
106+
**Usage**
107+
108+
```csharp
109+
await Expect(async () =>
110+
{
111+
var response = await Page.APIRequest.GetAsync("https://api.example.com");
112+
Assert.AreEqual(200, response.Status);
113+
}).ToPassAsync();
114+
```
115+
116+
### param: PlaywrightAssertions.expectFunction.callback
117+
* since: v1.66
118+
- `callback` <[Func<Task>]>
119+
120+
Callback to retry. A synchronous `Action` is accepted as well.
121+
98122
## method: PlaywrightAssertions.expectGeneric
99123
* since: v1.9
100124
* langs: js

‎docs/src/test-assertions-csharp-java-python.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,41 @@ The following assertions will retry until the assertion passes or the assertion
5858
| [`method: PageAssertions.toHaveURL`] | Page has a URL |
5959
| [`method: APIResponseAssertions.toBeOK`] | Response has an OK status |
6060

61+
## Expect.ToPassAsync
62+
* langs: csharp
63+
64+
Prefer auto-retrying assertions whenever possible. For more complex checks, retry a block of code until it passes with [`method: FunctionAssertions.toPass`].
65+
66+
```csharp
67+
await Expect(async () =>
68+
{
69+
var response = await Page.APIRequest.GetAsync("https://api.example.com");
70+
Assert.AreEqual(200, response.Status);
71+
}).ToPassAsync();
72+
```
73+
74+
You can also specify custom timeout and retry intervals:
75+
76+
```csharp
77+
await Expect(async () =>
78+
{
79+
var response = await Page.APIRequest.GetAsync("https://api.example.com");
80+
Assert.AreEqual(200, response.Status);
81+
}).ToPassAsync(new()
82+
{
83+
// Probe, wait 1s, probe, wait 2s, probe, wait 10s, probe, wait 10s, probe
84+
// ... Defaults to [100, 250, 500, 1000].
85+
Intervals = [1_000, 2_000, 10_000],
86+
Timeout = 60_000,
87+
});
88+
```
89+
90+
By default, `ToPassAsync` retries for 30 seconds. `SetDefaultExpectTimeout` does not change this timeout, use `SetDefaultExpectToPassTimeout` instead:
91+
92+
```csharp
93+
SetDefaultExpectToPassTimeout(60_000);
94+
```
95+
6196
## Soft assertions
6297
* langs: python
6398

0 commit comments

Comments
 (0)