I want to know how can I test if an object exists. For example, my API return these things :
"data": [ { "id": 1, "name": "Abu Dhabi", "locale": "AE", "rentWayCountryId": 242, "stations": [ { "id": 2, "rentWayName": "ABU DHABI AIRPORT", "rentWayStationId": "IAEAUH1", "bindExtrasToStationToExtraCategory": [] } ] },
I want to check that data.id exists.
I used the test options in postman and i did this :
var jsonData = JSON.parse(responseBody); tests["Name value OK"] = jsonData.data.id === "1";
Could you tell me which condition should I use to verify only if the data exist?
Thank you very much !!
In Postman, we can write the assertion in many ways. One of the simplest ways is the snippets, which are nothing but a block of codes that have some unique functions inside it and are available in the postman app. Users can easily access the snippets and can get the code inside the tests editor and run the test.
The Postman Body tab gives you several tools to help you understand the response quickly. You can view the body in one of four views: Pretty, Raw, Preview, and Visualize. in the results pane. You can also place your cursor in the response and select ⌘+F or Ctrl+F.
Here is a proper Postman test:
const jsonData = pm.response.json(); pm.test('Has data', function() { pm.expect(jsonData).to.have.property('data'); });
The above will either PASS or FAIL yor postman request based on the presence of the data
property in the response.
To check whether the object is exist or not is equivalent to check whether it is null or not.
if(object){//runs if object is not null}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With