3

Goal: Match the check value is correct for 123S and 123O response in API

First check the value on this location x.details[0].user.school.name[0].codeable.text if it is 123S then check if x.details[0].data.check value is abc

Then check if the value on this location x.details[1].user.school.name[0].codeable.text is 123O then check if x.details[1].data.check is xyz

The response in array inter changes it is not mandatory first element is 123S sometime API returns 123O as first array response.

Sample JSON.

{
    "type": "1",
    "array": 2,
    "details": [
        {
            "path": "path",
            "user": {
                "school": {
                    "name": [
                        {
                            "value": "this is school",
                            "codeable": {
                                "details": [
                                    {
                                        "hello": "yty",
                                        "condition": "check1"
                                    }
                                ],
                                "text": "123S"
                            }
                        }
                    ]
                },
                "sample": "test1",
                "id": "22222"
            },
            "data": {
                "check": "abc"
            }
        },
        {
            "path": "path",
            "user": {
                "school": {
                    "name": [
                        {
                            "value": "this is school",
                            "codeable": {
                                "details": [
                                    {
                                        "hello": "def",
                                        "condition": "check2"
                                    }
                                ],
                                "text": "123O"
                            }
                        }
                    ]
                },
                "sample": "test",
                "id": "11111"
            },
            "data": {
                "check": "xyz"
            }
        }
    ]
}

How I did in Postman but how to replicate same in Karate?

var jsonData = pm.response.json();

pm.test("Body matches string", function () {
    for(var i=0;i<jsonData.details.length;i++){
        if(jsonData.details[i].user.school.name[0].codeable.text == '123S')
        {
            pm.expect(jsonData.details[i].data.check).to.equal('abc');
        }
        if(jsonData.details[i].user.school.name[0].codeable.text == '123O')
        {
            pm.expect(jsonData.details[i].data.check).to.equal('xyz');
        }
    }
});
Maddy
  • 674
  • 1
  • 7
  • 26

1 Answers1

3

2 lines. And this takes care of any number of combinations of lookup values :)

* def lookup = { '123S': 'abc', '123O': 'xyz' }
* match each response.details contains { data: { check: '#(lookup[_$.user.school.name[0].codeable.text])' } }
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Awesome. Thanks Peter. It worked and good to see only 2 lines of code made it work :). Is this documented somewhere or any example in karate demo project? I didn't understand the logic { data: { check: '#(lookup[_$.user.school.name[0].codeable.text])' } } here wanted to understand if its documented. – Maddy Nov 21 '21 at 13:47
  • 1
    @Maddy yes this is a combination of 2-3 concepts but it is all there in the documentation. start here: https://github.com/karatelabs/karate#referring-to-self - and also read this: https://stackoverflow.com/a/59162760/143475 – Peter Thomas Nov 21 '21 at 13:50
  • I have added another issue with same. Please guide on that too. There are many keys in our actual JSON so I have provided the minimal one to show the issue we are having now – Maddy Nov 21 '21 at 15:04
  • 1
    @Maddy sorry. create a new question. remember, I should be able to paste and try it locally. if I can't, you don't get an answer – Peter Thomas Nov 21 '21 at 15:31