How to add Multiple JSON array Element depends on Example Data Set

Viewed 147

During my Karate scenario automation got the below problem statement. Could anyone please help me on this?

My JSON file will contain,

{
  "schema_id": 25,
  "records": [
    {
      "value": {
        "NUM_CHARGE_ID": "#(numChargeID)",
        "DTE_START": "2017-05-26",
        "NUM_DAY": "#(numDay)"
      }
    }
  ]
}

From my Karate feature file I want to pass data from Example and depends on the data set it should create multiple array element in the json. Like,

Examples:
| numChargeID   | numDay|
|C10,C21,C15,C18|1,2,3,4|
{
  "schema_id": 25,
  "records": [
    {
      "value": {
        "NUM_CHARGE_ID": "C10",
        "DTE_START": "2017-05-26",
        "NUM_DAY": "1"
      },
      {
        "NUM_CHARGE_ID": "C21",
        "DTE_START": "2017-05-26",
        "NUM_DAY": "2"
      },
      {
        "NUM_CHARGE_ID": "C15",
        "DTE_START": "2017-05-26",
        "NUM_DAY": "3"
      },
      {
        "NUM_CHARGE_ID": "C18",
        "DTE_START": "2017-05-26",
        "NUM_DAY": "4"
      }
    }
  ]
}

Is there any way in Karate that i can handle this type of situation?

1 Answers

There are plenty of ways to solve this and maybe you shouldn't try to force everything into Examples:. For instance:

* def chargeIds = ['C1', 'C2']
* def dayNums = [1, 2]
* def fun = function(x, i){ return { NUM_CHARGE_ID: x, NUM_DAY: dayNums[i] } }
* def records = karate.map(chargeIds, fun)
* print records

Which gives you:

[
  {
    "NUM_CHARGE_ID": "C1",
    "NUM_DAY": 1
  },
  {
    "NUM_CHARGE_ID": "C2",
    "NUM_DAY": 2
  }
]

What I would recommend is something like this:

* table records
| NUM_CHARGE_ID | NUM_DAY |
| 'C1'          | 1       |
| 'C2'          | 2       |

* print records

Which gives you the exact same thing.

Also look at the possible use of set: https://github.com/intuit/karate#set-multiple

Related