1.postman对变量的赋值和取值的函数用法:
1、设置全局变量:pm.globals.set("variable_key", "variable_value");
2、设置环境变量:pm.environment.set("variable_key", "variable_value");
3、获取全局变量:pm.globals.get("variable_key");
4、获取环境变量:pm.environment.get("variable_key");
2.postman 常用脚本命令:
1 、清除一个全局变量 :pm.globals.unset(“variable_key”);
2 、清除一个环境变量: pm.environment.unset(“variable_key”);
3 、获取一个全局变量 :pm.globals.get(“variable_key”);
4 、获取一个变量该函数在全局变量和活动环境中搜索变量: pm.variables.get(“variable_key”);
5 、获得一个环境变量 :pm.environment.get(“variable_key”);
6、 检查响应主体是否包含字: pm.test(“Body matches string”, function () { pm.expect(pm.response.text()).to.include(“string_you_want_to_search”);});
7 、将XML主体转换为JSON对象: var jsonObject = xml2Json(responseBody);
8 、检查响应体是否等于字符串:pm.test(“Body is correct”, function () { pm.response.to.have.body(“response_body_string”);});
9 、检查JSON值: pm.test(“Your test name”, function () { var jsonData = pm.response.json(); pm.expect(jsonData.value).to.eql(100);});
10、Content-Type 存在: pm.test(“Content-Type is present”, function () { pm.response.to.have.header(“Content-Type”);});
11、 返回时间少于200ms :pm.test(“Response time is less than 200ms”, function () { pm.expect(pm.response.responseTime).to.be.below(200);});
12、 发送异步请求此函数可作为预请求和测试脚本使用: pm.sendRequest(“https://postman-echo.com/get”, function (err, response) { console.log(response.json());});
13 、设置一个全局变量: pm.globals.set(“variable_key”, “variable_value”);
14 、设置环境变量: pm.environment.set(“variable_key”, “variable_value”);
15 、状态码是200: pm.test(“Status code is 200”, function () { pm.response.to.have.status(200);});
16 、代码名包含一个字符串: pm.test(“Status code name has string”, function () { pm.response.to.have.status(“Created”);});
17 、成功的POST请求状态码: pm.test(“Successful POST request”, function () { pm.expect(pm.response.code).to.be.oneOf([201,202]);});
18 、为JSON数据使用: TinyValidator 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;});
3.接口自动化,环境变量的用法详解及常用方法
在实现接口自动测试的时候,会经常遇到接口参数依赖的问题,例如调取登录接口的时候,需要先获取登录的key值,而每次请求返回的key值又是不一样的,那么这种情况下,要实现接口的自动化,就要用到postman中设置环境变量这个功能了;
在postman中,可以利用tests将接口返回的response设置为环境变量,供后续接口使用(类似参数化的概念)
获取环境变量需要具体方法如下图所示;
var jsonData =JSON.parse(responseBody);//获取body中返回的所有参数
postman.setEnvironmentVariable("appKey",jsonData.data.appKey);//把返回参数中的keys设置为环境变量
如此就能把登录所需的key设置为环境变量,供后续登录接口的调用了;
同理,获取headers值更新环境变量的方法,如下图:
postman常用方法集合:
1.设置环境变量
postman.setEnvironmentVariable("key", "value");
pm.environment.get("key", "value");//postman 5.0以上版本设置环境变量的方法
2.设置全局变量
postman.setGlobalVariable("key", "value");
pm.globals.set("variable_key", "variable_value");//postman 5.0以上版本设置全局变量方法
3.检查response body中是否包含某个string
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});//5.0以上版本方法
4.检测JSON中的某个值是否等于预期的值
var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;
JSON.parse()方法,把json字符串转化为对象。parse()会进行json格式的检查是一个安全的函数。
如:检查json中某个数组元素的个数(这里检测programs的长度)
var data = JSON.parse(responseBody);
tests["program's lenght"] = data.programs.length === 5;
5.转换XML body为JSON对象
var jsonObject = xml2Json(responseBody);
tests["Body is correct"] = responseBody === "response_body_string";
6.检查response body是否与某个string相等
7.测试response Headers中的某个元素是否存在(如:Content-Type)
//getResponseHeader()方法会返回header的值,如果该值存在
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
上面的方法,不区分大小写。下面的方法,要区分大小写。
8.验证Status code的值
tests["Status code is 200"] = responseCode.code === 200;
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});//5.0以上版本方法
9.验证Response time是否小于某个值
tests["Response time is less than 200ms"] = responseTime < 200;
//5.0以上版本方法
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
10.name是否包含某个值
tests["Status code name has string"] = responseCode.name.has("Created");
//5.0以上版本方法
pm.test("Status code name has string", function () {
pm.response.to.have.status("Created");
});
11.POST 请求的状态响应码是否是某个值
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
//5.0以上版本方法
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
12.很小的JSON数据验证器
var schema = {
"items": {
"type": "boolean"
}
};
var data1 = [true, false];
var data2 = [true, 123];
console.log(tv4.error);
tests["Valid Data1"] = tv4.validate(data1, schema);
tests["Valid Data2"] = tv4.validate(data2, schema);
13.获取request值:
var Json = JSON.parse(request.data);
-
data {object}:
this is a dictionary of form data for the request. (request.data["key"]=="value") -
headers {object}:
this is a dictionary of headers for the request (request.headers["key"]=="value") -
method {string}:
GET/POST/PUT etc. -
url {string}:
the url for the request.
假设requestBody中有"version":"1.0";这个值,如果想获取到version的value值,代码如下
var Json = JSON.parse(request.data);
var version = Json["version"];
14.JSON.parse()和JSON.stringify()
JSON.parse()【从一个字符串中解析出json对象】--把string转对象
JSON.stringify()【从一个对象中解析出字符串,主要针对[object object]类型数据的转换】--把对象转String
var data={name:'goatling'}
JSON.parse(data)
结果是: '{"name":"goatling"}'
JSON.stringify(data)
结果是:name:"goatling"
15.判断字段值是否为空typeof()
var Jsondata = JSON.parse(responseBody);
if( typeof(Jsondata.data) != "undefined" )
在pre-request和tests 中获取变量的方法:
变量可以被使用在 pre-request
和 test script
中。因为这些部分是通过 JavaScript
来写的
你可以以不同的方式初始化和检索这些变量。可以在脚本中初始化变量,并将它们放在特定的范围内
1.定义一个变量在脚本中
在脚本中设置一个变量可以根据变量预定的范围通过 pm.environment.set("variable_key", "variable_value");方法或者 pm.globals.set("variable_key", "variable_value");方法,这方法要求提供变量的 key
和 value
去设置变量。当你发送请求的时候,这脚本将会执行,值将会保存在变量中,如下图:
2.取一个预定义的变量
一旦一个变量被设置,你可以使用pm.environment.set("variable_key", "variable_value");; ;
或者pm.globals.set("variable_key", "variable_value");; ;
根据适合的范围去获取变量值。这方法要求提供一个变量名作为参数去检索储存的值,如下图:
3.设置一个变量在作用域中
可以访问环境变量在相应的环境模板。全局变量可以广泛的访问,不管选择的作用域
4.注意:初次编写、编辑postman时,脚本设置的环境变量、全局变量出现全局变量赋值只显示属性的处理方案
{{值}}-可以用在url地址栏,Params,Headers,Body,Pre-request Script,Tests等模块
get请求:Params、Headers里赋值正常-表单
正常:{{sign}}-sign:*******对应参数值*********
post请求:Body里赋值会出现只显示字段(key)名称,不可以显示字段值(value)-json
例如异常:{{sign}}-{{sign}}
异常处理方案:
1.环境变量:如下图,必须先设置填写一个环境变量属性(key),后面的VALUE列不填写即可,后面再次运行接口时,会把所有脚本设置的环境变量及值显示到此列表中(值是动态获取显示的)
2.全局变量:如上图,必须先设置填写一个全局变量属性(key),后面的VALUE列不填写即可,后面再次运行接口时,会把所有脚本设置的全局变量及值显示到此列表中(值是动态获取显示的)