postman是一款很好用的接口测试工具,最近才使用到测试工作中,真是相见恨晚。作为刚接触的人,对工具的使用及测试中出现的问题做一下使用记录。
参考连接:https://segmentfault.com/a/1190000012077563
GET请求
常用的头信息Content-Type、Host
POST请求
form-data----->multipart/form-data
x-www-form-urlencoded---->application/x-www-form-urlencoded
raw---->application/json #发送请求时会自动添加Content-Type: 'application/json'请求头
postman解析变量是由大范围都小范围解析的优先级从高到低:Data ,Local ,Enviroment ,Global
设置全局变量
运行时需要制定对应的环境,注意字符串类型的变量值要带双引号或单引号
批量参数化
1,编写文本,第一行写下参数名,第二行开始,逐行写下参数各值;多参数则多行,空格或tab符隔开;编码格式设置为UTF-8
2.在Pre-request Script中,获取参数值,并传到搜索接口
var keyword = data.keyword;//data是文件名,keyword是文件中的参数
3.collection runner中设置
Iterations选项:迭代次数,若大于文本中的值个数,则循环获取
Data选项:上传data.txt文件
Data File Type选项:选择text/csv,该选项添加data后才会出现,可以点Preview按钮查看数据;
通过脚本(scripts)在pre-request(测试前脚本)和test script(测试后脚本)中,JavaScript脚本;
postman.setEnvironmentVariable("argname",value)
或postman.setGlobalsVariable("argname",value)
若是字符串参数需要带双引号怎么参数化,如post请求body部分使用变量?
json中的引号问题,处理方法是内容-》转义-》json;做法是再添加一个变量对参数进行转义
答:若设置全局变量,字符串类型的变量值时要带双引号或单引号;
若在Pre-request Script中设置局部变量var videotype1 = "\"hls\"";
参数使用
变量在postman 中使用是用下面方式:{{variableName}}
可以在请求builder的任何地方添加文本,包括URL,URL parameters,headers,authorization, request body ,header presets可以使用
在Tests中使用则需要获取预定义变量postman.getEnvironmentVariable("argname")或postman.getGlobalsVariable("argname")
另一种写法pm.environment.get("variable_key");或者pm.globals.get("variable_key");#pm要定义
调试
打印变量(Logging variables)
console.log(argname)
显示Postman Console界面
作用:显示请求及响应相关内容
显示:View-》show postman console
多请求间的关联
1,一个请求的输出做另一个请求的输入
在第一个请求的Testst设置变量
//获取结果
var data1 = JSON.parse(responseBody);
//把结果设置成环境变量,下个接口需要
postman.setEnvironmentVariable("retKibana",data1.hits.hits)
或者这种写法:pm.environment.set('resKibana', data["hits"]["hits"]);
在第二个请求的需要参数处直接{{variableName}}使用变量;
2,2个请求结果做比较
在第一个请求的Testst设置变量
//获取结果
var data1 = JSON.parse(responseBody);
//把结果设置成环境变量,下个接口需要
postman.setEnvironmentVariable("retKibana",data1.hits.hits)
或者这种写法:pm.environment.set('resKibana', data["hits"]["hits"]);
在第二个请求Tests处直接postman.getEnvironmentVariable("argname")获取变量值;
记住是JavaScript脚本哦
1.检查的body中是否包含字符串
tests["测试点"] = responseBody.has("需要查找的字符串");
2.检查str是否等于字符串,完全匹配才行
tests["测试点"] = responseBody === "Response Body返回的内容";
3,设置检查点的状态
tests["Kibana format right"] = true;
获取时间
var date = new Date();
var day = date.getFullYear()+""+date.getMonth()+""+(date.getDate()-1);#一定要带“”,否则输出非期望值
数组操作
//设置和修改值都可以使用该方法进行设定
dic["q"] = "q1";
// 字典中项的遍历
//不能 for (var i = 0; i < dic.length; i++) 因为dic没有length属性为0
for (var key in dic) {
console.log(key + ":" + dic[key]);
}
//下面两种方式都可以进行删除
delete dic["r"];
delete dic.w;
字典操作
var dictionary = new Dictionary();
dictionary.set('Gandalf', '[email protected]');
dictionary.has('Gandalf'); //true
dictionary.size(); //3
dictionary.keys(); //["Gandalf", "John", "Tyrion"]
dictionary.values(); //["[email protected]", "[email protected]", "[email protected]"]
dictionary.get('Tyrion'); //outputs [email protected]
dictionary.delete('John');
dictionary.delete('John');
建议
1,最好数据转成字典格式处理
数据如下,转换成字典dic:
{"result":{"dataValue":[{"prog":"100209800","value":2},{"prog":"100209801","value":13},{"prog":"100209802","value":10},{"prog":"100303200","value":2},{"prog":"100303201","value":4},{"prog":"100303202","value":1},{"prog":"103049500","value":17},{"prog":"103049501","value":110},{"prog":"103049502","value":49},{"prog":"103079401","value":4},{"prog":"103079402","value":3},{"prog":"103079500","value":2},{"prog":"103079501","value":10},{"prog":"103079502","value":2},{"prog":"103079600","value":2},{"prog":"103079601","value":5},{"prog":"103141702","value":2},{"prog":"103180601","value":3},{"prog":"103180602","value":2},{"prog":"121201102","value":5},{"prog":"121201502","value":4},{"prog":"121201702","value":0},{"prog":"121201802","value":0},{"prog":"124208300","value":87},{"prog":"124208301","value":398},{"prog":"124208302","value":45},{"prog":"124208303","value":99},{"prog":"124211000","value":24},{"prog":"124211001","value":27},{"prog":"124211002","value":133},{"prog":"124211003","value":21},{"prog":"124235502","value":1},{"prog":"124239600","value":3},{"prog":"124239601","value":17},{"prog":"124239602","value":4},{"prog":"124239603","value":0},{"prog":"124239902","value":2},{"prog":"124242802","value":3},{"prog":"124242803","value":2},{"prog":"124245301","value":0},{"prog":"124246001","value":0},{"prog":"126603600","value":2},{"prog":"126603601","value":4}],"retcode":43}}
var dic = {};
for(var i =0;i
if(!(prog in dic)){
dic[prog] = 0;
}
dic[prog] += 1;
}