Store AUTH TOKEN in Global Variable

Hi @Harini2rajanna… Can you please share the code as I am also trying to do the same. Thanks a lot.

response = WS.sendRequest(findTestObject(‘NassToken/CreateTenancy’,[(‘Path’) : GlobalVariable.Path,(‘Token’) : GlobalVariable.Token]))

WS.verifyResponseStatusCode(response, 201)

JsonSlurper slurper = new JsonSlurper()

Map parsedJson = slurper.parseText(response.getResponseText())

String id =

GlobalVariable.tenancyId = id


@Harini2rajanna… Thanks for the reply. But I am getting following error

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object ‘{“Token”:“dfdfciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoidGVzdCIsImV4cCI6MTU2OTM5MDI4NiwiaXNzIjoiZGV3LnNhLmdvdi5hdSIsImF1ZCI6IlNhbXBsZUF1ZGllbmNlIn0.AKBOVfdmGcsIEGLE1e_lu1uNTEYxYso6_28z78o8zlI”}’ with class ‘java.lang.String’ to class ‘java.util.Map’

I have to admit that I am very new to Groovy and Katalon. :frowning:

The error comes on “Map result = slurper.parseText(res.getResponseText())” line of code

Even I am new to it

I need the doc too,can you help me ?

I got it resolved. In my case the token was like {“token”\ : “abbnncbcjhjkdjkdfafssdfas.asdfasf”}. So these extra slashes were making troubles and cast exception appears. So I have to do like
def parsedJson = slurper.parseText(res.getResponseBodyContent())

Map parsedAgain = slurper.parseText(pasedJson)

hey man, can you help me with this thing… i’m trying to do the same exact thing you did. I’m trying to apply a token globally to get called in all the other tests.

I did that through test listener.

	import groovy.json.JsonSlurper as JsonSlurper
	import com.kms.katalon.core.util.KeywordUtil as KeywordUtil 

	private void setToken()
			//set token to global variable 'Token'
			def authentication_response = 	WS.sendRequest(findTestObject('Web Services/Authentication'))
	WS.verifyResponseStatusCode(authentication_response, 200, FailureHandling.STOP_ON_FAILURE)
	JsonSlurper slurper = new JsonSlurper()
	Map parsedJson_Authentication = slurper.parseText(authentication_response.getResponseBodyContent())

	KeywordUtil.logInfo("TOKEN received:    " + parsedJson_Authentication.token)

	GlobalVariable.Token = parsedJson_Authentication.token
	KeywordUtil.logInfo("TOKEN set to Global variable :    " + GlobalVariable.Token)

		def beforeTestSuite()
			//set global variables

I hope that helps.

which API test/repo do you put that code in? the get token endpoint or this needs to be done for every single api endpoints?

In the Test Listner I run the “Web Services/Authentication”, and set the token in a Global variable. That private method is called in a method that execute before execution of test suite. Please read how test listners work and when BeforeTestSuite method executes. So once the test case in the test suite executes, the token is already setup in the global variable. Please note that once the test suite finishes execution, the global variable is set back to default value, which in my case is null.

thanks man, I’ll give it a try when I find some time in next 2 weeks. Definitely would help a lot to get this working right.

Hi, @sang.
thank you, this work in my case :slightly_smiling_face: