//得到URL,问号后面的参数,
var index = pm.request.url.query.all()[0].value;
//JSON.stringify(json); 保存json到变量的方式
var valuesArray = JSON.parse(pm.variables.get('channelValues'));//从变量中读取字符串再转化成Json格式的方式
if(index < 0){
index = 0;
}
else if(index >= valuesArray.length){
index = valuesArray.length-1;
}
var valuesObj = valuesArray[index];
var num=0;
var paramsArray = [];
//对动态参数进行取值,并整理成key,value值对。
for(var key in valuesObj) {
if (valuesObj.hasOwnProperty(key)) {
console.log(key + " = " + valuesObj[key]);
paramsArray.push({"key":key, "value": encodeURIComponent(valuesObj[key])});
num++;
}
}
//移除不需要的参数
pm.request.url.query.remove("index");
//对整理好的动态参数进行追加
pm.request.url.addQueryParams(paramsArray);
//可以通过这种方式,跳过本次不需要测试的集体脚本
pm.variables.set("skipTest","true");
//统一执行的脚本,可以写在Collection里面
var skipTest = pm.variables.get("skipTest");
(skipTest ? pm.test.skip : pm.test)("Status test 200", function () {
pm.response.to.have.status(200);
});
通过脚本发送请求,body格式是Raw时
pm.sendRequest({
url: pm.environment.get("api-url") + 'v1/authenticate',
method: 'POST',
header: {
'content-type': 'application/json',
'x-site-code': pm.environment.get("x-site-code")
},
body: {
mode: 'raw',
raw: JSON.stringify({ email: pm.environment.get("email"), password: pm.environment.get("password") })
}
}, function (err, res) {
pm.environment.set("authorization", "Bearer " + res.json().token);
});
通过脚本发送请求,body格式是
x-www-form-urlencoded时
pm.sendRequest({
url: pm.environment.get("OAUTH_URL")+"/uaa/oauth/token",
method: 'POST',
header: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic Abcdefghijk=='
},
body: {
mode: 'urlencoded',
urlencoded: [
{key: "grant_type", value: "password", disabled: false},
{key: "username", value: pm.environment.get("OAUTH_USERNAME"), disabled: false},
{key: "password", value: pm.environment.get("OAUTH_PASSWORD"), disabled: false}
]
}
}, function (err, res) {
pm.globals.set("token", res.json().access_token);
});
眼镜蛇