比较来自API的两个响应但出现错误

时间:2018-09-05 08:02:12

标签: javascript postman

This is the error which i am getting. It should check for the element inside userLinesList我有两个答复:-

ResultObj32 (Response1)
"errorDescription": "",
"message": "",
"resultCode": "OK",
"resultObj": {
    "userLinesList": [{
        "lineId": 1,
        "lineType": "MOBILE",
        "lineValue": "11"
    }],
    "externalObject": {

}}
  ResultObj63 (Response2)
{
  "resultCode": "OK",
"errorDescription": "",
"message": "",
"resultObj": {
    "userLinesList": [{
        "lineId": 1,
        "lineType": "MOBILE",
        "lineValue": "11"
    }],
    "externalObject": ""
},
"systemTime": 1536131455

 }

我正在比较邮递员中的两个响应,但是当我进行比较时,我无法进入userLinesList和externalObject里面

  

3.2的值是:[对象对象] --- 6.3值是:[对象对象]

所以我只想进入此用户列表和externalObject进行比较

这是代码:-

console.log("Entered into assertion check");
tests["3.2 response is"+pm.variables.get("ResultObj32")+ "--------" +"6.3 response is"+pm.variables.get("ResultObj63")] = true;
var x = JSON.parse(pm.variables.get("ResultObj32"));
var y = JSON.parse(pm.variables.get("ResultObj63"));
console.log("3.2 response object length is "+Object.keys(x).length);
console.log("6.3 response object length is "+Object.keys(y).length);



for (var i=0; i<Object.keys(x).length; i++)
{
    //console.log("Execution entered into first i loop");
    if (y.hasOwnProperty(Object.keys(x)[i]) && Object.keys(x)[i] !== "resultObj")
    {
        console.log("Object present:"+Object.keys(x)[i]);
        var key = Object.keys(x)[i];
        console.log("Key is:"+key);
        console.log("Corresponding value in 3.2 is :"+Object.values(x)[i]);
        console.log("Corresponding value in 6.3 is :"+y[key]);
        tests["Validation of element in response: "+Object.keys(x)[i]+ "----------" + "3.2 value is:  " + Object.values(x)[i] + ":" + "6.3 value is:   "+y[key] ]= Object.values(x)[i] === y[key];
    }
    else if (y.hasOwnProperty(Object.keys(x)[i]) && Object.keys(x)[i] === "resultObj")
    {

        for (var len=0; len<Object.keys(x.resultObj).length; len++)
        {
            console.log("Element that is getting verified is:"+Object.keys(x.resultObj)[len]);
            if (y[Object.keys(x)[i]].hasOwnProperty(Object.keys(x.resultObj)[len]))
            {
                 console.log("Object present:"+Object.keys(x.resultObj)[len]);
                 var key = Object.keys(x.resultObj)[len];
                 var y1 = y[Object.keys(x)[i]];
                 console.log("Key is:"+key);
                console.log("Key in 3.2 is:"+key);
                console.log("key in 6.3 is:"+Object.keys(y.resultObj)[len]);
                 console.log("Corresponding value in 6.3 is:"+y1[Object.keys(x.resultObj)[len]]);

                 if(Object.values(x.resultObj)[len] ==="" && y1[Object.keys(x.resultObj)[len]])
                 {
                 tests["Validation of element in resultObj is passed: "+Object.keys(x.resultObj)[len]+ "----------" + "3.2 value is:  " + Object.values(x.resultObj)[len] + "---" + "6.3 value is:   "+Object.values(y.resultObj)[len]]= true;
                 }
                 else
                 {
                     tests["Validation of element in resultObj is failed: "+Object.keys(x.resultObj)[len]+ "----------" + "3.2 value is:  " + Object.values(x.resultObj)[len] + "---" + "6.3 value is:   "+y1[Object.keys(x.resultObj)[len]]]= false;
                 }
            }
            else if(y[Object.keys(x)[i]].hasOwnProperty(Object.keys(x.resultObj)[len]))
            {
                console.log("Object present:"+Object.keys(x.resultObj)[len]);
                 var key = Object.keys(x.resultObj)[len];
                 var y1 = y[Object.keys(x)[i]];
                 console.log("Key in 3.2 is:"+key);
                 console.log("Corresponding value in 6.3 is:"+y1[Object.keys(x.resultObj)[len]]);

                 if(Object.values(x.resultObj)[len] === "" && y1[Object.keys(x.resultObj)[len]] === "")
                 {
                 tests["Validation of element in resultObj is passed: "+Object.keys(x.resultObj)[len]+ "----------" + "3.2 value is:  " + Object.values(x.resultObj)[len] + "---" + "6.3 value is:   "+y1[Object.keys(x.resultObj)[len]]]=true;
                 }
                 else
                 {
                   tests["Validation of element in resultObj is passed: "+Object.keys(x.resultObj)[len]+ "----------" + "3.2 value is:  " + Object.values(x.resultObj)[len] + ":" + "6.3 value is:   "+y1[Object.keys(x.resultObj)[len]]]=true;   
                 }

            }
            else if(y[Object.keys(x)[i]].hasOwnProperty(Object.keys(x.resultObj)[len]))
            {
                console.log("Object present:"+Object.keys(x.resultObj)[len]);
                 var key = Object.keys(x.resultObj)[len];
                  var y1 = y[Object.keys(x)[i]];
                 console.log("Key is:"+key);
                 console.log("Corresponding value in 6.3 is"+y1[Object.keys(x.resultObj)[len]]);
                 if(Object.values(x.resultObj)[len] !=="" && y1[Object.keys(x.resultObj)[len]] ===null)
                 {
                 tests["Validation of element in resultObj is passed: "+Object.keys(x.resultObj)[len]+ "----------" + "3.2 value is:  " + Object.values(x.resultObj)[len] + ":" + "6.3 value is:   "+y1[Object.keys(x.resultObj)[len]]]= true;
                 }
                 else
                 {
                  tests["Validation of element in resultObj is failed: "+Object.keys(x.resultObj)[len]+ "----------" + "3.2 value is:  " + Object.values(x.resultObj)[len] + ":" + "6.3 value is:   "+y1[Object.keys(x.resultObj)[len]]]= false;   
                 }
            }
            else
            {
                tests["Validation of element in resultObj is failed: "+Object.keys(x.resultObj)[len]+ "----------" + "3.2 value is:  " + Object.values(x.resultObj)[len] + ":" + "6.3 value is:   "+y[key] ]= false;
            }
        }
    }
    else
    {
        console.log("Object not present:"+Object.keys(x)[i]);
        tests["Validation of element in response: "+Object.keys(x)[i]+ "----------" + "3.2 value is:  " + Object.values(x)[i] + ":" + "6.3 value is:   "+y[key] ]= false;
    }

}

