How to use Global Variables between test suites while executing Test Suite Collection?

Lets say I have a Test Suite Collection TSC with two test suites TS-1 and TS-2.
TS-1 has a test case TC-1 with a single step ==> GlobalVariable.var1 = ‘123’
TS-2 has a test case TC-2 with a single step ==> KeywordUtil.logInfo(GlobalVariable.var1)

When I execute TSC, the TS-2 test doesn’t log ‘123’.

hi,

check this chain

Global variables works well between test cases in a test suite. How about between test suites?

yeah, that’s true, not able to use global variables between testsuites in testsuitecollection.
it always uses default value when testsuite will start
and detected new bug
when executed TSC in console log show only TS one log
TS2 log only blinks there
this is quite buggy release

and what is this popup
popup

ok, console log show testsuite 2 log if not pressed OK

Hi @Chris_Trevarthen
Any update on this?

Hello,

Katalon Studio ver. 7.2.1

Is there any follow up on this?

I have 3 mos work on the line here. I so far can NOT pass a global variable from 1 Test suite collection to another.

This makes no sense Both test suite collections are using the same default profile:

BOTH TEST SUITE COLLECTIONS USE THE SAME PROFILE

Test suite collection 1: defining a global variable getUrl= GlobalVariable.simURL
also defined in default profile: simURL = value type "

this works throughout any script in test suite collection 1

Test suite collection 2: openbrowser simURL - does not work opens a blank browser window.

This should not be a problem, as far as I can tell this is possibly a bug in Katalon?

In fact, make sense if you understand how the profiles are implemented.
The GlobalVariable compiled class which is generated at runtime in the compile phase it is scoped to the suite.
Which means, once the suite ends (no matter if it was started in a collection or standalone) the instance of the generated GlobalVariable class it is destroyed, during the run of the 2nd suite (no matter if it is running sequential or parallel) a new instance for the running thread is created based on the values defined in the selected .xml file (the profile)

So, on short, it is not possible to re-use values set in the selected profile during a suite run for another suite run, even if the selected profile is same (and honestly i see no reason for such feature … but this is just my opinion)

To achieve such goal, you may want to export the needed variables to an external file or a third party db (postgres, mysql, sqlite, whatever) and reuse-them by implementing a custom keyword.

Or … find a way to modify the profile .xml during the execution of the suite.
But pay attention, if you attempt to run the suites in parallel, lot of bad things can happen. And i doubt will work even with sequential execution too … feel free to try.

Thanks for your timely response - IMO this should be a critical feature enhancement, to have the ability to pass global variables.

@Paul_Smith1 well … Here is the time to debate.
You may consider it a critical feature, i don’t.
I do understand from where such behaviour may be desired, being an ex postman user.
And indeed, with postman, the environments (equivalnt to profiles in katalon) are updated ‘on the fly’
However, are updated only for the running instance,to make them permanent you have to export them.
In addition, postman and similar apps are not designed for parallel execution.
In katalon, you can do that (the parallel voodoo). Now let’s imagine that an inexperienced user will abuse it. Lot of stuff can go wrong.

As per myself,i will choose the workarounds i suggested, and i am possitive here @Timo_Kuisma1 can help a lot. He already posted an working example with postgres approach and if he is kind, he can develop a POC on sqlite too (i am lazy to write code).
For the feature request as in your original attempt, we may need to summon some others:

@ThanhTo @devalex88 and yeah, why not, being a great moderator, @Russ_Thomas too

I don’t see Katalon changing the scope of GlobalVariables. If they did, millions of lines of legacy code would suddenly break causing a huge backlash of angry test developers.

Perhaps a proposal for “UniversalVariables” could be made, if one saw the need, that could address the issues you believe are caused by the current scope of GlobalVariables. But even if that were agreed by the devs, it’s not going to happen overnight.

Much quicker would be a set of methods that read/write your data to external files. Personally, I use JSON files. In my case I don’t need to write to them, I only read them and they are managed outside of Katalon.

@Paul_Smith1 If you want to see some groovy code, let me know.

1 Like

Here is a sample code of loading and saving json file:

1 Like

Thanks for the response, I know nothing about groovy, so examples would be great.

In my example Im looking to put GetUrl = GlobalVariable.simURL and use it across multiple test suites. So how would I save the GetUrl into a file, and call it into the next test suite using groovy?

thanks again,

Follow @kazurayam’s advice above.

Okay, on second thoughts, you might need a little more help than @kazurayam’s post provides. Here is my method I use to load my vars. I trust you can take the save routine and beef it up to suit your needs (like I said, I don’t write to this file)

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

GLOBALS is a statically declared map which I make sure is available everywhere.

  static Map GLOBALS = [:]

The last line stores the entire JSON structure (map) into the GLOBALS map

1 Like

Opened a ticket. Thanks guys for your suggestions

Personally, I think because Test Suites are meant to be executed in parallel, enabling this type of shared states between them would possibly open the door to multitudes of concurrency problems. Nonetheless, I’d update if this ever makes to the approval stage.

2 Likes

It would be nice if Test Suites and Test Suite Collections had their own properties pages (like Test Cases have). I store TC-related data in the property description field. If TSCs had those, they could be used to read/write data that can then be viewed post execution if need be.

Sometimes it’s worth thinking inside the box :wink:

Thanks very much for your help Russ. Question: Does the code you provided get put into the “profile” for example if Im using the default profile, I would edit the default profile and add the code to it? Or would I create a new profile for every test suite collection and add the code to each new profile?

thanks again for all your help in this matter.

No. Despite the name, it does NOT touch your Katalon profile, it uses the same name to keep things “straight”, that’s all.

This…

String profileName = RunConfiguration.getExecutionProfile()

does what it says it does, grabs the “name”. That’s just a string, If yours are all called “StuffAndThings” that’s fine too.