postman笔记


postman脚本

常用语法

/* 相关脚本 */

// globals,environment,都适用
// 检测全局变量中某个变量,bool
pm.globals.has("mock_url");
// 获取全局变量中的变量,string
pm.globals.get("mock_url");
// 设置全局变量中的变量
pm.globals.set("mock_url", "Hello world!");
// 销毁某个全局变量
pm.globals.unset("mock_url");
// 清除所有全局变量
pm.globals.clear();
// 以对象形式输出全局变量
pm.globals.toObject();

// request请求
// 获取当前发起请求的url
pm.request.url
// 返回请求中的header信息
pm.request.headers

// sendRequest发送请求
// GET
pm.sendRequest("https://www.baidu.com/", function(err, res) {
    if (err) {
        console.log('Error!');
    } else {
        console.log('Succeed!');
    }
});

// POST
requestString = {
    url: "salon-sit.lite.m.com/common/login",
    method: "POST",
    header: 'Content-Type: application/json;charset=utf-8',
    body: {
        mode: 'raw',
        raw: JSON.stringify({
            account: "Moments", 
            password: "123456" 
        })
    }
};
pm.sendRequest(requestString, function(err, res) {
    if (!err) {
        console.log(res.json());
    }
});

简单实例

pm.test("response is ok", function () {
    pm.response.to.have.status(200);
});

pm.test("接口是否成功", function() {
    $response = pm.response.json();
    pm.expect($response.code).to.eql(1);
});

pm.test("生成mg后台授权", function() {
    $mgTimestamp = (new Date()).getTime().toString().substring(0, 10);
    $clientKey = pm.environment.get("client_key");
    console.log("生成mg后台授权:" + $mgTimestamp + $clientKey);
    $mgSign = CryptoJS.MD5($mgTimestamp + $clientKey, {asString: true}).toString()
    pm.globals.set("mg_timestamp", $mgTimestamp);
    pm.globals.set("mg_sign", $mgSign);
});

if (pm.request.url.path.join("/") == "common/login") {
    pm.test("设置 token_salon", function () {
        $token = pm.response.json().response.token;
        pm.globals.set("token_salon", $token);
    });
}

if (pm.request.url.path.join("/") == "crm/auth/salon/token") {
    pm.test("设置 token_salon", function () {
        $token = pm.response.json().response.token;
        pm.globals.set("token_salon", $token);
    });
}

if (pm.request.url.path.join("/") == "crm/auth/login") {
    pm.test("设置 token_crm", function () {
        $token = pm.response.json().response.token;
        pm.globals.set("token_crm", $token);
    });
}

if (pm.request.url.path.join("/") == "salon/rbac/role/createAndGrant") {
    pm.test("设置 salon_role_id", function () {
        $token = pm.response.json().response.id;
        pm.globals.set("salon_role_id", $token);
    });
}

if (pm.request.url.path.join("/") == "brand/rbac/salon/role/createAndGrant") {
    pm.test("设置 brand_role_id", function () {
        $token = pm.response.json().response.id;
        pm.globals.set("brand_role_id", $token);
    });
}

if (pm.request.url.path.join("/") == "mg/version/add") {
    pm.test("设置 mg_version_id", function () {
        $token = pm.response.json().response.id;
        pm.globals.set("mg_version_id", $token);
    });
}

pm.test("响应时间小于 1000ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(1000);
});

if (pm.request.method == "GET") {
    current_response = pm.response.json().response;
    url = pm.globals.get("mock_url");
    url += "/" + pm.request.url.path.join("/");
    pm.sendRequest(url, function(err, res) {
        if (!err) {
            pm.test("与镜像服务器做数据对比", function() {
                pm.expect(res.json().response).to.eql(current_response);
                // pm.expect(tv4.validate(res.json().response, current_response)).to.eql(true)
            });
        }
    });
}

用户登录与线上数据对比

pm.test("测试状态码:200", function() {
    pm.response.to.have.status(200);
});

pm.test("测试接口调用是否成功", function() {
  $response = pm.response.json();
  pm.expect($response.code).to.eql(1);
});

pm.test("测试接口响应速度不低于2000ms", function() {
    pm.expect(pm.response.responseTime).to.be.below(2000);
});
// 本地
$localJson = pm.response.json();
pm.globals.set("local_token", pm.response.json().response.token);
// 线上
url = "salon-uat.lite.m.com";
url += "/" + pm.request.url.path.join("/");
requestString = {
    url: url,
    method: "POST",
    header: 'Content-Type: application/json;charset=utf-8',
    body: {
        mode: "raw",
        raw: JSON.stringify({
            account: request.data.account,
            password: request.data.password
        })
    }
};
pm.sendRequest(requestString, function(err, res) {
    if (!err) {
        $inlineJson = res.json();
        pm.globals.set("inline_token", res.json().response.token);
        pm.test("测试本地与线上数据对比", function() {
            pm.expect($localJson.response.user).to.eql($inlineJson.response.user);
            pm.expect($localJson.response.brands).to.eql($inlineJson.response.brands);
            pm.expect($localJson.response.salons).to.eql($inlineJson.response.salons);
            pm.expect($localJson.response.staff).to.eql($inlineJson.response.staff);
        });
    }
});

获取用户权限列表与线上对比

// 本地
$localJson = pm.response.json();
// 线上
url = "salon-uat.lite.m.com";
url += "/" + pm.request.url.path.join("/");
url += "?token=" + pm.globals.get("inline_token");
requestString = {
    url: url,
    method: "GET",
};
pm.sendRequest(requestString, function(err, res) {
    if (!err) {
        $inlineJson = res.json();
        console.log("本地", $localJson);
        console.log("线上", $inlineJson);

        pm.test("测试本地与线上数据对比", function() {
            $inlineJson.response.actions = $inlineJson.response.actions.filter(function(element,index,self){
                return self.indexOf(element) === index;
            });
            pm.expect($localJson.response.actions).to.eql($inlineJson.response.actions);
            pm.expect($localJson.response.extends).to.eql($inlineJson.response.extends);
        });
    }
});

手机代码抓包

Capture API requests with Postman

PROXY SETTINGS

将Port设置成5555
将Target设置成History
手机进入代理设置,
IP为Mac电脑的IP,如192.168.131.190
代理端口为,5555

在History查年手机请求信息