Create Global Variables on the fly

Estimated reading time: 1 minute

To create Global Variables during runtime using Katalon scripts, there are many approaches as discussed here. One of the approaches below comes from Sergii Tyshchenko:

"You can also define environment variable (with path to external configuration or properties file) in the session will be used to execute Katalon studio and then in the TestListener read the value of variable (path to the file), load that file and override settings for project, Global variables etc. To create new GlobalVariable I used metaprogramming:

 void addGlobalVariable(String name, def value) {
  GroovyShell shell1 = new GroovyShell()
  MetaClass mc = shell1.evaluate("internal.GlobalVariable").metaClass
  String getterName = "get" + name.capitalize()
  mc.'static'."$getterName" = { -> return value }
  mc.'static'."$name" = value

It's possible to add getter/setter as new methods toGlobalVariableclass or add a new field (commented in this example)

Then in the script code, you can use GlobalVariable.VarName

CustomKeywords.'helper.addGlobalVariable'('localURL', '')
println GlobalVariable.localURL

Credit to Sergii Tyshchenko and refer to this

User Contributed Notes