1. 清除一个全局变量
Clear a global variable
对应脚本:
pm.globals.unset("variable_key");
参数:需要清除的变量的key
2.清除一个环境变量
Clear an environment variable
对应脚本:
pm.environment.unset("variable_key");
参数:需要清除的环境变量的key
3.获取一个全局变量的值
Get a global variable
对应脚本:
pm.globals.get("variable_key");
参数:需要获取的全局变量的key
4.获取一个变量的值
Get a variable
对应脚本:
pm.variables.get("variable_key");
参数:需要获取的变量的key
5.获取一个环境变量的值
Get an environment variable
对应脚本:
pm.environment.get("variable_key");
参数:需要获取的环境变量的key
6.response包含内容
Response body:Contains string
对应脚本:
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
参数:预期内容
7.将xml格式的response转换成son格式
Response body:Convert XML body to a JSON Object
对应脚本:
var jsonObject = xml2Json(responseBody);
参数:(默认不需要设置参数,为接口的response)需要转换的xml
8.response等于预期内容
Response body:Is equal to a string
对应脚本:
pm.response.to.have.body("response_body_string");
参数:预期response
9.json解析key的值进行校验
Response body:JSON value check
对应脚本:
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
参数:test替换被测的值,args替换被测的key
10.检查response的header信息是否有被测字段
Response headers:Content-Type header check
对应脚本:
pm.response.to.have.header("Content-Type");
参数:预期header
11.响应时间判断
Response time is less than 200ms
对应脚本:
tpm.expect(pm.response.responseTime).to.be.below(200);
参数:响应时间
12.执行请求
Send a request
对应脚本:
GET
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());
});
POST
var data = {
"key1":"value1",
"key2":"value2",
}
const echoPostRequest = {
url: 'https://postman-echo.com/post',
method: 'POST',
header:'Content-Type:application/json',
body: {
mode: 'raw',
raw: JSON.stringify(data)
}};
pm.sendRequest(echoPostRequest, function (err, res) {
console.log(res.json());
});
参数:要发送的请求
13.设置全局变量
Set an global variable
对应脚本:
pm.globals.set("variable_key", "variable_value");
参数:全局变量的键值
14.设置环境变量
Set an environment variable
对应脚本:
pm.environment.set("variable_key", "variable_value");
参数:环境变量的键值
15.判断状态码
Status code:Code is 200
对应脚本:
pm.response.to.have.status(200);
参数:状态码
16.检查code name 是否包含内容
Status code:Code name has string
对应脚本:
pm.response.to.have.status("Created");
参数:预期code name包含字符串
17.成功的post请求
Status code:Successful POST request
对应脚本:
pm.expect(pm.response.code).to.be.oneOf([201,202]);
说明:pm.response.code的值在[201,202]数组内则返回PASS,反之返回FAIL
18.微小验证器
Use Tiny Validator for JSON data
对应脚本:
var schema = {
"items": {
"type": "boolean"
}
};
var data1 = [true, false];
var data2 = [true, 123];
pm.test('Schema is valid', function() {
pm.expect(tv4.validate(data1, schema)).to.be.true;
pm.expect(tv4.validate(data2, schema)).to.be.true;
});
参数:可以修改items里面的键值对来对应验证json的参数
当前 postman 版本为 v5.5.3