//获取全局变量‘XZYXCookie’的值------------ Get a global variable
//pm.globals.get("XZYXCookie");
//获取环境变量‘variable_key’的值----------- Get a variable
//pm.environment.get("variable_key");
//设置环境变量 ----------------------------Set an environment variable
//pm.environment.set("variable_key", "variable_value");
//设置全局变量-----------------------------set a global variable
//pm.globals.set("variable_key", "variable_value");
//清除一个环境变量--------------------------clear an environment variable
//pm.environment.unset("variable_key");
//清除一个全局变量--------------------------clear a global variable
//pm.globals.unset("variable_key");
//发送异步请求 ,此函数可作为预请求和测试脚本使用 将XML主体转换为JSON对象
//-----------------------------------------send a request
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log("-------------------testing");
console.log(response.json());
});
//请求返回的状态码是200 ------------------status code:Code is 200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
//检查响应主体body是否包含字符串“success”-----Response body:Contains string
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("success");
});
//检查JSON值 -------------------Response body:JSON value check
/*
{
"success": true,
"staffName": "管理员",
"staffOrg": "营销中心"
}
*/
pm.test("Your test name", function () {
var jsonData = pm.response.json();
var jsonDatastaffOrg=jsonData.staffOrg;
pm.expect(jsonDatastaffOrg).to.eql("营销中心");
});
/*检查json里的值与某个值匹配
{
"dataList": [
{
"jump": {
"type": 41
}
}
]
}
var jsonDatastaffOrg=jsonData.["dataList"].jump.type;
注意[]和{}的区别,遇到[]括号则参数也用[],遇到{}括号,则用.
*/
//检查响应体是否为一个字符串"response_body_string"----Response body:-is equal to a string
/*
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
*/
//检查响应体是否包含"Content-Type"-----------Response headers:Content-Type header check
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
//检查响应时间是否小于200ms ----------------Response time is less than 200ms
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
//成功的POST请求状态码,状态码可以自己写404之类的,满足其一就行-status code:Successful Post request
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([200,202]);
});
//代码名包含一个字符串"Created"
pm.test("Status code name has string", function () {
pm.response.to.have.status("staffName");
});
/*
var jsonObject = xml2Json(responseBody);
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;
});*/
//var data = JSON.parse(responseBody); tests["Your test name"] = data.value === 100;
//如果通过postman里面会有绿色标识
//控制台打印输出,可以在view--show Postman Console
//console.log("");
//let json = JSON.parse(responseBody);
// responseBody是包含整个返回内容的字符串
//let foobar = json.foo.bar[0].foobar;
//提取某字段的值: 假设结构为 {"foo": {"bar": [{"foobar": 1}, {"baz": 2}]}}