vdrulerz
vdrulerz

Reputation: 264

Karate API framework is there any way to iterate through the child values

I am getting 90 cent success with my approach but when the response has got multiple entries in one of the child key then the logic gets failed and I am not able to put one common logic in place which would run for all the cases.

The response sample is

{
  "items": [
             {
              "id":1,
              "name": "John",
              "sections": [
                          { 
                           "id":1,
                           "description": "John smith" 
                          }
                      ]
           }
         ]
}

Now my use case says you search for John text and then items array would contains many objects whose items.name or items.sections.description should contains the "John" keyword

The matching logic which I have put is working fine because I am iterating through items[].name and items.sections[].description

The main challenge comes when the sections[*].description contains the multiple sections like below

{
  "items": [
             {
              "id":1,
              "name": "John",
              "sections": [
                          { 
                           "id":1,
                           "description": "John smith" 
                          },
                          { 
                           "id":1,
                           "description": "remain smith of the first object" 
                          }
                      ]
           }
         ]
}

The logic should now work on items[].name or items.sections[].description (multiple entries of sections[*].description)

Problem I am facing is when I am iterating items[].name & items[].sections[*].description

It gives me all the names and all the sections.description in separate arrays what I want is that it should give me one by one.

For example first result set should give me below

[
"John"
]

and 

[
"John smith"
"remain smith of the first object" 
]

So that I can run my existing logic to check whether John is available or not. Currently my logic runs on first entry of the description and it does not check the next entry or section.description that is the reason below matching object is failing because "john" is present in the second entry of the description

{
  "items": [
             {
              "id":11,
              "name": "SMITH",
              "sections": [
                          { 
                           "id":11,
                           "description": "SMITH" 
                          },
                          { 
                           "id":11,
                           "description": "JOHN Carter" 
                          }
                      ]
           }
         ]
}

The matching logic which I am currently using is -

* def matchText = 
"""
function (nameArr, sectionArr, matchingWord)
{
for(var i = 0; i < nameArr.length; i++)
var regEx = new RegExp(matchingWord, 'gi')
var nameMatch = nameArr[i].match(regEx)
var secMatch = sectionArr[i].match(regEx)
if (nameMatch ==null && secMatch == null) {
return false;
}
}
return true;
}
"""
* def getName = get response.items[*].name
* def getDescription = get response.items[*].sections[*].description
* assert matchText(getName,getDescription,'john')

So this logic works when you have same length in name & sections.description but when sections.description has got multiple arrays then it does not iterate correctly. That was the only reason I wanted to treat the name as one object and sections.description as another even when there will be multiple child ids in it.

Upvotes: 0

Views: 1674

Answers (1)

Neodawn
Neodawn

Reputation: 1096

Sample Code:

Feature: Validation

    Scenario:
        * def resp =
            """
            {
                "items": [
                    {
                        "id": 11,
                        "name": "JOHN",
                        "sections": [
                            {
                                "id": 11,
                                "description": "SMITH"
                            },
                            {
                                "id": 11,
                                "description": "JOHN Carter"
                            }
                        ]
                    }
                ]
            }
            """
    * def names = []
    * def fun =
        """
        function(x){ 
            var json  = x.sections; 
            var temp = x.name
            for(var i = 0; i < json.length; i++) {
                var obj = json[i];
                temp = temp + "," + obj.description
            }                
             karate.appendTo(names, temp);
            }
        """
    * def items = get resp.items[*]
    * karate.forEach(items, fun)
    * match each names == '#regex .*JOHN.*'

Upvotes: 1

Related Questions