Stumped: Array Validation

Hello folks,

Newbie here, and I’ve been reading through lots of posts to try and suss this out.

I want quite a lot of granular checks and be able to run my test from a JSON data file iteratively across a number of arrays of IDs.

To help in this, I started messing around with the Google Places Details Request (as I thought it would be a good test bed to play around in).

Firstly, Postman is doing a great job of firing off the singular request (though I still have some issues defining and re-using variables… i’ll suss that). I get a request back for a PlaceID and it presents me with a bunch of arrays containing photo information.

Example Request:

https://maps.googleapis.com/maps/api/place/details/json?placeid={{PlaceID}}&fields=photos&key={{APIkey}}

Example Response:

{
    "html_attributions": [],
    "result": {
        "photos": [
            {
                "height": 1184,
                "html_attributions": [
                    "<a href=\"https://maps.google.com/maps/contrib/106645265231048995466/photos\">Malik Ahamed</a>"
                ],
                "photo_reference": "CmRaAAAAs2lgP3MuzuqQ4_1lWO8jpfxNETCehlettG2XuLywcbPgTxJxWlRbWruzAv95ZNFQhaHKdoFkIlmrg7rRcC348395AjYh-xbC2PWQ-nwupKt5r4VbI7OxEgj-S6JyxxpuEhDSlYIPtvYI3p6Z3CppKiuvGhTfRJS670SBmZkBxjhq6ijB1C6ovw",
                "width": 1776
            }, yadda yadda yadda

I have a test defined:

pm.test('Confirm Valid Photo Responses from Google Place Details', function () {
    var responseJSON = pm.response.json();
    pm.expect(responseJSON.result).to.be.ok;
        console.log("JSON can be read and Results Present")
    // Check for Photos
    pm.expect(responseJSON.result.photos).to.be.ok;
        console.log("JSON can be read and Photos Present")    
    // Check for Photos height
    pm.expect(responseJSON.result.photos).to.include('height');

})

Everything until the include check for height works fine, almost like it won’t/can’t read the content of items in the photos array.

First thing in my head is the only way to do a test this way is to say .result.photos.height)to.include('1184') but that’s not what I really want to do… I don’t want to be that explicit

The test itself is supposed to do, though the collection runner and a data file containing many place ids, is iterate through each of those IDs and validate whether the photo_reference is returned, store the value returned if it is and then keep going (whether valid or not) until there are no more IDs left to check.

That way, I’ll get a list of all place ids that contain photo references or not :smiley:

As I’m getting stumped already just trying to validate that 'Photos' includes 'height', i’m pretty sure I can’t then do the next bit :slight_smile:

If I can solve why I can’t do that, then I should be able to set a new Var for PhotoRef that stores the photo_reference ID returned and carry on with the rest. Then I can apply this to my actual problem and stop playing around with this example lol

Can anybody help me suss it out? It’s probably REALLY obvious, I’m just not getting it.

NOTE: I don’t care about schema validation as such, i’m literally just stepping through checks that it can read the responseBody and then I’ll use a PASS for photo_reference being present to then confirm it has content and store that content to display to the console.

Thanks for any help you can provide, I’m usually just not getting a simple thing and it forces me to come talk to lovely helpful people who tell me “You so dumb, try this noobzorz” … and I’m happy for that to be the case :wink: … .include is probably not what I want because that only does content of elements/objects and not checks for elements/objects that might contain data? that’s my guess anyways right now…

Cheers! Russ

Hey Russ,

Thanks for including the example response and your test code as it makes it a lot easier to help!

The issue here is that you’re testing the photos array:

pm.expect(responseJSON.result.photos).to.include('height');

However, note how the data structure is different to this. photos is an array, not an object - height is not a key immediately inside photos. Instead you’re wanting to check within the first item of the array, which is the object containing height. To access an item within an array you need to provide it’s index (position):

pm.expect(responseJSON.result.photos[0]).to.include('height');

Let me know how you get on!

Matt

Hey Matt!

Thanks for that, it’s a shame it won’t iterate the array position though… if I wanted to be smarter about it and I’m not 100% sure of the response scheme or ordering (or it simply just changes some time in the future) I’d have to:

  1. know how many arrays are returned

  2. perform the check repeated for all array positions (I.e check 0-10 for ‘height’)

  3. probably some other frustrating thing :wink:

I wonder if there is a way to do that iteration automatically? A little for loop of some kind perhaps…

Ideally, I want this test to confirm if something is present for an array position and if so do something else…

Same is true for photo_reference… if I wanted to confirm it was present (because it’s not a guarantee in the response) but only fail my test if it’s presence is true but it’s content is empty… I can’t see a way to do that…

Thanks for trying to answer though!

It’s not your typical newbie question I’m sure, it’s oddly complex :wink:

You could totally do that, as you say you just need to wrap it up in a loop:

for (var i = 0; i < responseJSON.result.photos.length; i++) {
  pm.expect(responseJSON.result.photos[i]).to.include('height');
  pm.expect(responseJSON.result.photos[i]).to.include('photo_reference');
}

This will loop over every array item and check whatever you’d like… I haven’t looked, but suspect there might actually be a nicer way of doing this. Postman uses Chai under the hood so check out the possibilities/examples here:

https://www.chaijs.com/api/bdd/

2 Likes

You’re a legend Matt, thank you :slight_smile:

See, the principles I get… the know-how not-so-much :wink: