playwright支持进行API测试
示例:
如下代码创建API请求上下文,使用post方法请求API,并返回响应结果。
import playwright
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
api_request_context=p.request.new_context()
headers={"Content-Type":"application/json"}
query_params = {"username":"tester","password":"tester"}
response=api_request_context.post("https://api.pity.fun/auth/login", data=query_params,headers=headers)
print(response.body())
print(response)
APIRequestContext | Playwright Python
playwright.request 方法返回APIRequest类型
此 API 用于 Web API 测试。您可以使用它来触发 API 端点、配置微服务、准备环境或服务以进行 e2e 测试。
每个 Playwright 浏览器上下文都与其关联 APIRequestContext 实例,该实例与浏览器上下文共享 cookie 存储,可以通过 browser_context.request 或 page.request 访问。也可以通过调用api_request.new_context()手动创建新的APIRequestContext实例。
browser_context.request 返回的 APIRequestContext 和 page.request 与相应的 BrowserContext 共享 cookie 存储。每个 API 请求的标头都将填充浏览器上下文中的值。如果 API 响应包含标头,它将自动更新浏览器上下文 cookie,并且从页面发出的请求将选取它们。这意味着,如果您使用此 API 登录,您的 e2e 测试将被登录,反之亦然。Cookie
Set-Cookie
如果您希望 API 请求不会干扰浏览器 cookie,则应通过调用 api_request.new_context() 创建新的 APIRequestContext。此类对象将具有自己的独立 cookie 存储。
query_params = {
"isbn": "1234",
"page": "23"
}
api_request_context.get("https://example.com/api/getText", params=query_params)
body为json类型
data = {
"title": "Book Title",
"body": "John Doe",
}
api_request_context.post("https://example.com/api/createBook", data=data)
body为form
application/x-www-form-urlencoded
multipart/form-data类型
formData = {
"title": "Book Title",
"body": "John Doe",
}
api_request_context.post("https://example.com/api/findBook", form=formData)
body为multipart/form-data
api_request_context.post(
"https://example.com/api/uploadScrip'",
multipart={
"fileField": {
"name": "f.js",
"mimeType": "text/javascript",
"buffer": b"console.log(2022);",
},
})
api_request_context.delete(url)
api_request_context.delete(url, **kwargs)
api_request_context.dispose()
data = {
"title": "Book Title",
"body": "John Doe",
}
api_request_context.fetch("https://example.com/api/createBook", method="post", data=data)
请求数据类型为multipart/form-data
api_request_context.fetch(
"https://example.com/api/uploadScrip'",
method="post",
multipart={
"fileField": {
"name": "f.js",
"mimeType": "text/javascript",
"buffer": b"console.log(2022);",
},
})
api_request_context.head(url)
api_request_context.head(url, **kwargs)
api_request_context.patch(url)
api_request_context.patch(url, **kwargs)
api_request_context.put(url)
api_request_context.put(url, **kwargs)
返回请求上下文的存储状态,包含当前的cookie和本地存储快照。
api_request_context.storage_state()
api_request_context.storage_state(**kwargs)
APIRequestContext请求对应方法,可以得到APIResponse,根据APIResponse获得响应结果内容。
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
context = playwright.request.new_context()
response = context.get("https://example.com/user/repos")
assert response.ok
assert response.status == 200
assert response.headers["content-type"] == "application/json; charset=utf-8"
assert response.json()["name"] == "foobar"
assert response.body() == '{"status": "ok"}'
返回响应体
api_response.body()
释放此响应的正文。如果未调用,则正文将保留在内存中,直到上下文关闭。
api_response.dispose()
返回响应结果json
api_response.json()
返回响应体的text文本
api_response.text()
返回响应头
api_response.headers
api_response.headers_array
返回响应是否成功布尔值
api_response.ok
返回响应状态码
api_response.status
返回响应结果文本值,通常是ok
api_response.status_text
返回响应的URL
api_response.url