Skip to main content

APIResponseAssertions

APIResponseAssertions 类提供了可用于在测试中对 APIResponse 进行断言的方法。

🌐 The APIResponseAssertions class provides assertion methods that can be used to make assertions about the APIResponse in the tests.

using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;

namespace PlaywrightTests;

[TestClass]
public class ExampleTests : PageTest
{
[TestMethod]
public async Task NavigatesToLoginPage()
{
var response = await Page.APIRequest.GetAsync("https://playwright.nodejs.cn");
await Expect(response).ToBeOKAsync();
}
}

方法

🌐 Methods

ToBeOKAsync

Added in: v1.18 apiResponseAssertions.ToBeOKAsync

确保响应状态码在 200..299 范围内。

🌐 Ensures the response status code is within 200..299 range.

用法

await Expect(response).ToBeOKAsync();

返回


属性

🌐 Properties

Not

Added in: v1.20 apiResponseAssertions.Not

使断言检查相反的条件。

🌐 Makes the assertion check for the opposite condition.

用法

例如,这段代码测试响应状态是否不成功:

🌐 For example, this code tests that the response status is not successful:

await Expect(response).Not.ToBeOKAsync();

类型