空手道-如何在“重试直到"语句期间通过UUID访问数组元素 [英] Karate - how to access an array element by UUID during a 'retry until' statement

查看:106
本文介绍了空手道-如何在“重试直到"语句期间通过UUID访问数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回此JSON响应的终结点:

I have an endpoint which returns this JSON response:

{
  "jobs": [
    {
      "name": "job1",
      "id": "d6bd9aa1-0708-436a-81fd-cf22d5042689",
      "status": "pending"
    },
    {
      "name": "job2",
      "id": "4fdaf09f-51de-4246-88fd-08d4daef6c3e",
      "status": "pending"
    }
  ]

我想重复GET调用此端点,直到我关心的作业("job2")的"status""completed",但我想使用存储在以下变量中的UUID对此进行检查以前的通话.

I would like to repeatedly GET call this endpoint until the job I care about ("job2") has a "status" of "completed", but I'd like to check this by using a UUID stored in a variable from a previous call.

即通过执行以下操作:

#NB: code for previous API call is executed
* def uuidVar = response.jobRef
#NB: uuidVar equates to '4fdaf09f-51de-4246-88fd-08d4daef6c3e' for this scenario

* configure retry = { count: 5, interval: 10000 }

Given path /blah
And retry until response.jobs[?(@.id==uuidVar)].status == 'completed'
When method GET

有人可以为retry until建议正确的语法吗?

Could anyone suggest the correct syntax for the retry until?

我尝试引用很棒的空手道docs&示例(尤其是js-arrays.feature)和有关SO的一些问题(包括以下内容):

I've tried referencing the fantastic Karate docs & examples (in particular, js-arrays.feature) and some questions on SO (including this one: Karate framework retry until not working as expected) but sadly I haven't been able to get this working.

我也尝试按照上面链接中的建议在此处使用karate.match,但没有雪茄.

I also tried using karate.match here as suggested in the link above, but no cigar.

如果我遗漏了一些明显的东西,请提前道歉.

Apologies in advance if I am missing something obvious.

推荐答案

首先,我建议您在Stack Overflow上阅读此答案,它实际上是自述文件的链接,旨在作为权威参考.让我知道是否需要改进: https://stackoverflow.com/a/55823180/143475

First I recommend you read this answer on Stack Overflow, it is linked from the readme actually, and is intended to be the definitive reference. Let me know if it needs to be improved: https://stackoverflow.com/a/55823180/143475

简短的回答,您不能在retry until表达式中使用JsonPath,它必须是纯JavaScript.

Short answer, you can't use JsonPath in the retry until expression, it has to be pure JavaScript.

虽然您可以使用karate.jsonPath()桥接JsonPath和JS,但是JsonPath却很难编写和理解.这就是为什么我建议使用karate.filter()来执行相同的操作,但将步骤分解为简单易读的块的原因.这是您可以在全新的Scenario:中尝试的功能.提示,这是对代码进行故障排除而又不发出任何真实"请求的好方法.

While you can use karate.jsonPath() to bridge the worlds of JsonPath and JS, JsonPath can get very hard to write and comprehend. Which is why I recommend using karate.filter() to do the same thing, but break down the steps into simple, readable chunks. Here is what you can try in a fresh Scenario:. Hint, this is a good way to troubleshoot your code without making any "real" requests.

* def getStatus = function(id){ var temp = karate.filter(response.jobs, function(x){ return x.id == id }); return temp[0].status }
* def response =
"""
{
  "jobs": [
    {
      "name": "job1",
      "id": "d6bd9aa1-0708-436a-81fd-cf22d5042689",
      "status": "pending"
    },
    {
      "name": "job2",
      "id": "4fdaf09f-51de-4246-88fd-08d4daef6c3e",
      "status": "pending"
    }
  ]
}
"""
* def selected = '4fdaf09f-51de-4246-88fd-08d4daef6c3e'
* print getStatus(selected)

因此,如果您预先定义了getStatus,则可以执行以下操作:

So if you have getStatus defined up-front, you can do this:

* retry until getStatus(selected) == 'completed'

请注意,如果您不希望将全部内容压缩到一行,甚至不喜欢从文件中读取,则可以对JS函数使用多行.

Note you can use multiple lines for a JS function if you don't like squeezing it all into one line, or even read it from a file.

这篇关于空手道-如何在“重试直到"语句期间通过UUID访问数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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