How to create global strings

Hello,

There are many scripts that use the same strings/objects for different test cases. I am copying the same string declaration in those test cases. Is there a way to declare those strings once somewhere and reuse them in all the test cases? Note that I do not want to use Profiles (Global Variables) in this case.
Thanks.

Without using GVs, your only choice I believe is to use an external file.

I store my globals in and external file, globals.json.

This post reminds me of my previous post. I also used an external file: katalon.properties

principally no different from JSON file.

Sounds very complex. I was hoping you would tell me to create a file (external or internal ) and declace my global strings in that file and reference that file somehow.

That is, in effect, what GlobalVariables are.

The problem with using global variables is that you cannot use more than one profile per test case. Thats one problem, so i had to regroup all my global variables into one profile. Plus i do not want to add the strings in there it will be too much. I was wondering if there is an easier way.

My solution is here:

My custom keyword enables you to load multiple Execution Profiles to a Test Case ( or a Test Suite).

I know well what the problems are with profiles/GlobalVariables. The solution is an external file.

Sorry, but you’re going around in circles. Either live with GlobalVariables, OR, use an external file.

On this forum alone there are many links to help you - @kazurayam gave you one. On the web, there are likely millions.

Here is just one of mine…

{
  "description": "These become Katalon GlobalVariables when read in by com.x.utils.readProfileSettings()",
  "DB": "SQLSVR",
  "EXPECT_$WE": false,
  "TARGET_URL": "https://somewhere.com/",
  "TESTUSER": "Someone",
  "TESTPWD": "big-secret",
  "DOC_SMARTSEARCH": "C:\\Users\\russ\\Katalon Studio\\SMARTSEARCH.txt",
  "ALL_MODULES_PATH": "C:\\code\\russ\\allmodules.zip"
}


or an sqlite db (well yeah, this is also an external file). @kazurayam made a POC code too on this usage. is somewhere on the forum
:stuck_out_tongue:

that’s not 100 % true. You can organize your variables between certain profile’s and the Default profile.
whatever variable is not found in the current selected profile will be picked from the default one (that’s the main reason the default profile cannot be deleted, must exist all the time)
I demoed this on a certain topic.

For more complex cases you can use a combination of global variables and external files, store in the global variable just the name/path to the file(s). That will give you enough flexibility.

Here you are:

That;s interesting. I will spilt my global variables into 2 profiles in that case. Thanks!!

It’s actually called loadProfileSettings() - the comment is wrong :confused:

utils is my code class - you will need to write your own. In Katalon you will need to create a custom keyword class and add the static method loadProfileSettings and import it in the test cases where you wish to use it.

import com.kms.katalon.core.configuration.RunConfiguration
import static com.mgrandillo.utils.*

loadProfileSettings()

Here is a very cut-down version to get you started…

  /**
   * Read in the current profile settings.
   */
  static void loadProfileSettings() {
    String profileName = RunConfiguration.getExecutionProfile()
    String dir = getMySettingsPath()
    def f = new File(dir + "json/" + profileName + ".json")
    Map map = new JsonSlurper().parseText(f.text)
    GLOBALS << map
  }

GLOBALS is just a map defined elsewhere.

You asked, we answered.
Now feel free to choose what suits you :smiley:

Easiest option is to use Global Variables and split them into 2 profiles. I have done that and works fine for me. Thanks.

1 Like