Asserting and using conditions for an array response in Karate

First, you are encouraged to write static expected results in tests. That said there are multiple ways to do this, here’s one:

* def failedSchema = { xml: '#string', formErrors: '#array' }
* def isValid = function(x){ if (x.status == 'Receipted') return x.response == null; return karate.match(x.response, failedSchema).pass }
* match each response.itemList == '#? isValid(_)'

Here’s another example: https://stackoverflow.com/a/62567412/143475

There are other ways to loop in Karate, but not really designed for matching: https://github.com/intuit/karate#loops

Here’s an extreme example involving JSON transformation to make it easier to match: https://stackoverflow.com/a/53120851/143475

Also refer: https://github.com/intuit/karate#conditional-logic

Leave a Comment