Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions docs/src/api/class-functionassertions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# class: FunctionAssertions
* since: v1.66
* langs: csharp

The [FunctionAssertions] class provides assertion methods that retry a block of code. A new instance of [FunctionAssertions] is created by calling [`method: PlaywrightAssertions.expectFunction`]:

```csharp
using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;

namespace PlaywrightTests;

[TestClass]
public class ExampleTests : PageTest
{
[TestMethod]
public async Task ApiEventuallySucceeds()
{
await Expect(async () =>
{
var response = await Page.APIRequest.GetAsync("https://api.example.com");
Assert.AreEqual(200, response.Status);
}).ToPassAsync();
}
}
```

## async method: FunctionAssertions.toPass
* since: v1.66

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.

**Usage**

```csharp
await Expect(async () =>
{
var response = await Page.APIRequest.GetAsync("https://api.example.com");
Assert.AreEqual(200, response.Status);
}).ToPassAsync(new()
{
// Probe, wait 1s, probe, wait 2s, probe, wait 10s, probe, wait 10s, probe
Intervals = [1_000, 2_000, 10_000],
Timeout = 60_000,
});
```

### option: FunctionAssertions.toPass.intervals
* since: v1.66
- `intervals` <[Array]<[int]>>

Time to wait between the retries in milliseconds. The last interval is used for all the following retries. Defaults to `[100, 250, 500, 1000]`.

### option: FunctionAssertions.toPass.timeout
* since: v1.66
- `timeout` <[float]>

Time to retry the callback for in milliseconds. Defaults to `30000`, change the default with `SetDefaultExpectToPassTimeout`. Pass `0` to disable the timeout.
24 changes: 24 additions & 0 deletions docs/src/api/class-playwrightassertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ PlaywrightAssertions.assertThat(response).isOK();

[APIResponse] object to use for assertions.

## method: PlaywrightAssertions.expectFunction
* since: v1.66
* langs: csharp
- alias-csharp: Expect
- returns: <[FunctionAssertions]>

Creates a [FunctionAssertions] object for the given callback.

**Usage**

```csharp
await Expect(async () =>
{
var response = await Page.APIRequest.GetAsync("https://api.example.com");
Assert.AreEqual(200, response.Status);
}).ToPassAsync();
```

### param: PlaywrightAssertions.expectFunction.callback
* since: v1.66
- `callback` <[Func<Task>]>

Callback to retry. A synchronous `Action` is accepted as well.

## method: PlaywrightAssertions.expectGeneric
* since: v1.9
* langs: js
Expand Down
35 changes: 35 additions & 0 deletions docs/src/test-assertions-csharp-java-python.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,41 @@ The following assertions will retry until the assertion passes or the assertion
| [`method: PageAssertions.toHaveURL`] | Page has a URL |
| [`method: APIResponseAssertions.toBeOK`] | Response has an OK status |

## Expect.ToPassAsync
* langs: csharp

Prefer auto-retrying assertions whenever possible. For more complex checks, retry a block of code until it passes with [`method: FunctionAssertions.toPass`].

```csharp
await Expect(async () =>
{
var response = await Page.APIRequest.GetAsync("https://api.example.com");
Assert.AreEqual(200, response.Status);
}).ToPassAsync();
```

You can also specify custom timeout and retry intervals:

```csharp
await Expect(async () =>
{
var response = await Page.APIRequest.GetAsync("https://api.example.com");
Assert.AreEqual(200, response.Status);
}).ToPassAsync(new()
{
// Probe, wait 1s, probe, wait 2s, probe, wait 10s, probe, wait 10s, probe
// ... Defaults to [100, 250, 500, 1000].
Intervals = [1_000, 2_000, 10_000],
Timeout = 60_000,
});
```

By default, `ToPassAsync` retries for 30 seconds. `SetDefaultExpectTimeout` does not change this timeout, use `SetDefaultExpectToPassTimeout` instead:

```csharp
SetDefaultExpectToPassTimeout(60_000);
```

## Soft assertions
* langs: python

Expand Down
Loading