10.jmeter jsr223 javascript 深度比对json object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 function sortJSON(data, key, way) { //log.info(" " + key + " ------------------- " + typeof(key)); return data.sort(function(a, b) { var x = a[key]; var y = b[key]; if (way === '123' ) { return ((x < y) ? -1 : ((x > y) ? 1 : 0)); } if (way === '321') { return ((x > y) ? -1 : ((x < y) ? 1 : 0)); } }); } function isArray(what) { return Object.prototype.toString.call(what) === '[object Array]'; } var deepequals = function ( x, y ) { if ( x === y ) return true; // if both x and y are null or undefined and exactly the same if ( ! ( x instanceof Object ) || ! ( y instanceof Object ) ) return false; // if they are not strictly equal, they both need to be Objects if ( x.constructor !== y.constructor ) return false; // they must have the exact same prototype chain, the closest we can do is // test there constructor. // log.info("1111111111111111111111 "+ typeof("payMethod") + "" + Object.keys(x)[0] + "" + typeof(Object.keys(x)[0])); var sortkey2 = "payMethod"; if(isArray(x) && x.length > 0 && typeof x[0] == 'object' && Object.keys(x[0])[0] === "payMethod" ) { //log.info(JSON.stringify(x)+" ======== "+ Object.keys(x)); //log.info(JSON.stringify(y)+" ======== "+ Object.keys(y)); sortkey2 = Object.keys(x[0])[0]; if(isArray(x)) { sortJSON(x,sortkey2, '123');} if(isArray(y)) { sortJSON(y,sortkey2, '123'); } //log.info(JSON.stringify(x)+" ======== "+ Object.keys(x)); //log.info(JSON.stringify(y)+" ======== "+ Object.keys(y)); } //if(sortkey2 == sortkey) log.info("same string " + sortkey); // if(isArray(x)) sortJSON(x,'a', '123'); // if(isArray(y)) sortJSON(y,'a', '123'); for ( var p in x ) { // log.info(p+":"+ JSON.stringify(x[p]) + " ====== " + JSON.stringify(y[p])); if ( ! x.hasOwnProperty( p ) ) continue; // other properties were tested using x.constructor === y.constructor if ( ! y.hasOwnProperty( p ) ) { log.info(JSON.stringify(x)+"\n================属性不一致 ===============\n"+JSON.stringify(y)); return false; } // allows to compare x[ p ] and y[ p ] when set to undefined if ( x[ p ] === y[ p ] ) continue; // if they have the same strict value or identity then they are equal if ( typeof( x[ p ] ) !== "object" ) { log.info(JSON.stringify(x)+"\n================值不一致===============\n"+JSON.stringify(y)); return false; }; // Numbers, Strings, Functions, Booleans must be strictly equal if ( ! deepequals( x[ p ], y[ p ] ) ) return false; // Objects and Arrays must be tested recursively } for ( p in y ) { if ( y.hasOwnProperty( p ) && ! x.hasOwnProperty( p ) ) { log.info(JSON.stringify(x)+"\n================反向检查属性不一致===============\n"+JSON.stringify(y)); return false; }; // allows x[ p ] to be set to undefined } return true; } //Object.prototype.deepEquals = deepEquals; var beforeObject = JSON.parse(vars.get("83response")); var afterObject = JSON.parse(prev.getResponseDataAsString()); if(deepequals( beforeObject, afterObject )){ log.info("deep equals"); } else{ // log.info(deepequals(testa,testb)); AssertionResult.setFailure(true); AssertionResult.setFailureMessage("content not deep equal "); AssertionResult.setResultForFailure("content not deep equal"); }