Using variables between test cases

I’m having difficulty accessing variables between test cases.

For an example website url:

And default profile with following declared variable:
name: codeID, type: String, value: ""

I just wanted to grab the code at the end of url, which is dynamic, and store this in the above variable which should be accessed in any test case.

The scenario is as follows:-

GlobalVariable.codeID = WebUI.getUrl() - ""
println GlobalVariable.codeID

println GlobalVariable.codeID

But this doesn’t work: It prints correctly in the 1st test case, but only a blank line in the 2nd one.

Any help would be much appreciated.

How are you running the TC? Separately or did you make any test suite ?

I’ve tried it as a test suite as well as separate test cases.

This seems like a pretty fundamental thing. I was hoping that it would be more intuitive to do. But, I wouldn’t be surprised if it’s just me not doing something right?

I already had a look at that document but it appears to be that these global variables, declared in profile are static?

I want to be able to store values that are dynamic and change depending on the running of certain test cases. It would have been very easy for me to simply store that codeID as ‘31065’, but this can change when I run TC1.

Is there some other way of handling this?


You can share values between the test cases in this way only if TC1 and TC2 are run inside of a same Test Suite.


You seem to misunderstand the meaning of static modifier. Read the “Class Variable” section of It sais:

… Any object can change the value of a class variable (static variable), …

Another modifier final makes a variable unmodifiable. Read the “Constants” section of It sais:

The final modifier indicates that the value of this field cannot change.

In Katalon Studio, a GlobalVariable is declared as static but not as final. You can find the source code of GlobalVariables in the default Execution Profile at <projectDir>/Libs/internal/GlobalVariable.groovy. The following snippet is an example of GlobalVariable source:

package internal

import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.testobject.ObjectRepository as ObjectRepository
import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory
import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase

 * This class is generated automatically by Katalon Studio and should not be modified or deleted.
public class GlobalVariable {
     * <p>Profile default : e.g., 'Test Cases&#47;TC1'
Profile develop : e.g., 'Test Cases&#47;TC1'
Profile product : e.g., 'Test Cases&#47;TC1'</p>
    public static Object CURRENT_TESTCASE_ID
     * <p></p>
    public static Object MATERIAL_REPOSITORY
     * <p></p>
    public static Object Hostname
     * <p></p>
    public static Object Username
     * <p></p>
    public static Object Password
     * <p></p>
    public static Object URL

    static {
        def allVariables = [:]        
        allVariables.put('default', ['CURRENT_TESTCASE_ID' : '', 'MATERIAL_REPOSITORY' : null, 'Hostname' : '', 'Username' : 'John Doe', 'Password' : 'ThisIsNotAPassword'])
        allVariables.put('develop', allVariables['default'] + ['CURRENT_TESTCASE_ID' : '', 'MATERIAL_REPOSITORY' : null, 'Hostname' : '', 'Username' : 'John Doe', 'Password' : 'ThisIsNotAPassword'])
        allVariables.put('', allVariables['default'] + ['MATERIAL_REPOSITORY' : null, 'CURRENT_TESTCASE_ID' : '', 'URL' : ''])
        allVariables.put('', allVariables['default'] + ['MATERIAL_REPOSITORY' : null, 'CURRENT_TESTCASE_ID' : '', 'URL' : ''])
        allVariables.put('', allVariables['default'] + ['MATERIAL_REPOSITORY' : null, 'CURRENT_TESTCASE_ID' : '', 'URL' : ''])
        allVariables.put('product', allVariables['default'] + ['CURRENT_TESTCASE_ID' : '', 'MATERIAL_REPOSITORY' : null, 'Hostname' : '', 'Username' : 'John Doe', 'Password' : 'ThisIsNotAPassword'])
        String profileName = RunConfiguration.getExecutionProfile()
        def selectedVariables = allVariables[profileName]
		for(object in selectedVariables){
			String overridingGlobalVariable = RunConfiguration.getOverridingGlobalVariable(object.key)
			if(overridingGlobalVariable != null){
				selectedVariables.put(object.key, overridingGlobalVariable)

        CURRENT_TESTCASE_ID = selectedVariables["CURRENT_TESTCASE_ID"]
        Hostname = selectedVariables["Hostname"]
        Username = selectedVariables["Username"]
        Password = selectedVariables["Password"]
        URL = selectedVariables["URL"]

Provided with the above GlobalVariable.Hostname, my Test Case A can set a value, and Test Case B can read the value; B sets a new value and C reads the value — if all of the A, B, C runs in a Test Suite S.

Test Case A:

GlobalVariable.Hostname = ''

Test Case B:

def hostname = GlobalVarialbe.Hostname
GlobalVarialbe.Hostname = ''

Test Case C:

String hostname = (String)GlobalVariable.Hostname

HI kazurayam, I am trying to set a globalvariable value in default profile from running a test case (B) day 1, then come back the next day and run 2nd test case © to read value. I tested your exact code sample above for Test Case B+C (I figured i could skip A since B ‘sets’ the value too)
However this did not work, Test Case B does not save globalvariable value in the profile. Am I missing something?

This would never work.

A GlobalVariable is scoped within a TestSuite. You make a TestSuite S which includes TestCase A and TestCase B. TestCase A updates a GlobalVariable and TestCase B reads the GlobalVariable. And you execute the TestSuite S; TestCase A and B will be executed in a sequence having no time gap in between. Then TestCase B can read the value on memory set by TestCase A. The value updated into a GlobalVariable will never be persisted into a file.


If you need days in between TestCase B and C, then a GlobalVariable is not a good vehicle for your data. TestCase B needs to write the data into a file, and TestCase C reads data from the file. Katalon Studio does not provide a ready-made tool for persisting custom data. A practical way for you would just develop a Groovy code to write/read a JSON file.

thanks for quick response. It does appear to work in a test suite. after suite completes the variable no longer persists, but thats fine as long as works in a suite. I will use the write to file scenario.