Postman学习之常用断言

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数据使用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, // 假设它有一个base64编码值
    rawContent = base64Content.slice('data:application/octet-stream;base64,'.length);

intermediate = CryptoJS.enc.Base64.parse(base64content); // CryptoJS 是一个内置对象, 文档说明: https://www.npmjs.com/package/crypto-js
pm.test('Contents are valid', function() {
  pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true; //非空检查
});

发送异步请求

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

pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});

将XML正文转换为JSON对象

var jsonObject = xml2Json(responseBody);

参考文章:

https://www.cnblogs.com/suim1218/p/8931159.html

https://blog.csdn.net/wust_lh/article/details/87188197

https://stackoverflow.com/questions/46243745/postman-scripts-pm-is-not-defined

上一页12下一页


留言