微软出品自动化神器【Playwright+Java】系列(八) 之 使用 Playwright进行API接口测试
前言
我喜欢周末是因为,可以做一些我自己喜欢的事。
比如我插上耳机,写点东西就能坐上一天,这也许算是属于我自己的一份静谧
吧。
想系统学习请参考:Playwright+Java入门
使用Playwright进行API测试
1、总体感受
和其他API的依赖比起来,感觉使用起来不是很舒服,而且感觉繁琐呢,肯定是我研究的不够深入,不熟引起的。
2、初始化配置
这一部分相当于httpclient
的初始化客户端操作,示例代码如下:
@BeforeClass
public void beforeClass() {
playwright = Playwright.create();
request = playwright.request().newContext(new APIRequest.NewContextOptions()
.setBaseURL("http://localhost:8090"));
}
销毁操作,示例代码如下:
@AfterClass
public void afterClass() {
if (request != null) {
request.dispose();
request = null;
}
if (playwright != null) {
playwright.close();
playwright = null;
}
}
3、编写API测试
效果如下:
4、完整代码
这里我仅用查询(GET
)和新增接口(POST
)进行演示,完整示例代码如下:
package com.playwight.test;
import com.microsoft.playwright.APIRequest;
import com.microsoft.playwright.APIRequestContext;
import com.microsoft.playwright.APIResponse;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.options.RequestOptions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.Map;
import static org.testng.Assert.assertTrue;
public class TestGitHubAPI {
private Playwright playwright;
private APIRequestContext request;
@BeforeClass
public void beforeClass() {
playwright = Playwright.create();
}
/**
* get请求
*/
@Test
public void testGetAPi() {
request = playwright.request().newContext(new APIRequest.NewContextOptions()
.setBaseURL("http://localhost:8090"));
APIResponse getAPIResponse = request.get("/students");
assertTrue(getAPIResponse.ok());
System.out.println(getAPIResponse.text());
}
/**
* post请求
*/
@Test
public void testPostApi() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
request = playwright.request().newContext(new APIRequest.NewContextOptions()
.setBaseURL("http://localhost:8090")
.setExtraHTTPHeaders(headers));
Map<String, String> data = new HashMap<>();
data.put("className", "className");
data.put("courseName", "english");
data.put("email", "xiaoqiang@qq.com");
data.put("name", "xiaoqiang");
data.put("score", "90");
data.put("sex", "boy");
data.put("studentId", "00099");
APIResponse postAPIResponse = request.post("/studentAdd",
RequestOptions.create().setData(data));
assertTrue(postAPIResponse.ok());
System.out.println(postAPIResponse.text());
}
@AfterClass
public void afterClass() {
if (request != null) {
request.dispose();
request = null;
}
if (playwright != null) {
playwright.close();
playwright = null;
}
}
}
写在最后
感觉还是写API测试简单,而且好上手,难道是我错觉吗?有兴趣的同学可以自行尝试!
优秀不够,你是否无可替代
软件测试交流QQ群:721256703,期待你的加入!!
欢迎关注我的微信公众号:软件测试君

分类:
Playwright+Java
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库