Using variables in WS.sendRequest function


#1

I have a global variable set up:

Then I have my TestObject with the POST URL and local variables:

And below my testcase:

response = WS.sendRequest(findTestObject(‘REST examples/Simple examples/api-2-search/Search issues/Search issues by jql’,[(‘method’): ‘search’]))

def jsonSlurper = new JsonSlurper()

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

assertThat(jsonResponse.issues).hasSize(15)
assertThat(jsonResponse.issues[0].key).isEqualTo(‘KD-1’)
assertThat(jsonResponse.issues[14].key).isEqualTo(‘KD-15’)

When I execute this script, I get an error indicating that variable ${URL} was not set. However URL is a local variable of the POST TestObject with a default value.

Am i doing something wrong??


#2

Values for variables defined in the Rest object are considered only when you run it from the builder.
When running from test-case you have to bind them again, like you did for ‘method’.
try:

response = WS.sendRequest(findTestObject(‘REST examples/Simple examples/api-2-search/Search issues/Search issues by jql’,[(‘method’): ‘search’, ('URL'): GlobalVariable.restUrl]))

#3

Hi Ibus,

Your solutions works but I’m still confused. When I run my test case with this line:

response = WS.sendRequest(findTestObject(‘REST examples/Simple examples/api-2-search/Search issues/Search issues by jql’))

It works and the variables ‘URL’ and ‘method’, defined in the TestObject are used. Why should I list all the variables in the sendRequest function when I only want to change 1 variable?

I was under the assumption that it would work the same way, when executing a Testcase with the ‘callTestCase’ method.

When a Testcase uses local variables(varA and varB) with default values, and you want to execute this Testcase with a different value for varB, you only have to specify this variable with the new value in your ‘callTestCase’ function call like this:

WebUI.callTestCase(findTestCase(‘New Test Case’), [(’(‘varB’) : ‘newB’], FailureHandling.STOP_ON_FAILURE)

And it would still use the default value for varA.


#4

@Lukas_Boekenoogen not sure why like this, i am not using katalon very often (in fact lately i am not using it at all due to job change, just playing some time to time for fun) … but i have a hinch.

when you pass a binded variable … you have to pass them all, the ones not present in the parametrization from testcase are sent as null to the object. when you don’t bind any variable,the ones defined in the object are picked correctly as default values.
it may be a bug, i think there are some discussions on this matter

not sure how actualy the binding is running ‘under the hood’, perhaps somebody from dev team can take a deeper look.
@devalex88 any ideea who can bring some light? it may be how the request object class is implemented to pick or not dinamic kvars

@Lukas_Boekenoogen to confirm the behavior, try a case where you dont bind any variable to the object to see what is happening


#5

Hi Ibus,

What doe you exactly mean with:

to confirm the behavior, try a case where you dont bind any variable to the object to see what is happening


#6

do something like:

response = WS.sendRequest(findTestObject(‘REST examples/Simple examples/api-2-search/Search issues/Search issues by jql’))

so we can see if is getting the vars from the object when you don’t pass any binding … or not at all.


#7

Hi Ibus,

If I do

response = WS.sendRequest(findTestObject(‘REST examples/Simple examples/api-2-search/Search issues/Search issues by jql’))

It works and the variables ‘URL’ and ‘method’, defined in the TestObject are used.
So I think it’s a bug that you have the pass all the variables in your function call, if you want to change one.

@devalex88 any thoughts on this?