目录
1.如果是body返回的是html,想要把html的值设置成环境变量;
2.如果body返回的是json,想要把返回的值设置成环境变量;
5.如果想要把responseBody中的headers的值设置成环境变量;
6.如果要把body中的text、html、XML、json、JS设置成环境变量;
7.如果想要把responseBody中的text、XML、json、JS设置成环境变量;
1.如果是body返回的是html,想要把html的值设置成环境变量;
const $ = cheerio.load(responseBody);
pm.environment.set("yuyuedanbianhao", $('span').text());
2.如果body返回的是json,想要把返回的值设置成环境变量;
var jsonData =JSON.parse(responseBody);//获取body中返回的所有参数
postman.setEnvironmentVariable("orderid-value",jsonData.result.orderId)
3.如果想要把token的值设置成环境变量;
var jsonData =JSON.parse(responseBody);//获取body中返回的所有参数
postman.setEnvironmentVariable("token",jsonData.result.token)
4.如果想要把 params的值设置成环境变量:
5.如果想要把responseBody中的headers的值设置成环境变量;
var data =postman.getResponseHeaders("token");
postman.setEnvironmentVariable("token",data)
6.如果要把body中的text、html、XML、json、JS设置成环境变量;
7.如果想要把responseBody中的text、XML、json、JS设置成环境变量;
8.将xml格式的response转换成son格式
Response body:Convert XML body to a JSON Object
对应脚本:
var jsonObject = xml2Json(responseBody);
参数:(默认不需要设置参数,为接口的response)需要转换的xml
9. 清除一个全局变量
Clear a global variable
对应脚本:
postman.clearGlobalVariable("variable_key");
参数:需要清除的变量的key
10.清除一个环境变量
Clear an environment variable
对应脚本:
postman.clearEnvironmentVariable("variable_key");
参数:需要清除的环境变量的key
11.response包含内容
Response body:Contains string
对应脚本:
tests["Body matches string"] =responseBody.has("string_you_want_to_search");
参数:预期内容
12.将xml格式的response转换成son格式
Response body:Convert XML body to a JSON Object
对应脚本:
var jsonObject = xml2Json(responseBody);
参数:(默认不需要设置参数,为接口的response)需要转换的xml
13.response等于预期内容
Response body:Is equal to a string
对应脚本:
tests["Body is correct"] = responseBody === "response_body_string";
参数:预期response
14.json解析key的值进行校验
Response body:JSON value check
对应脚本:
tests["Args key contains argument passed as url parameter"] = 'test' in responseJSON.args
参数:test替换被测的值,args替换被测的key
15.检查response的header信息是否有被测字段
Response headers:Content-Type header check
对应脚本:
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
参数:预期header
16.响应时间判断
Response time is less than 200ms
对应脚本:
tests["Response time is less than 200ms"] = responseTime < 200;
参数:响应时间
17.设置全局变量
Set an global variable
对应脚本:
postman.setGlobalVariable("variable_key", "variable_value");
参数:全局变量的键值
18.设置环境变量
Set an environment variable
对应脚本:
postman.setEnvironmentVariable("variable_key", "variable_value");
参数:环境变量的键值
19.判断状态码
Status code:Code is 200
对应脚本:
tests["Status code is 200"] = responseCode.code != 400;
参数:状态码