Parameterize fields of json body passed as an external file


#1

Hello,
How to parameterize fields of JSON body passed as an external file ?
Appreciate your help.


#2

I’m not sure this answers your question, but I just started using JSON files to hold the input data for my tests. JSON files are more powerful and flexible than the comma-separated files (CSV) that are built in to Katalon Studio. JSON files allow me to store lists, maps, objects, etc.

Suppose my JSON file with test data looks like this

{
   "name" : "Harold",
   "hobbies" : [ "programming", "gardening", baking" ],
   "favorites" : { "food" : "sushi", "color" : "blue" }
}

Use these three lines to read the file. (Put these lines in a reusable test case or custom keyword so all your tests can use them.)

import groovy.json.JsonSlurper

def inputFile = new File("D:\\myPath\\my-data.json")
def data = new JsonSlurper().parseText(inputFile.text)

Now, in my test I can refer to the values as

String name = data.name
List hobbies = data.hobbies
Map favorites = data.favorites

This is a nice way to separate the inputs to my test from the logic/steps of the test. If I ever need to change the data, I can update the file without affecting the test. I can also use different data files with the same test.

Good luck!


#3

You can use Groovy Template Engine:
http://docs.groovy-lang.org/docs/next/html/documentation/template-engines.html