空手道框架重试,直到无法正常工作 [英] Karate framework retry until not working as expected

查看:99
本文介绍了空手道框架重试,直到无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Karate框架与JUnit一起使用.

I'm using Karate framework with JUnit.

使用此功能:

Given path 'save_token'
And request
"""
{
  "token": "test_token"
}
"""
And retry until response.tokens ==
"""
[
    "test_token"
]
"""
When method POST

我有这个例外:

java.lang.ArrayIndexOutOfBoundsException: 1
    at com.intuit.karate.core.MethodMatch.convertArgs(MethodMatch.java:60)
    at com.intuit.karate.core.Engine.executeStep(Engine.java:141)
    at com.intuit.karate.core.ScenarioExecutionUnit.execute(ScenarioExecutionUnit.java:171)

response.tokens列表为空时:

When response.tokens list is empty:

{
    "tokens": []
}

我不明白为什么==在这种情况下不起作用(它应该返回false,并继续重试).

I don't understand why == does not work in this case (it should return false, and keep retrying).

提前谢谢!

推荐答案

retry until 表达式必须是纯JavaScript,并且不支持特殊的空手道匹配关键字(例如contains),并且您不能像尝试的那样进行深度等于",因为这在JS中是不可能的.

The retry until expression has to be pure JavaScript and the special Karate match keywords such as contains are not supported, and you can't do a "deep equals" like how you are trying, as that also is not possible in JS.

还要注意,支持JsonPath,这意味着*..不能出现在表达式中.

Also note that JsonPath is not supported, which means * or .. cannot appear in the expression.

因此,如果您的答复为{ "tokens": [ "value1" ] },则可以执行以下操作:

So if your response is { "tokens": [ "value1" ] }, you can do this:

And retry until response.tokens.contains('value1')

或者:

And retry until response.tokens[0] == 'value1'

要进行实验,您可以尝试使用以下表达式:

To experiment, you can try expressions like this:

* def response = { "tokens": [ "value1" ] }
* assert response.tokens.contains('value1')

在运行时,当轮询时响应尚未准备好时,您可以使用JS来处理条件:

At run time, you can use JS to take care of conditions when the response is not yet ready while polling:

And retry until response.tokens && response.tokens.length

实际上,执行上述操作的一种更优雅的方法如下所示,因为karate.get()正常处理JS或JsonPath评估失败并返回null:

actually a more elegant way to do the above is shown below, because karate.get() gracefully handles a JS or JsonPath evaluation failure and returns null:

And retry until karate.get('response.tokens.length')

或者,如果您正在处理XML,则可以使用karate.xmlPath() API:

Or if you are dealing with XML, you can use the karate.xmlPath() API:

And retry until karate.xmlPath(response, '//result') == 5

如果您真的想使用空手道match语法的功能,则可以使用 JS API :

And if you really want to use the power of Karate's match syntax, you can use the JS API:

And retry until karate.match(response, { tokens: '##[_ > 0]' }).pass

请注意,如果您有更复杂的逻辑,则可以随时将其包装到可重用的函数中:

Note that if you have more complex logic, you can always wrap it into a re-usable function:

* def isValid = function(x){ return karate.match(x, { tokens: '##[_ > 0]' }).pass }
# ...
And retry until isValid(response)

最后,如果以上方法均无效,则始终可以切换到自定义轮询例程:

Finally if none of the above works, you can always switch to a custom polling routine: polling.feature

也请参见此答案,以获取有关如何使用karate.filter()而不是JsonPath的示例: https://stackoverflow.com /a/60537602/143475

also see this answer for an example of how to use karate.filter() instead of JsonPath: https://stackoverflow.com/a/60537602/143475

这篇关于空手道框架重试,直到无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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