API每次在URL中发送不同ID的循环呼叫

发布于 2025-02-01 11:08:09 字数 1002 浏览 2 评论 0原文

因此,我尝试了在线提供的可用解决方案,但我不知道我在哪里犯错。我想在一个数组中获取我从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过气美图社 2025-02-08 11:08:09

首先,请阅读文档。第二,请不要手工写JS环路,您永远不需要在空手道中: https:// github github .com/karatelabs/karate#循环

由于您对空手道有一些误解,因此我在下面重新编写了您的测试。我将其留给您,以研究其工作原理。您应该在文档中引用的部分是: https://github.com/karatelabs /空手道#数据驱动器

Feature: using a response json array to loop

  Background:
    * def urlBase = 'https://restful-booker.herokuapp.com/booking'

  Scenario: get a list of booking ids
    * url urlBase
    * params { checkin: '2020-01-01', checkout: '2021-01-01' }
    * method get
    * status 200
    * match each response == { bookingid: '#number' }
    # loop over all ids returned and get booking
    * call read('@called') response

  @ignore @called
  Scenario:
    * url urlBase
    * path bookingid
    * header Accept = 'application/json'
    * method get
    * status 200
    * match response contains { firstname: '#string', lastname: '#string' }

如果您真的愿意,可以使用方案大纲,但我不建议使用此特定流程。

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

Feature: using a response json array to loop

  Background:
    * def urlBase = 'https://restful-booker.herokuapp.com/booking'

  Scenario: get a list of booking ids
    * url urlBase
    * params { checkin: '2020-01-01', checkout: '2021-01-01' }
    * method get
    * status 200
    * match each response == { bookingid: '#number' }
    # loop over all ids returned and get booking
    * call read('@called') response

  @ignore @called
  Scenario:
    * url urlBase
    * path bookingid
    * header Accept = 'application/json'
    * method get
    * status 200
    * match response contains { firstname: '#string', lastname: '#string' }

You can use a Scenario Outline if you really want, but I don't advise it for this particular flow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文