本文为博主原创,未经许可严禁转载。
本文链接:https://blog.csdn.net/zyooooxie/article/details/113250778
之前分享过Postman、JMeter的 数据关联、 参数化,这次来分享下 Postman的前置、后置脚本;
个人博客:https://blog.csdn.net/zyooooxie
前提
在用Postman写脚本之前,个人建议:
- 看看JavaScript的基础语法
- 看看Postman的官方文档: 重点:sending-requests-from-scripts 、 Test script examples
get请求:无参数
//写法1
var url_1 = "httpbin.org/get";
console.log(url_1);
const send_req = {
url: url_1,
method:"GET",
header:{
"zy":"sz"}
};
pm.sendRequest(send_req,function(err, res){
console.log(res.json());
});
//写法2
console.log(url_1+"!!!!!!another");
pm.sendRequest(url_1, function(err,res){
console.log(res.json());
});
get请求:有参数
var url_1 = "httpbin.org/get?"+encodeURIComponent("$##$¥手=动xie支持");
console.log(url_1);
const send_req = {
url: url_1,
method:"GET",
header:{
"zy":"sz"}
};
pm.sendRequest(send_req,function(err, res){
console.log(res.json());
});
var url_2 = "httpbin.org/get?zy=csdn";
console.log(url_2);
const send_req_2 = {
url : url_2,
method:"get"
};
pm.sendRequest(send_req_2, function(err, res){
console.log(res.json());
});
var url_3 = "httpbin.org/get?zy=csdn&"+"some1='TTTEST'&"+"some_test_key="+encodeURIComponent(pm.variables.get("some_test_key")) ;
pm.sendRequest(url_3,function(err,res){
console.log(res.json());
});
post请求:x-www-form-urlencoded
var url_1 = "httpbin.org/post";
console.log(url_1);
const send_req = {
url: url_1,
method:"post",
header:"content-type:application/x-www-form-urlencoded; charset=UTF-8",
body:{
mode: 'raw',
raw:"shi=sd&s¥=sd的sd&123=sd大山东&some_test=('some')&"+"some_test_key="+pm.variables.get("some_test_key")
}
};
pm.sendRequest(send_req,function(err, res){
console.log(res.json());
});
post请求:application/json
var url_1 = "httpbin.org/post";
console.log(url_1);
const send_req = {
url: url_1,
method:"post",
header:"content-type:application/json; charset=UTF-8",
body:{
mode:"raw",
raw:JSON.stringify(
{
"zy":123,"sd&%¥#¥%123d的十大":"shiimasd","some_test_key":pm.variables.get("some_test_key")})}
// pm.variables.get("variable_key");
};
pm.sendRequest(send_req,function(err, res){
console.log(res.json());
});
实战
1.使用的接口还是老一套: 之前分享过 下面所用接口的详情;
2.下图所示 访问的是image接口,前置是key接口;后置是login接口+其他访问接口;
the Pre-request Script tab
var key_url = pm.variables.get("ip") +":" + pm.variables.get("port") + "/api/auth/get/key";
const send_key_req = {
url:key_url,
header:"Content-Type:charset=UTF-8"
};
pm.sendRequest(send_key_req, function(err, res){
var rj = res.json();
console.log(rj.data);
pm.environment.set("key_value", rj.data);
}
);
the Tests tab
pm.test("Your test name", function() {
console.log(responseHeaders);
pm.expect(responseHeaders["Content-Type"]).to.eql("image/jpeg");
});
var join = "phone="+pm.variables.get("phone")+"&password="+pm.variables.get("pwd")+"&code=1&key="+pm.variables.get("key_value");
var url_login = pm.variables.get("ip") + ":" + pm.variables.get("port") + "/api/auth/login"+"?"+join;
const send_login = {
url: url_login,
method: "get"
};
pm.sendRequest(send_login, function(err, res) {
pm.test("Your test name:login", function () {
var jsonData = res.json();
pm.expect(jsonData.status).to.eql("1");
});
});
var url_test= pm.variables.get("ip") +":" + pm.variables.get("port") + "/api/balance/account/bank_gateway/list";
const req_test = {
url:url_test,
Referer:"http://"+pm.variables.get("ip") +":" + pm.variables.get("port")+"/welcome"
}
pm.sendRequest(req_test,function(err,res){
pm.test("Your test name:test", function () {
var jsonData = res.json();
pm.expect(jsonData.msg).to.eql("success");
});
});
交流技术 欢迎+QQ 153132336 zy
个人博客 https://blog.csdn.net/zyooooxie