mercredi 24 juin 2020

Asserting and using conditions for an array response in Karate

I've got a request that returns a list of responses in two possible structures, depending on the 'status'.

{
    "listSize": 2,
    "itemList": [
       {
            "id": ,
            "Name": "",
            "submittedOn": "",
            "Reference": null,
            "status": "Receipted",
            "response": null
        },
        {
            "id": 12345,
            "submittedOn": "",
            "Reference": null,
            "status": "Failed",
            "response": {
                "xml": "",
                "formErrors": [
                    {
                        "error_type": "",
                        "error_location":"", 
                        "error_message": "",
                    }
                ]
            }
        }, 
     ]
}

I need to check the structure for the status being either 'Receipted' or 'Failed'. In Java I would use a for loop and an if statement within it to check the response field with different criteria depending on the 'status' field. (Example below)

for (int i = 0; i < response.length; i++){
   if (response[i].status.equals("receipted")){
      //do something
   }
   else{ //failed
      //do something else
   }
}

How could I achieve something similar in Karate? Should I use a Java Helper?

Aucun commentaire:

Enregistrer un commentaire