postman接口测试系列:接口参数化和参数的传递 (2)

这样就实现了接口的参数化,那么录入用户的接口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);

然后在potman中body中设置如图所示

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

之后在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; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zygydz.html