How to create a test that saves the token of my API


#1

I need to access my Api and then make GET AND POST requests and be able to show this result in the katalon. I attack any kind of help you can give me.


#2

Hello,
you just need to process headers from 1st response, and store token in variable so it can be used in construction of header in following request something like this:

def token = response.getHeaderFields().tokenCookie 
myCookie = new Cookie("token", token)
new Response = WS.sendRequest(findTestObject(ro.getObjectId(),[('COOKIE'): myCookie]))

i dont know how exacly your token looks like and if you can send it as cookie or you need to set it directly as headers this is request definition

image.png


#3

thanks,
where I have to write the code, I have to create a test case and define
the path of the object get or pos where I wait for the token?


#4

Fernando Espinoza said:

***cut*** I have to create a test case and define
the path of the object get or pos where I wait for the token?

yes, exactly


#5

How do I define the route to the object, is it right there?

def request = WS.sendRequest(findTestObject(‘User/GetUserDetails’))


#6

that looks good… just small fix (wording) result of WS.sendRequest() is response - so you will not be mistaken by yourself when processing variable


#7

I have one more question, how do you know this code, where will you get the token? that
is, where do I define the API url, would I need to write a code where I
wrote the credentials of the API in order to have the token and then
tomasen the token a global variable?


#8

that depends on type of authentication. in my case i needed to authenticate against kerberos server, i used libraries for that purpose. if you have OAuth 1.0 you can set that directly in request definition, otherwise you need to use existing library or write your own.


#9

hi,
I tried this code

def token = response.getHeaderFields().tokenCookie 
myCookie = new Cookie("token", token)
def newResponse = WS.sendRequest(findTestObject(ro.getObjectId(),[('COOKIE'): myCookie]))

but i am getting
05-28-2019 10:00:31 PM - [START] - Start Test Case : Verification
05-28-2019 10:00:32 PM - [ERROR] - Verification FAILED because (of) (Stack trace: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WSVerification1559073632137: 22: unable to resolve class Cookie
@ line 22, column 63.
elds().tokenCookie myCookie = new Cookie
^

1 error

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:946)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:542)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at com.kms.katalon.core.main.ScriptEngine.getScript(ScriptEngine.java:188)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:183)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:108)
at com.kms.katalon.core.main.WSVerificationExecutor.runScript(WSVerificationExecutor.java:145)
at com.kms.katalon.core.main.WSVerificationExecutor.doExecute(WSVerificationExecutor.java:139)
at com.kms.katalon.core.main.WSVerificationExecutor.processExecutionPhase(WSVerificationExecutor.java:122)
at com.kms.katalon.core.main.WSVerificationExecutor.accessMainPhase(WSVerificationExecutor.java:114)
at com.kms.katalon.core.main.WSVerificationExecutor.execute(WSVerificationExecutor.java:102)
at com.kms.katalon.core.main.TestCaseMain.runWSVerificationScript(TestCaseMain.java:112)
at com.kms.katalon.core.main.TestCaseMain$runWSVerificationScript$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)
at TempTestCase1559073625443.run(TempTestCase1559073625443.groovy:22)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)
at groovy.lang.GroovyShell.run(GroovyShell.java:518)
at groovy.lang.GroovyShell.run(GroovyShell.java:507)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
at groovy.ui.GroovyMain.run(GroovyMain.java:384)
at groovy.ui.GroovyMain.process(GroovyMain.java:370)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
at groovy.ui.GroovyMain.main(GroovyMain.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)

)
05-28-2019 10:00:32 PM - [END] - End Test Case : Verification

can you help

cheers

wale


#10

you need to import libraries for cookies CTRL+SHIFT+O
or

import org.openqa.selenium.Cookie

#11

Thanks. now i get no such property error
[ERROR] - Verification FAILED because (of) (Stack trace: groovy.lang.MissingPropertyException: No such property: token for class: WSVerification1559119880398
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)


#12

i edited code i posted
and it should be like this:

def token = response.getHeaderFields().tokenCookie 
myCookie = new Cookie("token", token)
def newResponse = WS.sendRequest(findTestObject(ro.getObjectId(),[('COOKIE'): myCookie]))

#13

Run the access API -> get the body of the access API + with the other APIs, set the token as global variable -> run the other APIs

response1 = WS.sendRequest(findTestObject(‘A’))

def slurper = new groovy.json.JsonSlurper()

def result = slurper.parseText(response1.getResponseBodyContent())

response2 = WS.sendRequest(findTestObject(‘B’, [(‘token’) : result.data.accessToken]))


#14

and do you have token variable in B TO?


#15

A is Login API to get token
B is example of the other APIs
=> (‘token’) is a global variable that you just set one time and use for all the APIs


#16

Thanks guys