简体   繁体   中英

Postman: Wanted to validate data values getting from API in JSON format

I wanted to check some combinations of validations for values I am getting from API request in like if "tracked": "Yes" then analytics > "transitTime": 239 should be present.

The snippet of API response:

{
  "status": 200,
  "data": [{
    "id": 107267,
    "branchId": "22",
    "status": "1",
    "googleETA": "2018-02-01 20:44:51",
    "runDate": "2018-01-29",
    "runOfTheDay": "1",
    "ATD": "2018-01-29 14:02",
    "simCarrier": "Vodafone",
    "pingStatus": "Ok",
    "driverName": "test",
    "shipperCompanyId": "007",
    "ETA": "2018-01-30 12:31:00",
    "locationSource": "sim",
    "created_at": "2018-01-29 07:05:54",
    "updated_at": "2018-01-29 12:35:40",
    "tracked": "Yes",
    "analytics": {
      "loadingTime": 55.62,
      "unloadingTime": 0,
      "transitTime": 239
    },
    "trackingStatusAtClosure": {
      "Origin": "No",
      "Transit": "No",
      "Destination": "No"
    }
  }]
}

This is a very basic check and I would advise against using it anywhere other than practising in a local environment.

pm.test("Basic Check", () => {
    var jsonData = pm.response.json().data
    for(i = 0; i < jsonData.length; i++) {
        if(jsonData[i].tracked === 'Yes') {
            pm.expect(jsonData[i].analytics.transitTime).to.not.be.null
        } else {
            console.log('Tracked does not equal Yes')
        }
    }
})

As your data only has one item in the data array you don't really need to add the for loop but I added it in for you so you can see that the same check would run if you have more than one item. If the tracked property is 'Yes' then the test is checking if analytics.transitTime not null. Like I said, its a basic check and it's not doing a great deal but hopefully that will give you the information you need to get started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM