Karate - Conditional logic with string and array

这一生的挚爱 提交于 2021-02-08 07:25:21

问题


This question derives from this question asked earlier. I am making a Soap request, and I am receiving the response as either an Array, or String.

[print] [   "M4205N",   "M4206U" ]

[print] M5967H

When I get the response as an array, I figured out how to loop through it, and pass the values to another request. However, sometimes the response will come back as a single Code, and it will be returned as a String. In that case, I cannot perform the same logic as I did with the array. I've read about Karate conditional logic, but I cannot figure out how to make it do what I want.

I want to do something like this: If the response is returned as a String, then call this method with the one value. If the response is returned as an array, then call this method and pass each value from the array.

This is one of the ways I had in mind, but it does not work because of the Type difference:

* def memberCodes = memberCodes.size() > 1 ? karate.mapWithKey(memberCodes, 'memberCode') : {}
* def result = call read('OtherRequest.feature') memberCodes

This works if the response is returned as an array, but obviously if it's returned as a String it will break.

What is the right way to perform this conditional logic? Also, please refer to my previous question for more context if needed. Thanks!


回答1:


Interesting. This check should work for testing if (not) a string:

* def memberCodes = typeof memberCodes != 'string' ? karate.mapWithKey(memberCodes, 'memberCode') : {}

Also refer this somewhat related question: https://stackoverflow.com/a/58543843/143475



来源:https://stackoverflow.com/questions/59037707/karate-conditional-logic-with-string-and-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!