postman使用合集
1.测试下载文件接口,保存即可
2.获取字段,设置环境变量
var addtoken = JSON.parse(responseBody);
pm.environment.set("dwzzb_token",addtoken.data.token);
3.打印环境变量信息
console.log(pm.environment.get("dwzzb_token"));
4.
pm.test("检查data字段中是否包含特定键", function () { const data = pm.response.json().data; #先格式化返回数据为json格式 const expectedKeys = ["dataPersonList", "dataApplyList", "dataDWGKList", "dataSHYKList", "dataZTDRList", "dataStudyList"]; #定义列表 expectedKeys.forEach(function (key) { pm.expect(data).to.have.property(key); }); });
5.
var jsonData = pm.response.json(); pm.test("查询成功", function () { pm.expect(jsonData.msg).to.eql("查询成功") }); pm.test("验证包含partyid", function () { pm.expect(pm.response.text()).to.include(pm.environment.get("dangyuan_partyid")); }); var jd = JSON.parse(responseBody); tests["验证用户名一致"] = jd.data.userName == pm.environment.get("dangyuan_name"); var jd = JSON.parse(responseBody); tests["验证userid一致"] = jd.data.userId == pm.environment.get("dangyuan_userid");
6.判断返回的数据和变量是否一致
pm.test("会议id一致", function () { var jsonData = pm.response.json(); pm.expect(jsonData.rows[0].meetingId).to.eql(pm.environment.get("党总支列表第一个会议id")) });
7.
var currentTime = new Date().toISOString(); pm.request.body.raw = { "address": "地址"+currentTime, "defaultType": 0, "partyType": 3 };
8.循环取数据到列表中
返回的数据:
需要取非自己的id,成为列表,再取列表里的第一个
var jsondata = JSON.parse(responseBody); var userIds = []; jsondata.rows.map(person => { if (person.userId !== pm.environment.get("dangyuan_userid")){ userIds.push(person.userId) } }); pm.environment.set("非自己的第一个userid",userIds[0]);
9.验证次数
//验证谈话次数是否-1 var jsondata = JSON.parse(responseBody); var num = jsondata.data[0].talkedNum var oldnum = pm.environment.get("谈话次数列表第一个人员的谈话次数") //console.log(oldnum) pm.test("新增后谈话次数-1", function () { if (oldnum == 1) pm.expect(num).to.eql(null) else pm.expect(num).to.eql(oldnum-1) }); //console.log(num) pm.environment.set("谈话次数列表第一个人员的谈话次数",num);
10.安装newman
npm install -g newman --registry=https://registry.npm.taobao.org
安装测试报告插件
npm install -g newman-reporter-html
运行脚本生成报告(导出环境变量,导出脚本,用到的文件放一个目录)
如果newman脚本运行 提示certificate has expired(ssl验证),禁用ssl验证
newman run 支部通.postman_collection.json -e 支部通环境变量.postman_environment.json -r html,cli --insecure --reporter-html-export result.html
输出htmlextra报告:
安装:npm install -g newman-reporter-htmlextra
运行:newman run run 支部通.postman_collection.json -e 支部通环境变量.postman_environment.json -r htmlextra,cli --insecure --reporter-html-export result.html
解决htmlextra报告排版错乱:
11.判断空和有数据
var jsonData = pm.response.json(); if(jsonData.rows == "") pm.test("查询成功", function () { pm.expect(pm.response.text()).to.include("查询成功"); }); else pm.test("筛选的类型正确", function () { pm.expect(jsonData.rows[0].type).to.eql(1); });
12.判断返回的数据包含
返回数据为:
{ "total": 14, "rows": [ { "advanceId": 53, "type": 4, "content": "这是修服务", "meetingId": 661, "meetingName": "日", "meetingType": 5, "startTime": "2023-12-07 15:10", "location": "二地址-17楼", "participant": [ { "userId": 12, "userName": "叶", "partyId": null, "partyName": null, "puserId": null }, { "userId": 744, "userName": "二级", "partyId": null, "partyName": null, "puserId": null } ], "rewardContent": null, "editParty": 137 }, { "advanceId": 59, "type": 2, "content": "222", "meetingId": null, "meetingName": null, "meetingType": null, "startTime": "2023-12-20 17:21", "location": "这是修地址", "participant": [ { "userId": 12, "userName": "叶", "partyId": null, "partyName": null, "puserId": null } ], "code": 200, "msg": "查询成功" }
//判断返回的数据 var jsonData = pm.response.json(); var total = jsonData.total; pm.test("返回的数据都包含2023", function () { for(var item of jsonData.rows) console.log(item.startTime) pm.expect(item.startTime).to.include("2023"); });
13.判断返回的数据里面有这条数据,找到该数据(返回也是data[{},{}])
var jsonData = pm.response.json();
const result = jsonData.data.find(item => (item.type === 5 && item.status == 3)); //console.log(result); console.log(result.meetingId); pm.environment.set("id",result.meetingId)
// 假设你有一个返回的JSON数组 const jsonArray = [ { id: 1, name: 'John', age: 25 }, { id: 2, name: 'Jane', age: 30 }, { id: 3, name: 'Tom', age: 35 } ]; // 要查找的值 const targetValue = 'Jane'; // 使用find方法查找目标值 const result = jsonArray.find(item => item.name === targetValue); // 输出结果 console.log(result); // 输出:{ id: 2, name: 'Jane', age: 30 }