使用匹配响应 [*] 找到的数组中对象的索引
Index of object in array found with match response[*]
是否可以获取a的索引值
* match response.Services[*] contains { "Service": "xyz"}
我希望稍后重用它以对同一服务对象进行更具体的测试。
这里提到了一个 __loop
变量 here 但我想我真的不明白如何应用它。
我喜欢你的问题,你肯定是在将空手道推向极限,我也不得不努力思考才能回答这些问题:)
match
不允许您获得 "found index" 甚至是 "loop position" 想到它 - 可能实际上与 match each
相关 -但我离题了。
这将需要几行额外的代码,我真的认为您需要升级到 0.8.0。如果有帮助,我可以确认没有重大更改(除非您使用独立 JAR)。
新的 karate.forEach()
和 karate.match()
函数允许您对数组执行非常复杂的操作。这里有 2 个例子:
Scenario: karate find index of first match (primitive)
* def list = [1, 2, 3, 4]
* def searchFor = 3
* def foundAt = []
* def fun = function(x, i){ if (x == searchFor) foundAt.add(i) }
* eval karate.forEach(list, fun)
* match foundAt == [2]
Scenario: karate find index of first match (complex)
* def list = [{ a: 1, b: 'x'}, { a: 2, b: 'y'}, { a: 3, b: 'z'}]
* def searchFor = { a: 2, b: '#string'}
* def foundAt = []
* def fun = function(x, i){ if (karate.match(x, searchFor).pass) foundAt.add(i) }
* eval karate.forEach(list, fun)
* match foundAt == [1]
如果实在升级不了,可以自己写一个函数手动循环遍历数组,上面的例子应该可以解决。
是否可以获取a的索引值
* match response.Services[*] contains { "Service": "xyz"}
我希望稍后重用它以对同一服务对象进行更具体的测试。
这里提到了一个 __loop
变量 here 但我想我真的不明白如何应用它。
我喜欢你的问题,你肯定是在将空手道推向极限,我也不得不努力思考才能回答这些问题:)
match
不允许您获得 "found index" 甚至是 "loop position" 想到它 - 可能实际上与 match each
相关 -但我离题了。
这将需要几行额外的代码,我真的认为您需要升级到 0.8.0。如果有帮助,我可以确认没有重大更改(除非您使用独立 JAR)。
新的 karate.forEach()
和 karate.match()
函数允许您对数组执行非常复杂的操作。这里有 2 个例子:
Scenario: karate find index of first match (primitive)
* def list = [1, 2, 3, 4]
* def searchFor = 3
* def foundAt = []
* def fun = function(x, i){ if (x == searchFor) foundAt.add(i) }
* eval karate.forEach(list, fun)
* match foundAt == [2]
Scenario: karate find index of first match (complex)
* def list = [{ a: 1, b: 'x'}, { a: 2, b: 'y'}, { a: 3, b: 'z'}]
* def searchFor = { a: 2, b: '#string'}
* def foundAt = []
* def fun = function(x, i){ if (karate.match(x, searchFor).pass) foundAt.add(i) }
* eval karate.forEach(list, fun)
* match foundAt == [1]
如果实在升级不了,可以自己写一个函数手动循环遍历数组,上面的例子应该可以解决。