Is there a way to create a global variable from test case calling an api and using later in the test case for a different api call

Good Morning,

I have hunted around for an answer but didn’t see what I was looking for.

I need to create a global variable from one api call and use that global variable in the next api call in the same test case.

API 1 has output string in the output results. I need to grab that output string and set it as the global variable
API 2 needs the new global variable set as the input string in the json input body

In test case, I want to call API 1 then AP2.

Is this possible? If so, how?

No. There isn’t any Katalon API that creates a new GlobalVariable runtime.

However, there is a workaround.

The easiest workaround would be to declare a GlobalVariable of type java.util.Map<String,Object> in the default profile. Your test script would use it as a container of ad hoc variables. You can easily add/modify/delete key=value pairs in that GlobalVariable with java.util.Map API. You do not need any special library.

Add/modify a variable named foo with value bar:

GlobalVariable.CONTAINER.put("foo","bar")

Print it

println "foo is " + GlobalVariable.CONTAINER.get("foo")

Delete “foo” if it exists:

if (GlobalVariable.CONTAINER.containsKey("foo")) {
    GlobalVariable.CONTAINER.remove("foo")
}

In the above sample code, the value of “foo” property in the GlobalVariable.CONTAINER is a String. However you can put any type of object instances into “foo” because CONTAINER is declared as an instance of Map<String, Object>

I have ever developed a Custom Keyword that can create a new GlobalVariable entity in memory runtime. See the following:

public static int loadEntries(Map<String, Object>) in https://github.com/kazurayam/ExecutionProfilesLoader/blob/master/Keywords/com/kazurayam/ks/globalvariable/ExecutionProfilesLoader.groovy

However it is a bit complicated.

You wouldn’t need this, as GlobalVariable.CONTAINER of type Map<String,Object> would suffice for you.

1 Like

Thank you for your response. I am have new to automation and had to put API testing on the backburner for front end automation. I don’t understand the steps.

When creating a new global variable


The first key should be string and key of foo? and the second is property of that parsedjson?

Please find the following example.

F.Y.I.

I am able grab the output string and use it in the next call in the test case.

Thanks.