API每次在URL中发送不同ID的循环呼叫
因此,我尝试了在线提供的可用解决方案,但我不知道我在哪里犯错。我想在一个数组中获取我从API中获得的数组中的ID,并将这些ID在循环中传递给其他API,以获取每个ID的响应,为此我写了以下代码。
Scenario Outline : Fetching booking details of ids via checkin/check out parameter
Given url 'https://restful-booker.herokuapp.com/booking'
And params { checkin : 2016-01-16, checkout : 2020-07-10 }
When method GET
Then status 200
And match response == '#notnull'
* def value = response
* def ids = karate.map(response, function(value){ var i = 0; var id = value[i].bookingid; i++; return id; })
And print 'Ids are' , ids
Given path 'https://restful-booker.herokuapp.com/booking'
And path '<ids>'
* header Accept = 'application/json'
When method GET
Then status 200
Examples:
| ids |
它给出了错误
javax.script.ScriptException: TypeError: Cannot get property "bookingid" of null in <eval> at line number 1
,我可以通过使用,
value[0].bookingid
但在循环中我会遇到错误。任何帮助将不胜感激。提前致谢!
So I tried the available solution provided online but I don't know where I am making a mistake. I am suppose to get the ids in an array that I am getting in response from an api and pass those ids in a loop to other api to get the response of each id for this I have written the below code.
Scenario Outline : Fetching booking details of ids via checkin/check out parameter
Given url 'https://restful-booker.herokuapp.com/booking'
And params { checkin : 2016-01-16, checkout : 2020-07-10 }
When method GET
Then status 200
And match response == '#notnull'
* def value = response
* def ids = karate.map(response, function(value){ var i = 0; var id = value[i].bookingid; i++; return id; })
And print 'Ids are' , ids
Given path 'https://restful-booker.herokuapp.com/booking'
And path '<ids>'
* header Accept = 'application/json'
When method GET
Then status 200
Examples:
| ids |
Its giving the error
javax.script.ScriptException: TypeError: Cannot get property "bookingid" of null in <eval> at line number 1
I am able to get the specific booking id in a variable by using
value[0].bookingid
But in a loop I am getting error. Any help would be appreciated. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,请阅读文档。第二,请不要手工写JS环路,您永远不需要在空手道中: https:// github github .com/karatelabs/karate#循环
由于您对空手道有一些误解,因此我在下面重新编写了您的测试。我将其留给您,以研究其工作原理。您应该在文档中引用的部分是: https://github.com/karatelabs /空手道#数据驱动器
如果您真的愿意,可以使用
方案大纲
,但我不建议使用此特定流程。First, please read the docs. Second, please don't write JS loops by hand, you never need to in Karate: https://github.com/karatelabs/karate#loops
Since you have a few misconceptions about Karate, I've re-written your test below. I leave it to you as an exercise to study how it works. The part you should refer in the docs is this: https://github.com/karatelabs/karate#data-driven-features
You can use a
Scenario Outline
if you really want, but I don't advise it for this particular flow.