JavaScript + Postman sample1

postman version: 

Postman for Windows

Version 7.29.0

win32 10.0.19041 / x64

Response 响应体样例

{

    "statusCode": "AAAXXXBBB0000",

    "message": "Success",

    "messageZh": null,

    "errMsgArgs": null,

    "traceId": "123",

    "timestamp": null,

    "nonceStr": null,

    "sign": null,

    "data": {

        "lastUpdateByName": "System",

        "ica": "300001",

        "changes": null,

        "questions": null,

        "parReId": "47023832233864",

        "insertTimestamp": "2020-12-21T07:05:41.947+0000",

        "deleteFlag": null,

        "lastUpdateTimestamp": "2020-12-21T06:49:55.263+0000",

        "attachment": null,

        "context": null,

        "action": "I",

        "paramDefCode": "XXBBB005",

        "requestType": "XXBBB005_A",

        "parallelStepData": null,

        "version": 2,

        "step": null,

        "insertBy": "AutomationTestUser",

        "parameters": "{\"cid\": \"88812112006\", \"ica\": \"121106\", \"locked\": false, \"paramId\": 466647024092659712, \"version\": 0, \"insertBy\": \"AutomationTestUser\", \"attachment\": \"{\\\"fileIndexName\\\":\\\"1ed2083f44c433ec769f40f8fa7b1\\\",\\\"fileName\\\":\\\"1573652742438-XXXXXXXX.txt\\\",\\\"downloadFileId\\\":\\\"130816595474798\\\"}\", \"deleteFlag\": false, \"lastUpdateBy\": \"AutomationTestUser\", \"insertTimestamp\": 1608533395213, \"xxx\": \"123456789.12\", \"lastUpdateTimestamp\": 1608533395213}",

        "stepCount": null,

        "immediateEffective": true,

        "insertByName": "AutomationTestUser",

        "cid": "135246",

        "lastUpdateBy": "System",

        "status": "EFFECT"

    },

    "successFlag": true,

    "deEncryptFlag": false

}

对上面的响应进行断言 Tests

1. 取到paramId, paramReqId

pm.globals.unset("paramId");pm.globals.unset("paramReqId");

var jsonData = pm.response.json();

var paramId = JSON.parse(jsonData.data.parameters).paramId; // 这里面有个问题,实际获取的值会把后两位的值变成两个0,而不是实际 响应里的值,尝试了强转也不行

console.log("paramId\n", paramId);

var paramReqId = jsonData.data.paramReqId;

console.log("paramReqId\n", paramReqId);

pm.globals.set("paramId",paramId);

pm.globals.set("paramReqId", paramReqId);

// 特定字段值校验

pm.test("xxx value", function () {

    var jsonData = pm.response.json();

    console.log("校验xxx 值\n");

    var xxxValue= JSON.parse(jsonData.data.parameters).xxx;

    console.log("xxx value\n", xxxValue);

    pm.expect(parseFloat(xxx)).to.eql(pm.globals.get("xxx"));

});

2. 一般性校验

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

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

});

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

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

});

pm.test("has xxx", function () {

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

});

pm.test("has cid", function () {

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

});

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(400); // 单位 ms

});

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

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

});

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

    pm.response.to.have.status("OK"); // or OK/Created

});

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值