在使用postman进行接口测试的时候,对于有些接口字段须要时间戳加密,这个时候咱们就遇到2个问题,其一是接口中的时间戳如何获得?其二就是对于如今经常使用的md5加密操做如何在postman中使用代码实现呢?
下面咱们以一个具体的接口例子来进行说明。
首先来看看咱们的接口文档信息,如图所示
此接口文档中,须要三个参数customercode、timestamp和itoken(是customerCode+timestamp+ytoken加密后的结果)。
第一次操做的时候,咱们使用postman会这样操做,如图
这样操做流程是:json
说明安全
x-www-form-urlencoded便是application/x-www-from-urlencoded,将表单内的数字转换为键对值session
postman中 form-data、x-www-form-urlencoded、raw、binary的区别:app
时间戳转换工具:post
md5加密工具:加密
这样建立会话的接口咱们就完成了!可是为了系统的安全性,这里的timestamp是每30分钟就会过时的,下次咱们又须要从新设置timestamp,就是md5加密的结果......这样操做岂不是太麻烦?.net
还好postman中Pre-Request Script能够在 Request 以前自定义请求数据,这样作的好处就是能够以嵌入脚本的方式动态准备测试数据,并根据业务需求设计测试用例。
这里咱们仍继续以上面的用例为例:
在postman中,如何才能获取当前机器上的timestamp呢?
Math.round(new Date().getTime())
能够知足咱们的要求!!!
那代码如何实现呢?
//设置当前时间戳毫秒 postman.setGlobalVariable("timestamp",Math.round(new Date().getTime()));
这样就将获取的时间戳设置为全局变量timestamp
咱们知道itoken的值是md5(customerCode+timestamp+ytoken')
那么接下来就能够动态的获取md5的信息了,代码以下:
//发起请求以前获取当前的时间戳放在参数里 //postman.setGlobalVariable("customerCode","***2345677***"); //1.设置环境变量 postman.setEnvironmentVariable("key", "value"); //2.设置全局变量 postman.setGlobalVariable("key", "value"); //environment.customerCode = "***2345677***"; customerCode = postman.getGlobalVariable("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.getGlobalVariable("customerCode"); timestamp = postman.getGlobalVariable('timestamp'); ytoken = postman.getGlobalVariable("ytoken"); var str = customerCode+timestamp+ytoken; //postman.setEnvironmentVariable("str",str); //environment.str = str; postman.setGlobalVariable("str",str); //var md5 = CryptoJS.MD5(str).toString().toLowerCase(); //使用md5加密 //var strmd5 = CryptoJS.MD5(str).toString(); var strmd5 = CryptoJS.MD5(str); //environment.strmd5 = strmd5; postman.setGlobalVariable('md5',strmd5); //environment.md5 = md5; //timecode=System.currentTimeMillis(); console.log(str);
而在接口请求中,就可使用已经定义好的变量来进行接口操做,代码以下
customerCode:{{customerCode}} timestamp:{{timestamp}} ltoken:{{md5}}
如图所示
这样下次建立接口的时候,直接运行该用例便可,不用再次修改参数值~(≧▽≦)/~
那么咱们如何才能知道该接口用例是成功的呢,该怎么断言呢?
这里列出我该接口断言的一个示例,代码以下
/* // 推荐用全等 ===,确保类型和值都一致 tests['Status code is 200'] = responseCode.code === 200; // 判断是否存在 'success' 值 tests["Body matches code"] = responseBody.has("0"); var jsonData = JSON.parse(responseBody); postman.setEnvironmentVariable("sessionId",jsonData.result); tests[`[INFO] Request params: ${JSON.stringify(request.data)}`] = true; tests["have result "]=jsonData.hasOwnProperty("error")!==true; tests[`[INFO] Response timeout: ${responseTime}`] = responseTime < 6000; **/ //状态代码是200 if(responseCode.code === 200){ // 判断是否存在 'success' 值,检查响应体包含一个字符串 tests["Body matches code"] = responseBody.has("0"); //响应结果中result保存为全局变量sessonId var jsonData = JSON.parse(responseBody); postman.setGlobalVariable("sessionId",jsonData.result); //输入接口参数信息 tests[`[INFO] Request params: ${JSON.stringify(request.data)}`] = true; // tests["have result "]=jsonData.hasOwnProperty("error")!==true; //判断接口响应结果有result tests["have result "]=jsonData.hasOwnProperty("result")===true; //判断接口响应时间小于N秒 tests[`[INFO] Response timeout: ${responseTime}`] = responseTime < 6000; }else{ //接口请求失败 tests["Waring:Request Failed. Please Fix!"] = false; }
这样建立会话的接口就完成了!