Configuring a Katalon Studio project with a JSON file

I have published a GitHub repository

Problem to solve

Katalon Studio provides Execution Profile and Global Variables.

Execution Profile helps cover multiple and different environments to execute your automation test scripts with ease. You can configure the testing environment in terms of data and behaviors through Global variables.

You can update the value of GlobalVariable during test runs but you can not persist the updates into Execution Profiles. If you want to change the persisted values of Execution Profiles, manual edit in Katalon Studio’s GUI is the only way.

I am not satisfied with this constraint. I want to find a way to update the config information programmatically and persist it into disk during a Test Suite run; then later I want to reuse the updated config information in another session of Test Suite. How can achieve it?

Solution

  • I will add JSON text files as a container of configuration information. Thanks to the expressiveness of JSON, I can write anything.

  • I will create a GlobalVariable of type Null.

  • My test script will fully utilize the Groovy language features of processing JSON.

  • My test script will explicitly load a JSON configuration file, turn it into an Object, substitute it into a GlobalVariable. The GlobalVariable will be shared by all Test Cases in a Test Suite. My test script will update the Object during test run, and will explicitly save it into a JSON file.

  • Thanks to the Groovy’s features of processing JSON, it is quite easy and straight-forward to implement this processing. I will show you a full set of sample code.

Description of the demo implementation

JSON configuration file

I created a text file in JSON format: <projectDir>/myconfig.json

{
    "bill": "debit",
    "home": "debit",
    "money": "credit"
}

GlobalVariable.config

I created a GlobalVariable named config in the default Execution Profile. I assigned the type Null to it.

GlobalVariable.config

This GlobalVariable is used as a placeholder for an Object deserialized from JSON file. The GlobalVariable will be visible for all Test Cases in a Test Suite.

Test Case TC1 — load the JSON to GlobalVariable

Test Cases/TC1 loads a JSON file, deserialize it into an Object, put the Object into the placeholder GlobalVariable.

import groovy.json.JsonSlurper
import internal.GlobalVariable
/*
 * load the config file and put it into a GlobalVariable to share
 * in the scope of a Test Suite
 */
JsonSlurper slurper = new JsonSlurper()
GlobalVariable.config = slurper.parse(new File('./myconfig.json'))

Test Case TC2 — update information programmatically

Test Cases/TC2 updates the Object programmatically.

import internal.GlobalVariable as GlobalVariable
/*
 * demonstrate that we can update the GlobalVariable in memory
 */
println "GlobalVariable.config before update: " + GlobalVariable.config
GlobalVariable.config['home'] = 'mona lisa'
println "GlobalVariable.confing after update: " + GlobalVariable.config

Test Case TC3 — demonstrate that the updated information is carried over

Test Cases/TC3 demostrates that the Object updated by the preceding TC2 is carried over and visible for TC3.

import internal.GlobalVariable as GlobalVariable
/*
 * demostrate that the GlobalVariable which was updated by the preceding Test Case TC2
 * is globaly shared in the Test Suite scope
 */
println "GlobalVariable.confing carried over: " + GlobalVariable.config

Test Case TC4 — persist the information into an external file

Test Cases/TC4 serializes the updated Object into JSON and save it into a text file.

import groovy.json.JsonOutput
import internal.GlobalVariable as GlobalVariable
/*
 * demonstrate that we can save the updated config into an external JSON file
 */
def myconfig = GlobalVariable.config
myconfig['money'] = 12345

File f = new File('./myconfig2.json')
// you can specify the original file name 'myconfig.json' to overwrite it if you want

f.text = JsonOutput.prettyPrint(JsonOutput.toJson(myconfig))

println f.text

Please pay attention to the output file name.

If you specify a new name, then a new file will be created.

If you specify the name of source JSON file, then the file will be overwritten.

Which way to take?

It is up to you.

Test Suite TS1 — run TC1 + TC2 + TC3 + TC4

I made a Test Suite TS1, which just runs TC1, TC2, TC3 and TC4 in this sequence.

When I ran it, I got the following output in the console:

...
GlobalVariable.config before update: [bill:debit, home:debit, money:credit]
...
GlobalVariable.confing after update: [bill:debit, home:mona lisa, money:credit]
...
GlobalVariable.confing carried over: [bill:debit, home:mona lisa, money:credit]
...
{
    "bill": "debit",
    "home": "mona lisa",
    "money": 12345
}
...

Conclusion

I think that the built-in features in Katalon Studio GUI around GlobalVariable are designed with an assumption that users will be happy and satisfied with manually updating Execution Profiles (= GlobalVariables); they would never want to update Execution Profiles on disk programmatically during the executions of Test Case scripts. I think that this assumption is appropriate as Katalon Studio is designed for non-programmers.

A post in Katalon user forum discussed how to break this design assumption by overwriting XML files as the serialized format of Execution Profiles. IMHO, their approach has an arguable point. I think we shouldn’t try to modify the behavior of Katalon Studio. Just leave it as is. We can invent an alternative of configuring a Test Suite using Groovy scripting.

A shortage of my approach is that it does not provide any GUI component to edit/view the JSON configuration as a part of Katalon Studio GUI just like it does for Execution Profiles. — I don’t mind it. I don’t need any GUI-support. Text editor is enough for authoring JSON.

3 Likes

I would like to propose a usage pattern where we use Execution Profiles and JSON config files in combination. See the following:

I will create 3 Execution Profiles

  • development-env
  • production-env
  • stating-env

All these 3 Execution profiles declares a common GlobalVariable named config. The GlobalVariable.config will be String type, it will contain the path to the configuration JSON file.

I will create 3 JSON files

  • ./config-development.json
  • ./config-production.json
  • ./config-staging.json

In these 3 JSON files, I will declare any configuration information to control the test. For example, the target’s hostname, port, etc.

Of course I will link the Execution Profiles to JSON configs like

  • development-env./config-development.json
  • production-env./config-production.json
  • staging-env./config-staging.json

On the top-right side of the Katalon Studio GUI, you can choose one out of the 3 Execution Profiles development-env, production-env and staging-env.

This means effectively you can choose, by GUI operation, one out of the 3 JSON configuration files to apply to your test execution. Similarly, in Katalon Runtime Engine, you choose it by a command line parameter.

This way makes it possible for me to write all the configuration information of 3 environments in JSON text. I prefer JSON to XML for configuration format. No need to discuss why.

Do you have more environments to test? — Fine. You want to add more pairs of Execution Profile - JSON.

I’m a little disappointed there’s no acknowledgement of prior art.