|
| 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. |
0 commit comments