2

I have a scenario where a portion of the response arrays is the response from a child API. child API response looks like below, but there is no specific order. And I need to check whether the child API response is present in the parent API(irrespective of the order of the elements in the child API). I followed this Karate - Match two dynamic responses thread but its not working in my case.

* def response1 =

    """
   {
    "array1": [
        {
            "element": {
                "id": "A1",
                "array11": [
                    {
                        "uid": "u123",
                        "gid": [
                            "g1"
                        ]
                    }
                ]
            }
        },
        {
            "element": {
                "id": "A2",
                "array11": [
                    {
                        "uid": "u124",
                        "gid": [
                            "g2"
                        ]
                    }
                ]
            }
        }
    ]
}
"""
* def response2 =

    """
 {
    "array1": [
        {
            "element": {
                "id": "A2",
                "array11": [
                    {
                        "uid": "u124",
                        "gid": [
                            "g2"
                        ]
                    }
                ]
            }
        },
        {
            "element": {
                "id": "A1",
                "array11": [
                    {
                        "uid": "u123",
                        "gid": [
                            "g1"
                        ]
                    }
                ]
            }
        }

    ]
}
    """
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
JCK
  • 129
  • 3
  • 11

1 Answers1

2

This is a one liner :)

* match response2.array1 contains response1.array1

Guess what, you don't have to match pure JSON all the time, using child-sections is fine.

But also read this specific part of the docs: https://github.com/intuit/karate#contains-short-cuts

And this example: https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/graphql/graphql.feature

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thanks @Peter Thomas. In my case I need to check the size of the child api response, if the size of the array >1 then only I need to match those elements in the parent API response. If child API returns only one array, then that wont be populated in parent API. So I have made a JS function to check the responseStatus and the size of the child api response. How can I user `contains` in JS function? – JCK Feb 17 '20 at 15:25
  • @JCK Karate does not encourage you to do `match` or `contains` in JS, this is deliberate. you seem to be over-complicating your test. maybe it is time you ask a new question, keep it specific, and please try to use simpler examples and respect the time of people here trying to help you – Peter Thomas Feb 17 '20 at 15:27
  • I am not over-complicating I did not find a way in Karate to dot it so I used JS. Is there a way to do it in karate? – JCK Feb 17 '20 at 15:35
  • @JCK just my opinion. anyway, I don't understand your question, so I suggest you ask a new question with a *specific* example. or wait for someone else to provide anything I missed – Peter Thomas Feb 17 '20 at 16:03