空手道API测试:无法打印我想从IF语句中获得的响应数组,并希望将其传递到下一个API呼叫请求URL

发布于 2025-01-21 02:43:47 字数 620 浏览 1 评论 0原文

步骤1:我在一个变量中存储的一个API调用的响应中得到一个变量

    * def ID = response.id

:在第二个API调用中,响应是带有多个字段的JSON对象的数组,在方法响应后,我试图通过' ID'内部如果条件,并且根据条件,我期望数组(将会为空) API的示例响应[{City:“ NY”,代码:41049,ID:ABC-123,名称:“美国”},{city:“ ot”,代码:42049,ID:ABC-123,name:“加拿大:” “}

  • def idarray = [] 并评估(var i = 0; i< response.length; i ++)如果(响应[i] .id =='+id+id+'&&& wespons [i] .name =='usic'')idarray。添加(响应[i] .cityname) 然后打印idarray(获取null值)

步骤3:我想通过下一个API调用urlPath(我尝试)

和Path dynamicform/' + iDarray +'/form1(在此处获取null值) 中的下一个API调用urlPath中的特定索引的打印数组值对于 +iDarray +) 当方法获得时 然后状态200

step 1 : I am getting one variable in response of one API call which I stored in one variable

    * def ID = response.id

step 2 : In the second API call, response is the array of json object with multiple fields, after post method response I am trying to pass 'ID' inside if condition and based on if condition I am expecting array ( which is coming empty)
example response of API [ {city: "NY", code:41049, id: ABC-123, name: "USA"},{city: "OT", code:42049, id: ABC-123, name: "CANADA"}

  • def idarray = []
    And eval for(var i=0;i<response.length;i++) if (response[i].id == '+ ID +' && response[i].name == 'USA') idarray.add(response[i].cityname)
    Then print idarray ( getting null value)

step 3: I want to pass step 2 printed array value of specific index in next API call urlpath (I am trying like)

And path dynamicform/'+ idarray +'/form1 ( getting null value here for +idarray+)
when method get
then status 200

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南月 2025-01-28 02:43:47

请不要使用 JS 循环。它们会给您带来麻烦,请参阅文档以获取更好的方法:https://github.com/karatelabs /karate#loops

另外,JsonPath 会在 90% 的情况下为您提供所需的内容,而无需循环。请仔细阅读文档并查找 JsonPath

* def ids = $[*].id
* match ids == ['ABC-123', 'ABC-123']

我还怀疑将 ids 连接到 path 是否会按照您期望的方式工作。如果需要的话请寻求懂 JS 的人的帮助。

也许您正在寻找这个: https://github.com/karatelabs/karate#jsonpath-filters

* def fun = function(x){ return x.name == 'USA' } 
* def filtered = karate.filter(response, fun)
* def id = filtered[0].id

Please don't use JS loops. They get you into trouble, please refer to the docs for better approaches: https://github.com/karatelabs/karate#loops

Also JsonPath will give you what you need in 90% of the situations without needing loops. Please read the docs carefully and look for JsonPath.

* def ids = $[*].id
* match ids == ['ABC-123', 'ABC-123']

I also doubt whether concatenating ids into the path will work the way you expect. Please take the help of someone who knows JS if needed.

Maybe you are looking for this: https://github.com/karatelabs/karate#jsonpath-filters

* def fun = function(x){ return x.name == 'USA' } 
* def filtered = karate.filter(response, fun)
* def id = filtered[0].id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文