重试:在数组内匹配 [英] Retry : match inside an array

查看:27
本文介绍了重试:在数组内匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用重试,以检查答案中是否存在包含给定ID的对象.

I'm trying to use retry, to check if an object containing a given ID is present in the answer.

这是我尝试过的尝试之一:

Here is one of the attempts I made :

Background:
* url 'https://jsonplaceholder.typicode.com'

Scenario: get all users and then get the first user by id

Given path 'users'
When method get
Then status 200

* def first = response[0]

Given path 'users'
And retry until response[0].id == first.id
When method get
Then status 200

Given path 'users'
And retry until response[*].id == first.id
When method get
Then status 200

第一个重试有效,但是第二个重试会产生错误,因为[*]无法与retry命令一起使用.但是,当使用retry而不是javascript函数时,如何检查first.id是否存在于响应数组中的至少一个对象中?

The first retry works, but the second one generates an error because [*] cannot be used with the retry command. But how can I check that first.id is present in at least one of the objects in the response array, while using retry and not a javascript function?

推荐答案

是的,表达式必须是纯JS,并且不能混合使用JsonPath.但是,由于您可以定义和重用函数,因此这是一种实现方法:

Yes the expression has to be pure JS and you can't mix JsonPath. But since you can define and re-use functions, here is one way to do it:

* def response = [{ id: 1 }, { id: 2 }, { id: 3 }]
* def hasId = function(id){ return karate.filter(response, function(x){ return x.id == id }).length != 0 }
* assert hasId(1)
* assert !hasId(9)

所以现在这应该可行:

And retry until hasId(first.id)

请注意,有一个很少使用的 karate.match() 也可能有效,但是除非您使用快捷方式,否则它不直接支持contains.

Note that there is a rarely used karate.match() that may also work, but it doesn't support contains directly unless you use the short-cuts.

这篇关于重试:在数组内匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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