1.Postman数据校验/参数关联/获取返回内容等

一、数据校验

//定义变量

var jsonData = pm.response.json();

//检查状态为200

pm.test("Status code is 200", function () {

    pm.response.to.have.status(200);

});

//代码名称包含一个字符串

pm.test("Status code name has string", function () {

    pm.response.to.have.status("Created");

});

 

//成功的POST请求状态码

pm.test("Successful POST request", function () {

    pm.expect(pm.response.code).to.be.oneOf([201,202]);

});

//检查返回的内容为json格式

pm.test("response must be valid json", function () {

     pm.response.to.be.json;

});

//返回内容中包含is_open (nationality_id字段的值为is_open?)

pm.test("ResponseBody has nationality_id", function () {

    pm.expect(pm.response.text()).to.include("nationality_id");

});

//检查响应正文是否包含字符串

pm.test("Body matches string", function () {

    pm.expect(pm.response.text()).to.include("string_you_want_to_search");

});

//检查响应体是否等于一个字符串

pm.test("Body is correct", function () {

    pm.response.to.have.body("response_body_string");

});

 

//检查json字段的值

pm.test("nationalities id in reponseBody is right", function () {

    pm.expect(jsonData.nationalities[0].nationality_id).to.eql(1);

});

//显示返回的内容

pm.test("response is "+pm.response.text(), function(){

})

pm.test(jsonData.data.code+" version: "+jsonData.data.version, function () {
});

//拿数组的名字nationalities,[0]代表数组中第一个元素

pm.test("nationalities name in response is right", function () {

    pm.expect(jsonData.nationalities[0].name).to.eql("汉");

});

//检查nationalities的长度为6位

pm.test("nationalities length is right", function(){

    pm.expect(jsonData.nationalities.length)==6;

});

pm.test("Response JSON length is correct", function () {

    pm.expect(jsonData.questions.length).to.eql(6);

});

//打印short_uri_clients的长度

pm.test("Response JSON length is: "+jsonData.short_uri_clients.length, function () {

});

 

//设置环境变量

pm.environment.set("variable_key", "variable_value");

 

//获取一个环境变量

pm.environment.get("variable_key");

 //设置全局变量

pm.globals.set("ContactID", jsonData.con.id);

//获取全局变量

pm.test("ID :" + pm.globals.get("ID"), function () {

    pm.expect(jsonData.con.id).to.eql(pm.globals.get("ID"));

}); 

//清除一个环境变量

pm.environment.unset("variable_key");

 

//清除一个全局变量

pm.globals.unset("variable_key");

选择环境:

var Environment = environment.Environment;

if(Environment.indexOf("uat")>0)

{

    pm.globals.set("ClientId", "60c2d123-6e");

    pm.globals.set("Prefix", "https://xxxxx.com/xxxxx");

}

else if(Environment.indexOf("aws-m")>0)

{

    pm.globals.set("ClientId", "223ea8ad-0ba7-4");

    pm.globals.set("Prefix", "https://xxxxx.com/xxxxx");

}

//将嵌套对象设置为环境变量

var array = [1, 2, 3, 4];

pm.environment.set("array", JSON.stringify(array, null, 2));

 

var obj = { a: [1, 2, 3, 4], b: { c: 'val' } };

pm.environment.set("obj", JSON.stringify(obj));

 

//内容类型是存在的

pm.test("Content-Type is present", function () {

    pm.response.to.have.header("Content-Type");

});

 

//响应时间小于200ms

pm.test("Response time is less than 200ms", function () {

    pm.expect(pm.response.responseTime).to.be.below(200);

}); 

 

//对于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;

});

 

//解码base64编码的数据

var intermediate,

base64Content, // assume this has a base64 encoded value

rawContent = base64Content.slice('data:application/octet-stream;base64,'.length);

 

intermediate = CryptoJS.enc.Base64.parse(base64content); // CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-js

pm.test('Contents are valid', function() {

  pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true; // a check for non-emptiness

});

 

//发送异步请求

//该功能既可以作为预先请求,也可以作为测试脚本使用。

pm.sendRequest("https://postman-echo.com/get", function (err, response) {

    console.log(response.json());

});

 

//将XML正文转换为JSON对象

var jsonObject = xml2Json(responseBody);

 

//设置下一个要执行的请求

postman.setNextRequest("request_name");

 

二、参数关联

1.Tests中定义全局变量: pm.globals.set("ID", jsonData.con.id);

2.后续接口中引用 {{ID}}

{{$guid}}:添加一个v4样式的guid

{{$timestamp}}:添加当前的时间戳。

{{$randomInt}}:添加0到1000之间的随机整数

三、获取返回的内容

var jsonData = pm.response.json();
//输出到环境变量
// pm.globals.set("version", version);

//输出到控制台
//console.log(version);

//返回json字段的值
pm.test(jsonData.data.tenantCode+" version: "+jsonData.data.version, function () {
});

参考文档https://learning.getpostman.com/docs/postman/scripts/test-examples/

猜你喜欢

转载自www.cnblogs.com/elaine888/p/11560392.html