2 个答案:

答案 0 :(得分:0)

您可以发布Postman窗口的屏幕截图吗?

通常,在邮递员中,当返回带有嵌套JSON值的响应时,对象旁边会出现一个下拉箭头,单击该对象时该箭头会展开以显示内容。

答案 1 :(得分:0)

在检查代码时,似乎是在串联一个字符串和一个JavaScript对象。发生这种情况时,您将获得“ [object Object]”。要突破此“ [object Object]”,您有三个选择。您可以有选择地取消引用对象。试试:

console.log("Corresponding value in 6.3 is:"+ y1[Object.keys(x.resultObj)[len]].lineId);

或吐出整个对象。试试

console.log("Corresponding value in 6.3 is:"+ JSON.stringify(y1[Object.keys(x.resultObj)[len]]));

或将其放入foreach中。试试

var innerObject = y1[Object.keys(x.resultObj)[len]]);
for(var innerKey in innerObject)
{
    console.log("run your magic code", innerKey, innerObject[innerKey]);
}

这些方法的挑战在于,随着时间的流逝,它们将导致高维护成本。考虑这种方法。

首先,让我们添加每个对象中彼此不在的几个元素。其次,让我们更改某些对象的数据类型。记下lineId。一个是字符串,另一个是数字。

var ResultObj32 = {
"errorDescription": "",
"message": "",
"resultCode": "OK",
"internal32":{},
"resultObj": {
    "userLinesList": [{
        "lineId": "1",
        "lineType": "MOBILE",
        "lineValue": "11",
        "notin63": true
    }],
    "externalObject": {

}}};
  var ResultObj63 =
{
  "resultCode": "OK",
"errorDescription": "",
"message": "",
"internal63":{},
"resultObj": {
    "userLinesList": [{
        "lineId": 1,
        "lineType": "MOBILE",
        "lineValue": "11",
        "notin32":true
    }],
    "externalObject": ""
},
"systemTime": 1536131455

 };



function objCompare(obj1, obj2, missing) {
    missing = (undefined == missing ? false : missing);
    Object.entries(obj1).forEach(([key, value]) => {
        if(undefined == obj2[key] || null == obj2[key]) {
            console.log(key, "is not in obj2");
            return;
        }
       if (value && ["string", "number", "boolean"].indexOf(typeof value) < 0) {
            objCompare(obj1[key], obj2[key], missing);
            return;
        }
        if(missing){
            return;
        }
        if(obj1[key] == obj2[key] && obj1[key] === obj2[key]) {
            console.log(key, value, "Is in both Objects and they are the same type");
        }
        if(obj1[key] == obj2[key] && obj1[key] !== obj2[key]) {
            console.log(key, value, "Is in both Objects and they are NOT the same type");
        }
    });
}

让我们通过上述递归函数运行新更改的对象。

console.log("Comparing obj32 to obj63 as obj2");
objCompare(ResultObj32, ResultObj63);
console.log("Comparing ob63 to obj32 as obj2")
var diffOnly = true;
objCompare(ResultObj63, ResultObj32, diffOnly);

请注意,我在第二次调用时反转了对象,并且报告始终为obj2。然后我得到的是两个结果集,一个显示它们是相同的,另一个显示它们是相同但不相同的类型。

Comparing obj32 to obj63 as obj2
errorDescription  Is in both Objects and they are the same type
message  Is in both Objects and they are the same type 
resultCode OK Is in both Objects and they are the same type
internal32 is not in obj2
lineId 1 Is in both Objects and they are NOT the same type
lineType MOBILE Is in both Objects and they are the same type
lineValue 11 Is in both Objects and they are the same type
notin63 is not in obj2

Comparing ob63 to obj32 as obj2
internal63 is not in obj2
notin32 is not in obj2
systemTime is not in obj2