1

I am using karate for my API testing, and my current requirement is I need to write just one scenario and handle two responses as part of it. What I mean by this is my response sometimes returns a json object and sometimes returns a array of json object.

For Ex: First execution of endpoint Returns Response --

{ 
  "id": 123,
  "Name: "mytest",
  "Loc: "United States"
}

And during the next execution, the same endpoint Returns Response --

[
  { 
    "id": 123,
    "Name: "mytest",
    "Loc: "United States"
  },
  { 
    "id": 456,
    "Name: "mytest1",
    "Loc: "United Kingdom"
  }
]

My use case is to compare the keys available should be ['id','name','Loc']

Initially it was just returning a json so I was using karate.keysOf(response) and it was working fine. Now I need to handle both my usecases inside one scenario itself as it could return a json or an array of json and I need to check keys present -- if json then check just once and if array then check for all json objects inside array. I know there is way to do it separately but not sure if this could be clubbed into one use case Is there a way where I can get responseType to be either a json or an array and write two separate code with conditions or any way in which my use case could be handle.

Any help would be appreciated. Thank you in advance.

Thomas Freudenberg
  • 5,048
  • 1
  • 35
  • 44

1 Answers1

2

you can handle this using schema,

# schmea for single json object
* def schema = {'id':'#present', 'Name' : '#present', 'Loc' : "#present"}

# determine if it an Json Array or object and set expected schema
* def expectedSchema = (karate.match(response, "#array").pass) ? "#[] schema" : schema

* match response == expectedSchema
Babu Sekaran
  • 4,129
  • 1
  • 9
  • 20
  • Thanks for your response. What if I don't want to compare the schema and just validate if the keys are present in my json array or object. Is that possible? – automation.ninja.08 Jun 12 '20 at 23:25
  • @automation.ninja.08 in my (informed) opinion, you have fallen into this trap: https://stackoverflow.com/a/54126724/143475 – Peter Thomas Jun 13 '20 at 02:27
  • Thanks @PeterThomas for your response. I totally get your point around this. But one of our API behaves in this manner. So is there any possibility of adding any conditional logic and verifying both the scenarios based on any condition. I understand that for verifying both there will be separate piece of JavaScript functions. But I am struggling with the condition, as api is not returning any type – automation.ninja.08 Jun 13 '20 at 12:29
  • 1
    @automation.ninja.08 a) see if this answer helps: https://stackoverflow.com/a/58543843/143475 b) search the docs for "conditional logic" c) use babu's answer it gives you a hint – Peter Thomas Jun 13 '20 at 12:44
  • @peterthomas - thanks, will try this and let you know if that works. – automation.ninja.08 Jun 13 '20 at 12:50
  • Thanks for all your help @peterthomas and babu, I managed to write two different scenarios instead of clubbing both into one – automation.ninja.08 Jun 24 '20 at 21:43