The retry until https://github.com/intuit/karate#retry-until表达式必须是纯 JavaScript 和特殊的空手道匹配关键字,例如contains
不支持,并且你不能像你尝试的那样做“深度等于”,因为这在 JS 中也是不可能的。
编辑:在 0.9.6 中。从那时起你就可以做一个复杂的match
in JS: https://stackoverflow.com/a/50350442/143475 https://stackoverflow.com/a/50350442/143475
另请注意,JsonPath 是not支持,这意味着*
or ..
不能出现在表达式中。
所以如果你的回答是{ "tokens": [ "value1" ] }
, 你可以这样做:
And retry until response.tokens.includes('value1')
Or:
And retry until response.tokens[0] == 'value1'
为了进行实验,您可以尝试这样的表达式:
* def response = { "tokens": [ "value1" ] }
* assert response.tokens.includes('value1')
在运行时,您可以使用 JS 来处理轮询时响应尚未准备好的情况:
And retry until response.tokens && response.tokens.length
编辑:实际上,执行上述操作的更优雅的方法如下所示,因为karate.get()
优雅地处理 JS 或 JsonPath 评估失败并返回null
:
And retry until karate.get('response.tokens.length')
或者,如果您正在处理 XML,则可以使用karate.xmlPath()
API:
And retry until karate.xmlPath(response, '//result') == 5
如果你真的想利用空手道的力量match
语法,您可以使用JS API https://github.com/intuit/karate#karate-match:
And retry until karate.match(response, { tokens: '##[_ > 0]' }).pass
请注意,如果您有更复杂的逻辑,您始终可以将其包装成可重用的函数:
* def isValid = function(x){ return karate.match(x, { tokens: '##[_ > 0]' }).pass }
# ...
And retry until isValid(response)
最后,如果上述方法都不起作用,您可以随时切换到自定义轮询例程:polling.feature https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/polling/polling.feature
编辑:另请参阅此答案以获取如何使用的示例karate.filter()
而不是 JsonPath:https://stackoverflow.com/a/60537602/143475 https://stackoverflow.com/a/60537602/143475