How to Write a Postman Test to Compare the Response Json Against Another Json

How to compare CSV to Postman JSON Response values?

Try casting the csv integer value to a string and using to.equal instead of to.eql (which is equivalent to to.deep.equal):

pm.expect(jsonData.args.ramen).to.equal(new String(pm.iterationData.get("Ramen")));

Using Postman to take values from two different responses to assert against each other

In your first request test, get required elements and push it to array and set that array into environment variable.

var jsonData = pm.response.json();
var array = [];

jsonData.forEach(function(element) {
array.push(element.name);
});

pm.environment.set("pre_request_array", array);

In your second request test, get environment variable we previously set and now can compare the both:

var jsonData = pm.response.json();
var currArray = [];

var prevArray = pm.environment.get("pre_request_array");

jsonData.sites.forEach(function(element) {
currArray.push(element.name);
});

console.log(prevArray);
console.log(currArray);

//TODO: add compare logic here

In console, you can find output as below screen.

enter image description here

How to compare two dates in Postman

I have a similar scenario: I need to compare two dates in postman, dates in a format like "2022-01-16T19:40:51.816".

So I use this:

// (dates from JSON for my case)
// var date1 = "2022-01-16T19:40:51.816";
// var date2 = "2022-01-16T17:40:51.816";
// (today for your case)
// var today = new Date();
pm.expect(Date.parse(date1)).to.be.above(Date.parse(date2));


Related Topics



Leave a reply



Submit