1

I need to select any random value from the below sample json every time i run the code. How can i achieve this in Karate? I need to get any random value and use in another feature file.

def myJson = 
"""
{
      "sampleJson": {
        "random1": "1",
        "random2": "2",
        "random3": "3",
        "random4": "4",
        "random5": "5"
      }
  }
"""
sn1693
  • 271
  • 2
  • 21

1 Answers1

3

Also refer: https://github.com/intuit/karate#commonly-needed-utilities

* def random = function(max){ return Math.floor(Math.random() * max) + 1 }
* def myJson = 
"""
{
  "sampleJson": {
    "random1": "1",
    "random2": "2",
    "random3": "3",
    "random4": "4",
    "random5": "5"
  }
}
"""
* def key = 'random' + random(5)
* def result = myJson.sampleJson[key]
* print result

Also see: https://stackoverflow.com/a/70376593/143475

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