Store AUTH TOKEN in Global Variable

Hello,

I have a problem with storing in GlobalVariable my authorization
token, which I want to use in other test cases.

I’ve tried to do this in Postman and it’s works perfectly,
but not in Katalon…

What I want to do:

[tc-1] POST auth_token
and store access_token and refresh_token in GlobalVariable

[tc-2] GET user list
using access_token

How code looks [tc-1]:

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint

import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase

import static com.kms.katalon.core.testdata.TestDataFactory.findTestData

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import
com.google.common.util.concurrent.CycleDetectingLockFactory.ExampleStackTrace

import
com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint

import
com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory

import
com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywords

import
com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile

import
com.kms.katalon.core.model.FailureHandling as
FailureHandling

import
com.kms.katalon.core.testcase.TestCase as
TestCase

import
com.kms.katalon.core.testcase.TestCaseFactory as
TestCaseFactory

import
com.kms.katalon.core.testdata.TestData as
TestData

import
com.kms.katalon.core.testdata.TestDataFactory as
TestDataFactory

import
com.kms.katalon.core.testobject.ObjectRepository as ObjectRepository

import
com.kms.katalon.core.testobject.TestObject as
TestObject

import
com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords

import
com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

import
com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywords

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords
as WebUI

import internal.GlobalVariable as GlobalVariable

import groovy.json.JsonSlurper as JsonSlurper

import
com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

import com.kms.katalon.core.testobject.TestObjectProperty

import
com.kms.katalon.core.testobject.ConditionType

‘Send a REST request and return its response’

def response = WS.sendRequest(findTestObject(‘Object
Repository/QA_POST_AUTH_TOKEN’))

‘Store access_token’

def jsonSlurper = new JsonSlurper()

def res = jsonSlurper.parseText(response.getResponseText())

AutToken = res.access_token

println AutToken

GlobalVariable.AutToken = AutToken

As I can see in console, access token is printable. But when
I try to run [tc-2] there is no
AutToken (the string is empty)

In case of Postman, I’ve used this:

var jsonData =
JSON.parse(responseBody);

postman.setGlobalVariable(“access_token”,
jsonData.access_token);

And access_token is stored in GlobalVariable, which I can
use in next test case…

I’ve tried everything what I could find in Katalon forum and
in the internet

1 Like

(and I forgot to add)

It seems to me, that GlobalVariable works like local
variables…

I’m using Katalon Studio Version: 5.4.1 on Windows 10

Julian,

Global variables can be set new value at run-time, and technically, this new value is only available at run-time. If you get the token, update to Global variable and use this variable in the new request, then the request will be able to use the token.

Is this working I’m trying to do the same exact thing but I can’t, I guess because it only persists for runtime, then disappears. What is the best practice for achieving this? Saving it to a file and reading the data?

My investigation shows me that It seems to GlobalVariables are stored and transfer (between test cases) only when You run a TestSuite and there is possible to use a GlobalVariable in REST API HTTP Header (GUI).
To use just call ${variablename) and define them in variables in testcases and run through TestSuite.

image

PS. I also found something, but I can;t use this:
String authHeader = “whateverYouNeedForAuthentication”
TestObjectProperty header1 = new TestObjectProperty(“Authorization”, ConditionType.EQUALS, authHeader)
TestObjectProperty header2 = new TestObjectProperty(“Content-Type”, ConditionType.EQUALS, “application/json”)
TestObjectProperty header3 = new TestObjectProperty(“Accept”, ConditionType.EQUALS, “application/json”)
ArrayList defaultHeaders = Arrays.asList(header1, header2, header3)

Hi Julian_Wladyczynski

Please retry by following this step:

1. Assigned access_token value to the GlobalVariable

response = WS.sendRequest(findTestObject('Employees/Login backoffice and get access token'))
WS.verifyResponseStatusCode(response, 200)
JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(response.getResponseText())
String access_token = parsedJson.access_token
GlobalVariable.Token = access_token

2. On the GET request, set the Variable as below.

3. Using the access_token in the request header.

Hope this helps.

3 Likes

Sang, do you know if I could use the same in the body? When I copied pasted the auth token in the body it worked, when I used the variable in the body it failed.

I am not getting your step 1. where to set the response

From the test case script nitijabin

For this I decided to just write to excel sheet I save the first token, call it in the next auth call, then store the authorization bearer token. Something else to consider.

Hi Sang,

I m trying in the same way .

but not able to provide the token in authorization
image

facing

{
“message”:“Unauthorized”,
“code”:“401”,
“reason”:“Invalid OAuth token provided.”
}

Hi Harini2rajanna

Please recheck all steps to make sure that it is correct, or print the access token variable value to see if you got it.

Hi Sang,

Please see below with screen-shots

  1. I am assigning the token to global variable

2.Declared the Acess_token in default as a global varaible

  1. In another post call I am calling the global variable in header

when I print it its a empty string

Note: I am doing all this operations on object repository

Not sure what I am doing wrong here
Please assist me on this

this will never work.
you have to do the setvariable > grab variable from the testcase, not from the request object.
and put the testcases in a testsuite.

let me guess, you are a Postman user? :slight_smile:
katalon is not working like this. the instance of the profile (global) class used when you run the tests manually it is different than the instance used by the GUI.
when you run manually the tescases (or you run the request object) the GUI instance of the global class is not updated, it can be read only. (in fact, i think the associated xml file is read … not sure how is working under the hood)

when you run a request, or a testcase, a new instance of the class is created, having the default values as defined in the GUI one. but this
object is scoped to the runtime.
so, when, you run manually a request (or a testcase) > a new instance is created. the request is ok, or the testcase run with success > all fine and the instance is destroyed. or they fail … whatever. the runtime scope is ended so the created ‘in memory’ objects are destroyed.
you run the second request or testcase > the above process is repeated. so, the global exported in step one is not preserved for step two.

when you run a testuite, the instance of the global class created is scoped to the suite, so the value set by first testcase can be grabbed in the second testcase.

hope that helped to understand why katalon behaviour <> postman behaviour

Thank you.
Yes I am a postman user.
This helped me lot .

As u said

1.I grab the value from test case and assign it to Global variable


2. I have to pass this value in header of next request
in the object repository

3. Create the empty global value

4. Both the test case are running in test suite

facing the same 401 token is invalid

dont pass the value straight to the request object … but from the second testcase, call the request object with parametters binding.
helooo … should i explain also the variables visibility? is in the docs.
request objects are only for design. i repeat. forget about postman

Hey can you share me the docs or in detail explain me how do i need to pass it in 2 test case
It will be really helpful

Thank you

Its working for me thank you

Its working for me

Thank you