Reputation: 543
I am very new to Postman. I want to compare JSON body object response.
json_response = JSON.parse(responseBody);
x=json_response.counter
pm.expect(x).to.equal("400");
It gives me 400 value of that corresponding 'counter' (or '400' Value of Key 'counter')
But I would like to compare 'counter' to 'counter' itself. (cases like, Counter, CounTer, etc.)
{
"counter": 400,
"validInMinutes": 660,
"currentCounter": 322,
}
Basically, I want to test that received all JSON Keys are equal to what I am looking for! Is there an easy way to do it?
Upvotes: 3
Views: 2443
Reputation: 880
You have a specific schema in your mind, that you want to check. (e.g. Mandatory fields, Types, etc.) An other possible way is to validate against a pre-defined JSON-schema:
//Schema definition (can be available as a global var)
var schema = {
"type": "object",
"properties": {
"counter": {
"type": "number"
},
"validInMinutes": {
"type": "number"
},
"currentCounter": {
"type": "number"
}
},
"required": ["counter", "validInMinutes", "currentCounter"]
};
//Test
pm.test('Schema is valid', function() {
var jsonData = pm.response.json();
pm.expect(tv4.validate(jsonData, schema)).to.be.true;
});
The Line "required": ["counter", "validInMinutes", "currentCounter"]
is exactly your definition of what kind of properties must be there. But you can also define more strct rules, as "must be of type Numer", or must have exactly 3 digits, and so on.
Upvotes: 1
Reputation: 29086
Postman's pm.expect
uses the Chai library for assertions (this links to pm.cookies
but the entry for pm.expect
is under that heading, for some reason). A using the Chai API you can verify the response only has the keys you expect by saying expect(someObject).to.have.keys(expectedKeys)
and define what the keys should be:
//for convenience - in Postman, these would be readily available.
var pm = {
expect: chai.expect,
response: {
json: function() {
// the response you would get
return { "counter": 400, "validInMinutes": 660, "currentCounter": 322 };
}
}
};
/* --- test script begins --- */
var expectedKeys = ["counter", "validInMinutes", "currentCounter"];
pm.expect(pm.response.json()).to.have.keys(expectedKeys); // OK
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.2.0/chai.min.js"></script>
And here are some examples of how this can expectation can fail:
var pm = {
expect: chai.expect,
response: {
json: function() {
// "Counter" starts with capital C
return { "Counter": 400, "validInMinutes": 660, "currentCounter": 322 };
}
}
};
/* --- test script begins --- */
var expectedKeys = ["counter", "validInMinutes", "currentCounter"];
pm.expect(pm.response.json()).to.have.keys(expectedKeys); // fails
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.2.0/chai.min.js"></script>
var pm = {
expect: chai.expect,
response: {
json: function() {
// "counter" is missing
return { "validInMinutes": 660, "currentCounter": 322 };
}
}
};
/* --- test script begins --- */
var expectedKeys = ["counter", "validInMinutes", "currentCounter"];
pm.expect(pm.response.json()).to.have.keys(expectedKeys); // fails
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.2.0/chai.min.js"></script>
var pm = {
expect: chai.expect,
response: {
json: function() {
//same object you have...
return { "counter": 400, "validInMinutes": 660, "currentCounter": 322 };
}
}
};
/* --- test script begins --- */
//...but there is an extra expected key
var expectedKeys = ["counter", "validInMinutes", "currentCounter", "otherKey"];
pm.expect(pm.response.json()).to.have.keys(expectedKeys); // fails
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.2.0/chai.min.js"></script>
Upvotes: 3