一、时间戳和MD5加密
//发起请求之前获取当前的时间戳放在参数里
//postman.setGlobalVariable("customerCode","***2345677***");
//1.设置环境变量 postman.setEnvironmentVariable("key", "value");
//2.设置全局变量 postman.setGlobalVariable("key", "value");
//environment.customerCode = "***2345677***";
customerCode = postman.getEnvironmentVariable("customerCode");
//设置当前时间戳毫秒
postman.setGlobalVariable("timestamp",Math.round(new Date().getTime()));
//environment.timestamp = Math.round(new Date().getTime());
//postman.setEnvironmentVariable("unixtime_now","timecode");
//var jsonData = JSON.parse(request.data.applyJsonStr);
//postman.setGlobalVariable("ytoken","*********b176a4739bfccb*********");
//获取全局变量
//如postman.getGlobalVariable("key");
customerCode = postman.getEnvironmentVariable("customerCode");
timestamp = postman.getGlobalVariable('timestamp');
ytoken = postman.getEnvironmentVariable("yytoken");
var stry = customerCode+timestamp+ytoken;
//postman.setEnvironmentVariable("str",str);
//environment.str = str;
postman.setGlobalVariable("stry",stry);
//var md5 = CryptoJS.MD5(str).toString().toLowerCase();
//使用md5加密
//var strmd5 = CryptoJS.MD5(str).toString();
var strmd5y = CryptoJS.MD5(stry);
//environment.strmd5 = strmd5;
postman.setGlobalVariable('md5y',strmd5y);
//environment.md5 = md5;
//timecode=System.currentTimeMillis();
console.log(stry);
二、获取session并保存为变量
if(responseCode.code === 200){
// 判断是否存在 'success' 值
tests["Body matches code"] = responseBody.has("0");
//获取响应结果result,作为下一个接口的入参
var jsonData = JSON.parse(responseBody);
postman.setGlobalVariable("sessionIdy",jsonData.result);
tests[`[INFO] Request params: ${JSON.stringify(request.data)}`] = true;
// tests["have result "]=jsonData.hasOwnProperty("error")!==true;
tests["have result "]=jsonData.hasOwnProperty("result")===true;
tests[`[INFO] Response timeout: ${responseTime}`] = responseTime < 6000;
}else{
//接口请求失败
tests["Waring:Request Failed. Please Fix!"] = false;
}
三、数据随机生成
/**使用者变量信息 */
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",`50010620001108${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);
网友评论