参考:
JSON字符串JSON对象和js对象的区别
Json对象和Json字符串的区别
postman的常见断言
JSON.stringify(obj) 将JSON对象转为字符串。
JSON.parse(string) 将字符串转为JSON对象格式。
(如果不明白,可以看以下链接https://blog.51cto.com/11871779/2105308)
pm:代表postman
test:代表断言
pm.test:测试断言
pm.reponse:表示请求的返回对象
pm.reponse.code:响应状态码
pm.reponse.headers:响应消息头
pm.reponse.reponseTime:响应时间
pm.reponse.text():字符串
pm.reponse.json():json对象
var jsonData = pm.response.json();
var jsonData = JSON.parse(responseBody);
pm.environment.set("variable_key", "variable_value");
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.environment.get("variable_key");
// These statements should be wrapped in a try-catch block if the data is coming from an unknown source.
var array = JSON.parse(pm.environment.get("array"));
var obj = JSON.parse(pm.environment.get("obj"));
pm.environment.unset("variable_key");
pm.globals.set("variable_key", "variable_value");
pm.globals.get("variable_key");
pm.globals.unset("variable_key");
Get a variable (获取一个变量)
该函数在全局变量和活动环境中搜索变量。
pm.variables.get("variable_key");
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");
});
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(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");
});
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
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 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
});
Send an asynchronous request (发送异步请求)
该功能既可以作为预先请求,也可以作为测试脚本使用。
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());
});
var jsonObject = xml2Json(responseBody);
本文地址:https://blog.csdn.net/weixin_45471859/article/details/107389864
如对本文有疑问, 点击进行留言回复!!
XSS训练网站XSS Challenges所有题目解(中篇)
如何给未越狱的ios下载历史版本的app?未越狱IOS系统安装历史版本软件的方法,iPhone未越狱的手机如何安装旧版本APP?
Unity Android playerSetting相关设置
网友评论