如何遍历空手道响应数组并将其传递到另一个Web服务响应的json路径中 [英] How to loop through karate response array and pass this in json path of another web service response

查看:50
本文介绍了如何遍历空手道响应数组并将其传递到另一个Web服务响应的json路径中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是空手道版本0.8.0.1,我想执行以下步骤来测试一些响应.

I am using Karate version 0.8.0.1 and I want to perform following steps to test some responses.

  1. 我进行了获取Web服务1"操作
  2. 使用jsonpath从$ Web服务1的响应中找到货币值:$ .currencies
  3. 第2步给我以下结果:["USD","HKD","SGD","INR","GBP"]
  4. 现在我将Get方法用于Web服务2
  5. 从Web服务2的响应中,我想获得带有json-path的price字段的值,如下所示(传递上面第3步中的值):

  1. I make a Get to web service 1
  2. find the value for currencies from the response of web service 1 using jsonpath: $.currencies
  3. Step 2 gives me following result: ["USD","HKD","SGD","INR","GBP"]
  4. Now I use Get method for web service 2
  5. From the response of web service 2 I want to get the value of price field with json-path something like below(passing the values from step 3 above):

  • $.holding [?(@.currency =='USD')].price
  • $.holding [?(@.currency =='HKD')].price
  • $.holding [?(@.currency =='SGD')].price
  • $.holding [?(@.currency =='INR')].price
  • $.holding [?(@.currency =='GBP')].price

因此有很多货币,但是我只想验证Web服务1返回的货币的价格(它将始终是随机的),并将其传递给Web服务2的输出以获取价格. 一旦获得价格,我便会将每个价格值与DB返回的值进行匹配.

So there are so many currencies but I want to verify price for only the currencies returned by web service 1(which will be always random) and pass it on to the the output of web service 2 to get the price. Once i get the price I will match each price value with the value returned from DB.

我不确定是否有任何简单的方法可以将服务1返回的值一个接一个地传递到服务2的json-path中.这样做的任何建议都会有所帮助,因为我将要自动化的大多数Web服务都是这种情况.

I am not sure if there is any simple way in which I can pass the values returned by service 1 into the json-path of service 2 one by one and get the results required. Any suggestions for doing this will be helpful As this will be the case for most of the web services I will be automating.

推荐答案

在空手道中有多种方法可以做到这一点.以下应该为您提供一些指导.请注意,使用match each时如何使用魔术变量_$.而且,由于您可以引用范围内的任何其他JSON,因此您有一些非常强大的选项.

There are multiple ways to do this in Karate. The below should give you a few pointers. Note how there is a magic variable _$ when you use match each. And since you can reference any other JSON in scope, you have some very powerful options.

* def expected = { HKD: 1, INR: 2, USD: 3}
* def response1 = ['USD', 'HKD', 'INR']
* def response2 = [{ currency: 'INR', price: 2 }, { currency: 'USD', price: 3 }, { currency: 'HKD', price: 1 }]
* match response2[*].currency contains only response1
* match each response2 contains { price: '#(expected[_$.currency])' }

您可能已经了解了如何 call循环,这对于您的特定用例可能是必需的.还有一个难题可能是-将任何JSON数组转换成Karate希望在循环中调用功能文件所期望的形式非常容易:

You probably already have seen how you can call a second feature file in a loop which may be needed for your particular use case. One more piece of the puzzle may be this - it is very easy to transform any JSON array into the form Karate expects for calling a feature file in a loop:

* def response = ['USD', 'HKD', 'INR']
* def data = karate.map(response, function(x){ return { code: x } })
* match data == [{code: 'USD'}, {code: 'HKD'}, {code: 'INR'}]

这篇关于如何遍历空手道响应数组并将其传递到另一个Web服务响应的json路径中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