Capture unique Bearer token into variable and passed into header of request2

Hi have upgraded to Katalon from postman.

How to capture the ‘access_token’ from 1st Request response and save into Variable

Need to save this new token every time into that variable from Json response.

Thanks

Hi,

check regex
https://www.vogella.com/tutorials/JavaRegularExpressions/article.html

Thank, but that is not what i am looking

If the token has to be reused in same testcase, you can use a local variable.
to make the token available across testcases, define a global variable (can have an empty value) and save the token into it. You have to put the testcases in a testsuite to make it work (global variables are scoped to suite).
see also this workaround to create globals on-the-fly: https://docs.katalon.com/katalon-studio/docs/execution-profile-v54.html#create-global-variables-during-runtime

Note that Katalon does not work like postman (saving variables into generic env straight from the request or from manually executing a tescase) so you will have to re-think a bit your workflow.

Some tips here … yeah, i know, coming from Postman into Katalon can be a bit confusing at the beginning:

With Postman, for every test scenario, you create a request, pre-conditions are initialized into the Pre script and verifications are done into the Test script. So, for a new scenario using the same request (let’s say once you do a valid API call and check for success and for the second you provide wrong data and check for 404 code) you have to create a new request (could be duplicated from the first one)

With Katalon, you have to think up-side-down. You make a single request object, parametrized, and for every scenario you create a TestCase, passing variables accordingly. Verification’s are made in the testcase.

Of-course, you can use same approach like in postman, for every scenario create a request object and use the verification snippets straight in the request object … but using the other way will give you more flexibility and the resulting project will be cleaner, will have less request objects.

Personally i prefer to make only few generic request objects. Let’s say one for GET, one for POST, one for PUT, one for DELETE, fully parameterized, and maybe one more for grabbing the auth token.
For the rest I simply create testcases for each scenario needed and initialize the variables straight from the code (endpoint, header if its changing, body etc)

During the design stage, I just run the auth call manually, copy the token and paste into the Execution Profile in use. When it’s expired, renew it.
Once I have few tescases working, I make a suite, where the first tescase is the one which gets the token and is saving it into the Global Variables > run it.

And so on, for every new scenario, make a new testcase, run it manually, once is working fine, add it to the suite.
You can have also another verification suite where you put only the auth testcase and the new designed … once it’s working fine add the new case into the main suite too.
And more, once you have a full suite with all kind of scenarios (postive, negative, boundary etc) you can duplicate it, keep only the positive tests and use it as a smoke suite, the full one can be used as sanity or regression. With Postman, for this you will have to duplicate the full collection and remove the requests not needed or do some dirty voodoo using postman.setNextRequest… so I think you can see some benefits here.

Hope this hints will help you to migrate easily. Cheers!

2 Likes

and yeah … later, when you gain some experience with Katalon, you may find out you don’t need request objects at all … but you can make a CustomKeyword class and define your request templates there …
but hey, one step at a time.

Happy testing!

1 Like

@bionel
Agree with you :, I make a suite, where the first tescase is the one which gets the token and is saving it into the Global Variables > run it.
[/quote]

Yes this is where i want to come

image
image

@bionel @kazurayam jst to update i am able to work with 2nd request. but what i did is copy and paste the token (manual)

thanks for your inputs. But now what to do automated this. @huynguyen
Capture that value in Variable and used it like above Screenshot(readsBearer ${a_token} )
image

I get token expires, so this is where i need to use Variables for Any help in this would be great.

@bionel
facing this error with jsonSlurper

WS.sendRequest(findTestObject('Postman/GenerateAccessToken', [('TOKEN_URL') : GlobalVariable.TOKEN_URL, ('c_ID') : GlobalVariable.c_ID
            , ('u_name') : GlobalVariable.u_name, ('c_SECRET') : GlobalVariable.c_SECRET, ('enc_pwd') : GlobalVariable.enc_Pwd]))

tokenGen().toString()


String tokenGen() {
def response = WS.sendRequest(findTestObject('Object Repository/Postman/GenerateAccessToken'))
//def jsonSlurper = new JsonSlurper()
def jsonSlurper = new JsonSlurper()
def result = jsonSlurper.parseText(response.getResponseBodyContent())
def token = result.access_token
WS.verifyResponseStatusCode(response, 200)
println(token)
GlobalVariable.token = token
}

This is what I needed . All Manual Work is working with copy pasting

i have already told you to stop thinking ‘Postman’. why did you put the code for getting the token into a variable?
and for god sake. stop using request objects and explore test-cases scripting

I am sorry @bionel . I am trying. Sorry frustrating you. I am not aware of what value of ‘token’ would be there… ( I had a manually copied the token value and it worked)
So wanted to automate the same.

Let me delete all the test and start afresh.

you do not ‘frustrating’ me … ‘au contraire’ :slight_smile:
I’ve been blocked many times before by my own mind, this is why I suggest you a ‘reset’.
with Katalon you will be one more step forward to truly AQA and yeah … i know it is hard to migrate from manual testing habits to code …
we are here to guide you … try to understand how it works under-the-hood (hint: is a mini IDE, not just a tool) …

1 Like

@bionel Thankyou.

Just seems all working with copy pasting stuff.
Request 1(Post) token generation (GenerateAccessToken)
Request 2 (generate id) with token) done (from Globally variable manually copy paste) (2_TPO-Create Item)
image

Now step by Step. What would be the next step. I am happy to learn

ok … now go to Step 4, create two testcases, one for each request, and execute them.
see the doc:

1 Like

@bionel
Thanks able to create 3 test case (Tested and Verify)
Post(auth)
Getitem(from auth)

But for other GET requests I am getting Not Found (not ever Token expired, when it should should give on expired token)

What would be next step ?

well … now is time to get your hands dirty.
Open the testcase wich gets the token, parse the response and save it in the Global Variable.
You should already have in it something like:

response = WS.SendRequestBlah ....

now use the slurper to get the token value and save it in the variable, sort of:

slurper = new groovy.json.JsonSlurper()
result = slurper.parseText(response.getResponseBodyContent())
token = result.token // use the key name from the json response here
GlobalVariable.token = token //use the variable name you defined in the profile

also make sure you add the needed import at the top for Json.Slurper.

once done and the tescase is working fine, go to step 6 in the doc:

  • create a testsuite
  • add the tescase to get the token to be the first one
  • add the second testcase known to work
  • run it

Now your testsuite should automatically retrieve the token and save it in the scoped instance of the Global Variable so other testcases from the suite will use this one instead of the one set in the GUI.
(you can add some println statements in your testcases to make sure the right value is used)

1 Like

Thanks @bionel :slight_smile: I am doing the same. Thank you for patience and answering my queries .
and can you please check this

Hi gsp,
did you get solution for above , to make it automate to get the access token in a variable , without putting manually , so that even if it gets expired I dont have to manually change it while running the suite.
I am working on similar, so would like to hear from you.

Thanks

Hi All ,

I am also new with Katalon Studio and just going to switch from Postman to Katalon , can you please guide me where i am doing wrong.

Please see below my screen shots and code

AuthToken Generate Call:

Code:
ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()

slurper = new groovy.json.JsonSlurper()
result = slurper.parseText(response.getResponseBodyContent())
AutToken = result.access_token // use the key name from the json response here
GlobalVariable.access_token = AutToken //use the variable name you defined in the profile
println(AutToken)

Now, second call AuthToken is used to get Data