Template Request Scenario Outline empty secondary array object causes invalid JSON to form

Viewed 38

I am trying to formulate a template request which can be used via a scenario outline which will be used to test the API without the need for many payloads to be constructed & maintained.

But I've struck a problem when an array is included in the request which contains different optional blocks - that if left empty cause an invalid JSON to be formed and sent.

How can I have an array set correctly (disclude the ",{}" when there is the absence of values from in the scenario outline?

Background: * def template_request = read('./request/request.json')

 Scenario Outline: blah
 * def request_payload = karate.filterKeys(template_request, <request_filter>)

Given request request_payload
When method post

Examples:
  |denominationType1   |amount1|denominationType2 |amount2 |request_filter   |
  |NOTE                |10     |                  |        | 'depositDetail' |
  |NOTE                |10     |COINS             |20      | 'depositDetail' |
                                                                

The request sent from the first row of tbl (which has empty value for denomination type 2 (COINS) is: **See below the second array object which is still set (albeit with no values - which is as per tbl) -> the comma and curly braces , {} which is setting the second array is causing the problem

{
 "depositDetail": {
   "denomination": [
    { 
     "amount": "10",
     "denominationType": "NOTE"
    },
    {
    }
]

}

The request for the second row is fine

{
 "depositDetail": {
   "denomination": [
    { 
     "amount": "10",
     "denominationType": "NOTE"
    },
    {
     "amount": "20",
     "denominationType": "COINS"
    }
]

}

requestTemplate.json

{
 "depositDetail": {
  "denomination": [
   {
    "denominationType": ##(denominationType1),
    "count": ##(count),
    "amount": ##(amount1)
   },
   {
    "denominationType": ##(denominationType2),
    "amount": ##(amount2)
  }
]

} }

I am having no luck with filters or functions - could you please help me.

1 Answers

You still haven't provided the template. Anyway, this is how I would do it. You can try it and see it work.

Scenario Outline:
* def payload = { denomination: [] }
* if (type1) payload.denomination.push({ type: type1, amount: amount1 })
* if (type2) payload.denomination.push({ type: type2, amount: amount2 })

* url 'https://httpbin.org/anything'
* request payload
* method post

Examples:
 | type1  | amount1 | type2 | amount2 |
 | NOTE   | 10      |       |         |
 | NOTE   | 10      | COIN  | 20      |

But even the above is not elegant in my opinion. I recommend you don't overcomplicate your tests and just do something like this:

Scenario Outline:
* def payload = {}
* payload[key] = items

* url 'https://httpbin.org/anything'
* request payload
* method post

Examples:
| key  | items!                                                       |
| foo  | [{ type: 'NOTE', amount: 10 }]                               |
| bar  | [{ type: 'NOTE', amount: 10 }, { type: 'COIN', amount: 20 }] |
Related