pm.test("Verify the status and name" , function () {
var jsonData = pm.response.json();
pm.expect(jsonData[0].name, "Name is not available").not.equal(null);
});
for some situation we get valid response but in some situation, we get empty response . So I wanted to test “name” value should not be null in the response. When the response is Not null, in this case my tests Passed. But when get empty response, in this scenario, I am getting below error:
Verify the status and name | TypeError: Cannot read property ‘name’ of undefined.
This is not a ideal test report. Instaed of this I wanted someting like that:(Valid assertion error)
Verify the status and name | AssertionError: Name is not available : expected undefined to not equal null.
How can I achieve this?
This is something handling type error and providing generic error.
The assertion that you have is looking for a value in the name key and it’s not there (undefined), that’s why it’s failing. This part .not.equal(null); also looks not quite right to me.
There is a difference between something being in the response body and having null as a value and something not being part of the response.
Would this be what you need?
pm.expect(jsonData[0].name, 'Name is not available').to.not.be.undefined;
[
{
"sku": "22",
"name": "U.S. Roaming Pass",
"expiryDate": "2020-10-02T11:10:23",
"isActive": true,
"oneTimePasses": [
{
"sku": "2000023",
"name": "U.S. Roaming - 100MB/Talk/Text (30 Days)",
"description": "Get 100 MB of extra data, 100 minutes of local/back to Canada calling, and 100 SMS of global texting while roaming in the U.S., Puerto Rico, or the U.S. Virgin Islands. Roaming pass valid for up to 30 days.",
"price": 0,
"purchasedDate": "2020-09-02T11:39:24",
"packages": [],
"duration": {
"amount": "30",
"unit": "Days"
}
}
],
"cap": [
{
"amount": "100",
"unit": "Megabytes",
"type": "data"
},
{
"amount": "Unlimited",
"unit": "Units",
"type": "text"
},
{
"amount": "Unlimited",
"unit": "Minutes",
"type": "voice"
}
],
"usages": [
{
"amount": "0",
"unit": "Megabytes",
"type": "data"
},
{
"amount": "n/a",
"unit": "Units",
"type": "text"
},
{
"amount": "n/a",
"unit": "Minutes",
"type": "voice"
}
],
"isExpired": false,
"correlationId": null,
"operationResult": false
}
]
My tests passed as Name in not null here. But if response comes blank than I am getting type error. I wanted to show proper assertion fail error as name is null here (as name is not present in the response)
sometime name is part of response and sometime it does not part of response. so when it does not part of rersponse I would like to show Assertion error instead of type error. That is my requiement.
If it’s part of the response, it will have a value. If it’s not part of the response it’s undefined because it’s not there.
pm.test("Verify the status and name" , function () {
var jsonData = pm.response.json();
pm.expect(jsonData[0].name, 'Name is not available').to.not.be.undefined;
});
aahh…Dany is right pm.expect(jsonData[0].name, 'Name is not available').to.not.be.undefined; do not leads you anywhere… you need change your scope? what you what to test for jsonData[0].name ?
Yes … I want to test jsonData[0].name. If we have Data it should be validated successfully but if we have empty response, it should throw Assertion error ,not Type error.
Type error in the response misleading to more on script error.
To check if response JSON object contains key XYZ:
pm.test("respons has asset key", function() {
pm.expect(pm.response.json()).to.have.property('name') ;
});
Then you can add additional assertions:
pm.test("Name is a string", function() {
pm.expect(pm.response.json().name).to.be.a('string');
});
pm.test("Model match respons - name", function() {
pm.expect(pm.response.json().name).to.equal("U.S. Roaming Pass");
});
Edit:
If you have more than a ‘name’ to check:
pm.test("Check if response contains all the keys", function () {
pm.expect(pm.response.json()[0]).to.be.an('object').that.has.all.keys('id', 'name');
});
i have api response, were my values return "“0” , can we able to verify these value , without using below code. do we have any alternate option to verify this values in Loop, if so how to do it.
I’m not really sure what you’re asking - are you able to expand on what’s currently not working for you?
Can you also post the raw response rather than a cut down image of it, it makes it super difficult to understand the references you have in those expect statements.
Hi Danny,
I am facing a similar situation, I have to write a test based on status code and json body, where expected status code=200 and JSON response body should have some attributes.
I am getting the empty json body ( response body:{}) like this: