微软出品自动化神器【Playwright+Java】系列(八) 之 使用 Playwright进行API接口测试
2023-04-18 12:38:02 时间
前言
我喜欢周末是因为,可以做一些我自己喜欢的事。
比如我插上耳机,写点东西就能坐上一天,这也许算是属于我自己的一份静谧
吧。
想系统学习请参考: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测试简单,而且好上手,难道是我错觉吗?有兴趣的同学可以自行尝试!
相关文章
- Python中关于进度条的6个实用技巧
- 新一代Python包管理工具来了
- jupyter中那些神奇的第三方拓展魔术命令
- 99%的Python用户都不知道的f-string隐秘技巧
- (数据科学学习手札107)在Python中利用funct实现链式风格编程
- (数据科学学习手札91)在Python中妥善使用进度条
- 必知必会的8个Python列表技巧
- 实用的Python(3)超简单!基于Python搭建个人“云盘”
- 怎么更改jupyter的端口错误?
- 怎么更改jupyter的home文件路径?
- pycharm不能输入怎么解决?
- 怎么运行anaconda的spyder?
- aix vim中文乱码怎么解决?
- apt-get install vim失败如何解决?
- cat显示正常vim显示中文乱码解决方法
- centos7中vi与vim的区别
- centos7怎么查看vim?
- adt如何加进eclipse?
- adt中的eclipse打不开解决方法
- 怎样在python里安装jupyter?