APIRequestContext
此 API 用于 Web API 测试。您可以使用它触发 API 端点、配置微服务、准备环境或服务以进行端到端测试。
每个 Playwright 浏览器上下文都有一个与之关联的 APIRequestContext 实例,该实例与浏览器上下文共享 cookie 存储,可通过 BrowserContext.request() 或 Page.request() 访问。也可以通过调用 APIRequest.newContext() 手动创建一个新的 APIRequestContext 实例。
Cookie 管理
BrowserContext.request() 和 Page.request() 返回的 APIRequestContext 与相应的 BrowserContext 共享 cookie 存储。每个 API 请求的 Cookie
标头都将使用浏览器上下文中的值填充。如果 API 响应包含 Set-Cookie
标头,它将自动更新 BrowserContext cookie,并且页面发出的请求将获取它们。这意味着如果您使用此 API 登录,您的端到端测试将处于登录状态,反之亦然。
如果您希望 API 请求不干扰浏览器 cookie,则应通过调用 APIRequest.newContext() 创建一个新的 APIRequestContext。此类 APIRequestContext
对象将拥有自己独立的 cookie 存储。
方法
delete
添加于:v1.16发送 HTTP(S) DELETE 请求并返回其响应。该方法将从上下文填充请求 cookie 并从响应更新上下文 cookie。该方法将自动遵循重定向。
用法
APIRequestContext.delete(url);
APIRequestContext.delete(url, options);
参数
-
目标 URL。
-
options
RequestOptions (可选)新增于: v1.18#可选请求参数。
返回
dispose
添加于:v1.16APIRequestContext.get() 和类似方法返回的所有响应都存储在内存中,以便您以后可以调用 APIResponse.body()。此方法会丢弃其所有资源,对已处理的 APIRequestContext 调用任何方法都将抛出异常。
用法
APIRequestContext.dispose();
APIRequestContext.dispose(options);
参数
返回
fetch
添加于:v1.16发送 HTTP(S) 请求并返回其响应。该方法将从上下文中填充请求 cookie,并从响应中更新上下文 cookie。该方法将自动遵循重定向。
用法
JSON 对象可以直接传递给请求。
Map<String, Object> data = new HashMap();
data.put("title", "Book Title");
data.put("body", "John Doe");
request.fetch("https://example.com/api/createBook", RequestOptions.create().setMethod("post").setData(data));
在请求主体中发送文件(s)的常用方法是将它们作为表单字段与 multipart/form-data
编码一起上传,通过指定 multipart
参数。
// Pass file path to the form data constructor:
Path file = Paths.get("team.csv");
APIResponse response = request.fetch("https://example.com/api/uploadTeamList",
RequestOptions.create().setMethod("post").setMultipart(
FormData.create().set("fileField", file)));
// Or you can pass the file content directly as FilePayload object:
FilePayload filePayload = new FilePayload("f.js", "text/javascript",
"console.log(2022);".getBytes(StandardCharsets.UTF_8));
APIResponse response = request.fetch("https://example.com/api/uploadScript",
RequestOptions.create().setMethod("post").setMultipart(
FormData.create().set("fileField", filePayload)));
参数
-
urlOrRequest
String | Request#目标 URL 或从中获取所有参数的请求。
-
options
RequestOptions (可选)新增于: v1.18#可选请求参数。
返回
get
添加于:v1.16发送 HTTP(S) GET 请求并返回其响应。该方法将从上下文填充请求 cookie 并从响应更新上下文 cookie。该方法将自动遵循重定向。
用法
请求参数可以通过 params
选项配置,它们将被序列化为 URL 搜索参数。
request.get("https://example.com/api/getText", RequestOptions.create()
.setQueryParam("isbn", "1234")
.setQueryParam("page", 23));
参数
-
目标 URL。
-
options
RequestOptions (可选)新增于: v1.18#可选请求参数。
返回
head
添加于:v1.16发送 HTTP(S) HEAD 请求并返回其响应。该方法将从上下文填充请求 cookie 并从响应更新上下文 cookie。该方法将自动遵循重定向。
用法
APIRequestContext.head(url);
APIRequestContext.head(url, options);
参数
-
目标 URL。
-
options
RequestOptions (可选)新增于: v1.18#可选请求参数。
返回
patch
添加于:v1.16发送 HTTP(S) PATCH 请求并返回其响应。该方法将从上下文填充请求 cookie 并从响应更新上下文 cookie。该方法将自动遵循重定向。
用法
APIRequestContext.patch(url);
APIRequestContext.patch(url, options);
参数
-
目标 URL。
-
options
RequestOptions (可选)新增于: v1.18#可选请求参数。
返回
post
添加于:v1.16发送 HTTP(S) POST 请求并返回其响应。该方法将从上下文填充请求 cookie 并从响应更新上下文 cookie。该方法将自动遵循重定向。
用法
JSON 对象可以直接传递给请求。
Map<String, Object> data = new HashMap();
data.put("title", "Book Title");
data.put("body", "John Doe");
request.post("https://example.com/api/createBook", RequestOptions.create().setData(data));
要向服务器发送表单数据,请使用 form
选项。其值将以 application/x-www-form-urlencoded
编码编码到请求主体中(有关如何使用 multipart/form-data
表单编码发送文件,请参见下文)。
request.post("https://example.com/api/findBook", RequestOptions.create().setForm(
FormData.create().set("title", "Book Title").set("body", "John Doe")
));
在请求正文中发送文件的一种常见方式是将其作为具有 multipart/form-data
编码的表单字段上传。使用 FormData 构造请求正文并将其作为 multipart
参数传递给请求
// Pass file path to the form data constructor:
Path file = Paths.get("team.csv");
APIResponse response = request.post("https://example.com/api/uploadTeamList",
RequestOptions.create().setMultipart(
FormData.create().set("fileField", file)));
// Or you can pass the file content directly as FilePayload object:
FilePayload filePayload1 = new FilePayload("f1.js", "text/javascript",
"console.log(2022);".getBytes(StandardCharsets.UTF_8));
APIResponse response = request.post("https://example.com/api/uploadScript",
RequestOptions.create().setMultipart(
FormData.create().set("fileField", filePayload)));
参数
-
目标 URL。
-
options
RequestOptions (可选)新增于: v1.18#可选请求参数。
返回
put
添加于:v1.16发送 HTTP(S) PUT 请求并返回其响应。该方法将从上下文填充请求 cookie 并从响应更新上下文 cookie。该方法将自动遵循重定向。
用法
APIRequestContext.put(url);
APIRequestContext.put(url, options);
参数
-
目标 URL。
-
options
RequestOptions (可选)新增于: v1.18#可选请求参数。
返回
storageState
添加于:v1.16返回此请求上下文的存储状态,包含当前 cookie 和如果传递给构造函数的本地存储快照。
用法
APIRequestContext.storageState();
APIRequestContext.storageState(options);
参数
options
ApiRequestContext.StorageStateOptions
(可选)
返回