Postman的一些进阶用法

1. 获取接口请求响应状态码

语法:

responseCode.code 

例子,判断接口请求的响应状态是否为200:

tests["code == 200 ?"] = responseCode.code === 200;

2. 获取接口请求响应时间

语法:

responseTime 

例子,判断接口请求的响应时间是否小于200ms:

tests["Response time is less than 200ms"] = responseTime < 200;

3. 获取接口请求响应文本

语法:

responseBody

例子,判断接口请求响应文本中是否有"ok"字符:

tests["Body matches string"] =responseBody.has("ok");

如果接口返回的值Json字符串:

var jsonData = JSON.parse(responseBody);
var code = jsonData.code

4. 获取接口请求响应Header

语法:

postman.getResponseHeader("****");

例子,获取响应文本类型

var content_type = postman.getResponseHeader("content_type");

5. 获取Cookie信息

语法:

postman.getResponseCookie("***").value;

例子,获取sessionid

var sessionid = postman.getResponseCookie("sessionid").value;

6. 设置环境变量

语法:

postman.setEnvironmentVariable(key, value);

例子,获取到sessionid后,把它设置为环境变量

postman.setEnvironmentVariable("sessionid", sessionid);

7. 设置全局变量

语法:

postman.setGlobalVariable("variable_key", "variable_value");

8. 清除环境变量

语法:

postman.clearEnvironmentVariable("variable_key");

9. 清除全局变量

语法:

postman.clearGlobalVariable("variable_key");

10. Pre-request scripts

在发送request之前,编写pre-request script,定制化request

10. 插件版Postman开启调试模式

步骤1:
在chrome地址栏中输入:chrome://flags/#debug-packed-apps,开启Debugging for debug-packed-apps
步骤2:
输入chrome://inspect/#apps,选择postman的inspect,就会弹出postman的调试框

你可能感兴趣的:(Postman的一些进阶用法)