Retry : match inside an array

风流意气都作罢 提交于 2019-12-11 05:39:53

问题


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

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?


回答1:


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)

So now this should work:

And retry until hasId(first.id)

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.



来源:https://stackoverflow.com/questions/53961329/retry-match-inside-an-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!