0

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!

noobcooder
  • 31
  • 3

1 Answers1

0

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.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248