1

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.

jbart18
  • 138
  • 7
  • I will help if you can simplify your question. right now it is too much to read and understand. it should be possible for you to explain the need using a simple json and simple table with not more than 2 keys / columns – Peter Thomas Aug 17 '22 at 04:05
  • Hi @PeterThomas - i tried to clean it up - apologies for the messy question - i hope it reads abit better – jbart18 Aug 17 '22 at 04:40

1 Answers1

0

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 }] |
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I've added the template back in I got rid of tit to make a bit cleaner. Thanks for the super quick reply... i'll give the options a try - and advise how i go. – jbart18 Aug 17 '22 at 05:08
  • @jbart18 also please keep an open mind and consider that it is perfectly okay to have separate "normal" scenarios for each variation you want to test. you may find that you are not saving much effort trying to fit everything in tables, and you will be able to write nice descriptions making your tests more maintainable. if you have time you can read this rant: https://stackoverflow.com/a/54126724/143475 – Peter Thomas Aug 17 '22 at 05:16