Postman接口测试系列:接口参数化和参数的传递

发表于:2017-12-04 10:08

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:janehost    来源:博客园

分享:
  这样就实现了接口的参数化,那么录入用户的接口Pre-Request Script代码如下
//获取全局变量值
sessionId = postman.getGlobalVariable("sessionId");
customerCode = postman.getGlobalVariable("customerCode");
timestamp = postman.getGlobalVariable('timestamp');
ytoken = postman.getGlobalVariable("ytoken");
unicode = postman.getGlobalVariable("unicode");
var pytokenstr = sessionId + customerCode+unicode+timestamp+ytoken;
//postman.setEnvironmentVariable("str",str);
//environment.str = str;
postman.setGlobalVariable("pytokenstr",pytokenstr);
//var md5 = CryptoJS.MD5(str).toString().toLowerCase();
//使用md5加密得到ptoken
//var strmd5 = CryptoJS.MD5(str).toString();
var ptoken = CryptoJS.MD5(pytokenstr);
//environment.strmd5 = strmd5;
postman.setGlobalVariable('ptoken',ptoken);
/**使用者变量信息   */
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;  // 随机整数
const getRandomValue = list => list[randomInt(0, list.length - 1)];  // 随机选项
//随机生成一个字符串作为用户名
postman.setGlobalVariable("compellation", ("0000" +
(Math.random()*Math.pow(36,7) << 0).toString(36)).slice(-7));
//用户昵称,随机2-6字姓名
const charsInName = ['赵', '钱', '孙', '李', '王', '张'];
const numOfChars = randomInt(2, 6);
let randomName = '';
for (let i = 0; i < numOfChars; i++) {
let index = randomInt(0, 5);
randomName += charsInName[index];
}
//environment.nickname = randomName;
postman.setGlobalVariable("nickname",randomName);
/**
//随机生成一个字符串作为昵称
postman.setEnvironmentVariable("nickname", ("0000" +
(Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4));
*/
//用户性别
const divisions = ['O', 'M', 'F'];
//为什么使用environment无法设置变量? 原因environment是引用环境变量的操作
//environment.sex = getRandomValue(divisions);
postman.setGlobalVariable("sex",getRandomValue(divisions));
//postman.setEnvironmentVariable("sex","O");
//用户身份证号码
//postman.setEnvironmentVariable("card","500106200412055816");
postman.setGlobalVariable("card",`50010620001208${randomInt(1000, 9999)}`);
// 随机生日(时间戳)
// 假设今天是2017-1-1,距1970-1-1 47年,则生日范围为 1923-1-1 ~ 2017-1-1
//environment.birthday = randomInt(0 - Date.now(), Date.now());
//postman.setEnvironmentVariable("birthday",Date.now());
/**
//获取系统当前时间,并组装成固定格式时间戳
var d = new Date();
//将日期组装成2位
var timeDate = d.getDate().toString();
if(timeDate.length == 1){
timeDate = "0" + timeDate;
}
//组装整个时间戳
var timesDate = d.getFullYear() + "-"+(d.getMonth()+1).toString() +"-"+ timeDate;
postman.setEnvironmentVariable("birthday", timesDate);
*/
postman.setGlobalVariable("birthday", '2000-12-08');
//身高
//environment.height = '${randomInt(120, 221)}';
//environment.height = randomInt(120, 221);
postman.setGlobalVariable("height",randomInt(120, 221));
//体重
//environment.weight = '${randomInt(40, 150)}';
//environment.height = randomInt(40, 150);
postman.setGlobalVariable("weight",randomInt(40, 150));
//remark
//随机生成一个字符串作为remark
postman.setGlobalVariable("remark", ("test" +
(Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4));
// 随机手机
//environment.phone = `18${randomInt(100000000, 999999999)}`;
postman.setGlobalVariable("phone",`18${randomInt(100000000, 999999999)}`);
console.log(pytokenstr);
  部分代码参考 https://testerhome.com/topics/6641
  然后在potman中body中设置如图所示
  之后在Tests中增加断言信息,代码如下
// 推荐用全等 ===,确保类型和值都一致
//tests['Status code is 200'] = responseCode.code === 200);
// 判断是否存在 'success' 值
if(responseCode.code===200 & responseBody !== null & responseBody.has("code") ){
tests["Body matches code"] = responseBody.has("0");
var jsonData = JSON.parse(responseBody);
postman.setGlobalVariable("userId",jsonData.result.user_id);
tests[`[INFO] Request params: ${JSON.stringify(request.data)}`] = true;
//可以考虑判断user_id,后续升级考虑
tests["have result "]=jsonData.hasOwnProperty("result")===true;
tests[`[INFO] Response timeout: ${responseTime}`] = responseTime < 6000;
}
else{
//接口请求失败
tests["Waring:Request Failed. Please Fix!"] = false;
}
  在上面这段代码中,使用var jsonData = JSON.parse(responseBody); postman.setGlobalVariable("userId",jsonData.result.user_id);再次获取了该接口的响应结果中的user_id的值作为下一个接口的参数,而该接口的响应结果代码如图
{
"result": {
"birthday": "2000-12-08",
"phone": "18271263485",
"sex": "O",
"register_time": "2017-11-30 10:49:43",
"weight": "109",
"height": "184",
"remark": "fdn1",
"organization_id": "1149",
"compellation": "-pm6eil",
"card": "500106200012081705",
"user_id": 1638
},
"code": "0"
}
  通过这两处不同的接口响应结果的取值,对于这种构造类型的响应结果的取值可以轻松应对。以后如果在遇到其他接口的响应结果取值,再慢慢积累吧......
相关推荐:
Postman接口测试系列:时间戳和加密
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号