First you have to set the environment which is having 2 base url's one is of STG OR UAT and other is PROD
==========================================================================
{{API_UAT}}/app/api.json // this is request 1
// then in test request Script of UAT/STG
================================================================
var jsonData = pm.response.json();
let y = ;
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
statusCode = true;
});
pm.test("Response time is less than 1000ms", function() {
pm.expect(pm.response.responseTime).to.be.below(1000);
});
if (statusCode === true) {
const jsonData = pm.response.json();
const ErrorSchema400 = {
"type": "object",
"properties": {
"assets": {
"type": "null"
},
"status": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
]
}
},
"required": [
"assets",
"status"
]
};
const ErrorSchema404 = {
"type": "object",
"properties": {
"assets": {
"type": "null"
},
"status": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
]
}
},
"required": [
"assets",
"status"
]
};
if (jsonData.status.code == 200) {
pm.test('Json is valid', () => {
var ignoreDigit = new RegExp("^[0-9]*$");
var keys = extractJSON(jsonData, []);
function extractJSON(obj, keys) {
for (const i in obj) {
if (/^"0-9"/.test(i)) {
continue}
if (Array.isArray(obj[i]) || typeof obj[i] === 'object') {
if (!ignoreDigit.test(i))
y.push(i);
//console.log("Outer", i);
//keysn = i;
extractJSON(obj[i], keys + ' > ' + i + ' > ');
} else {
if (!ignoreDigit.test(i))
y.push(i);
//console.log("Nested", i);
}
}
}
var keysArr = y;
//console.log(keysArr);
pm.test("Keys of UAT API are " + " = " + keysArr.sort());
pm.environment.set("ALL UAT KEYS", keysArr.sort());
});
} else if (jsonData.status.code == 400) {
pm.test('code: 400,Refer some Error in response', () => {
const schema = ErrorSchema400; // Only checking for success response at present
pm.expect(tv4.validate(jsonData, schema)).to.be.true; {
throw new Error("Please refer Validation in Response");
} //to treat as an error
});
} else if (jsonData.status.code == 404) {
pm.test('code: 404,message:Data not Found', () => {
const schema = ErrorSchema404; // Only checking for success response at present
pm.expect(tv4.validate(jsonData, schema)).to.be.true;
});
}
}
Then in production response
{{API_PROD}}/app/api.json // this is request 2 production API
Then in test request of prod Api
NOTE:- Please make sure that use the same collection and environment but base url will be different
==============================================================================
var jsonData = pm.response.json();
let x = ;
var NewArray1 = ;
var NewArray2 = ;
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
statusCode = true;
});
pm.test("Response time is less than 1000ms", function() {
pm.expect(pm.response.responseTime).to.be.below(1000);
});
if (statusCode === true) { // this is the real schema validation
const jsonData = pm.response.json();
const ErrorSchema400 = {
"type": "object",
"properties": {
"assets": {
"type": "null"
},
"status": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
]
}
},
"required": [
"assets",
"status"
]
};
const ErrorSchema404 = {
"type": "object",
"properties": {
"assets": {
"type": "null"
},
"status": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
]
}
},
"required": [
"assets",
"status"
]
};
if (jsonData.status.code == 200) {
pm.test('Json is valid', () => {
//code to fetch all json keys whether nested or other no need to go for object.keys !!
var ignoreDigit = new RegExp("^[0-9]*$");
var keys = extractJSON(jsonData, []);
function extractJSON(obj, keys) {
for (const i in obj) {
if (/^"0-9"/.test(i)) {
continue}
if (Array.isArray(obj[i]) || typeof obj[i] === 'object') {
if (!ignoreDigit.test(i))
x.push(i);
//console.log("Outer", i);
extractJSON(obj[i], keys + ' > ' + i + ' > ');
} else {
if (!ignoreDigit.test(i))
x.push(i);
//console.log("Nested", i);
}
}
}
var keysArr = x;
//console.log(keysArr);
pm.test("Keys of PROD API are" + " = " + keysArr.sort());
pm.environment.set("ALL PROD KEYS", keysArr.sort());
pm.test("[>>PASS(If PROD and UAT Keys are equal)]== OR ==>[>>FAIL(If any Mismatches => Refer details in Report[Total Requests]== OR ==>[>>FAIL(No Mismatches but Shortage of Keys Array]", function() {
pm.expect(pm.environment.get('ALL PROD KEYS', '{{ALL PROD KEYS}}')).to.deep.equal(pm.environment.get('ALL UAT KEYS', '{{ALL UAT KEYS}}'));
});//this is the main test case which is comparing the two different Environment json keys
function diff(array1, array2) { // Here User can compare the 2 different Api json keys to me matched
var arra1 = pm.environment.get('ALL UAT KEYS', '{{ALL UAT KEYS}}');
//console.log("UATUATUATUAT", pm.environment.get('ALL UAT KEYS', '{{ALL UAT KEYS}}'));
var arra2 = pm.environment.get('ALL PROD KEYS', '{{ALL PROD KEYS}}');
//console.log("PRODPRODPROD", pm.environment.get('ALL PROD KEYS', '{{ALL PROD KEYS}}'));
arra1 = arra1.toString().split(",").map(String);
arra2 = arra2.toString().split(",").map(String);
for (var i in arra1) {
if (arra2.indexOf(arra1[i]) === -1) NewArray1.push(arra1[i]);
}
for (i in arra2) {
if (arra1.indexOf(arra2[i]) === -1) NewArray2.push(arra2[i]);
}
pm.test("Keys present in UAT api but not in PROD api = " + NewArray1.sort((a, b) => a - b));
pm.test("Keys present in PROD api but not in UAT api = " + NewArray2.sort((a, b) => b - a));
//console.log("Keys present in UAT api but not in PROD api = " + NewArray1.sort((a, b) => a - b));
//console.log("Keys present in PROD api but not in UAT api = " + NewArray2.sort((a, b) => b - a));
var comparison = [];
comparison[0] = NewArray1.sort((a, b) => a - b);
comparison[1] = NewArray2.sort((a, b) => a - b);
return comparison;
}
diff("arra1", "arra2");
});
} else if (jsonData.status.code == 400) {
pm.test('code: 400,Refer some Error in response', () => {
const schema = ErrorSchema400; // Only checking for success response at present
pm.expect(tv4.validate(jsonData, schema)).to.be.true; {
throw new Error("Please refer Validation in Response");
} //to treat as an error
});
} else if (jsonData.status.code == 404) {
pm.test('code: 404,message:Data not Found', () => {
const schema = ErrorSchema404; // Only checking for success response at present
pm.expect(tv4.validate(jsonData, schema)).to.be.true;
});
}
}
//===========JAI SHREE MAHANKAL========OM BAM SHIV=================