跳到正文
本页尚未翻译成中文,以下为英文原文。 查看英文版

FunctionAssertions ​

Added in: v1.66

The FunctionAssertions class provides assertion methods that retry a block of code. A new instance of FunctionAssertions is created by calling PlaywrightAssertions.Expect():

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();
    }
}

Methods ​

FunctionAssertions.ToPassAsync() ​

Added in: 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,
});

Arguments

  • options Object (optional)

    • intervals Array<int> (optional)

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

    • timeout float (optional)

      Time to retry the callback for in milliseconds. Defaults to 30000, change the default with SetDefaultExpectToPassTimeout. Pass 0 to disable the timeout.

基于 Apache 2.0 许可证发布。