Please show me way to use GlobalVariable as parameter in web service object

When defining parameter for web service object, I see an option in Type column that is “Global Variable”. As my understanding, I can choose a global variable and its value will be set to the parameter. But when running test case, it seems like the parameter value is not replaced by global variable value. Please refer to the screen shot.
45%20PM
59%20PM
I’m not sure it’s the right way, so if anybody used to use Global variable in web service object before, please help me.

the purpose of variables tab in the request object is just to provide some default values in the builder only for quick testing.
so they will be substituted only when you run the request from the object builder.

From the testcase, you have to call the object by binding again the desired globals, e.g:

request = findTestObject('yourtestObjectName', [('protocol') : GlobalVariable.PROTOCOL, ('url') : GlobalVariable.BO_BASE_URL, ... and so on])

So you mean that if I define a variable name “username” and set its default value “abc” in variable tab, when finding this object in my code, I still have to pass the binding [username: ‘abc’] again?

If it’s true, I really really want to have a feature request to make this “default” values be real default, i.e: when user does not pass value to those variables, default values will be used.

@phuonglam Yes, this is how it is working at this moment (at least in 5.9.0, is the version i use for now)
Agreed, will be nice if the implementation can be improved to pick-up automatically the values declared in the tab as defaults if they are not provided from the script

Even following the steps above, I am not able to use a global variable in a WS request.

WS.sendRequest(findTestObject('getVisitTemplateVisits', [('baseURL') : GlobalVariable.baseURL]))

image

I get the following stack trace:

05-22-2019 09:43:03 AM sendRequest(getVisitsRequest)

Elapsed time: 0.086s

Unable to send request (Root cause: java.net.MalformedURLException: no protocol: /data/config/cr_protocol/register?contents=true

at com.kms.katalon.core.webservice.common.RestfulClient.sendRequest(RestfulClient.java:65)

at com.kms.katalon.core.webservice.common.RestfulClient.send(RestfulClient.java:51)

at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword$_sendRequest_closure1.doCall(SendRequestKeyword.groovy:55)

at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword$_sendRequest_closure1.call(SendRequestKeyword.groovy)

at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:68)

at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword.sendRequest(SendRequestKeyword.groovy:50)

at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword.execute(SendRequestKeyword.groovy:45)

at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:56)

at com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords.sendRequest(WSBuiltInKeywords.groovy:40)

at com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords$sendRequest.call(Unknown Source)

at New Test Case (1).run(New Test Case (1):17)

at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)

at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)

at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)

at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)

at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)

at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)

at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)

at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)

at TempTestCase1558536180457.run(TempTestCase1558536180457.groovy:21)

)

Help please?

Hey @Patrick_Clough,

On the request object, you must click on the ‘Variables’ tab under the query parameters section. Here, click ‘Add’ and give the entry Name ‘baseURL’ and select Type String. Double checked on Katalon 6.1.5 and it’s working for me.

Shoot, didn’t work for me.

image

05-23-2019 09:26:46 AM Test Cases/temp/New Test Case (1)

Elapsed time: 1.089s

Test Cases/temp/New Test Case (1) FAILED.

Reason:

com.kms.katalon.core.exception.StepFailedException: Unable to send request (Root cause: java.net.MalformedURLException: no protocol: /data/config/cr_protocol/register?contents=true

at com.kms.katalon.core.webservice.common.RestfulClient.sendRequest(RestfulClient.java:65)

at com.kms.katalon.core.webservice.common.RestfulClient.send(RestfulClient.java:51)

at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword$_sendRequest_closure1.doCall(SendRequestKeyword.groovy:55)

at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword$_sendRequest_closure1.call(SendRequestKeyword.groovy)

at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:68)

at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword.sendRequest(SendRequestKeyword.groovy:50)

at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword.execute(SendRequestKeyword.groovy:45)

at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:56)

at com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords.sendRequest(WSBuiltInKeywords.groovy:40)

at com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords$sendRequest.call(Unknown Source)

at New Test Case (1).run(New Test Case (1):19)

at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)

at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)

at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)

at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)

at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)

at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)

at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)

at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)

at TempTestCase1558621603763.run(TempTestCase1558621603763.groovy:21)

)

at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy:50)

at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy)

at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:70)

at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword.sendRequest(SendRequestKeyword.groovy:50)

at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword.execute(SendRequestKeyword.groovy:45)

at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:56)

at com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords.sendRequest(WSBuiltInKeywords.groovy:40)

at com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords$sendRequest.call(Unknown Source)

at New Test Case (1).run(New Test Case (1):19)

at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)

at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)

at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)

at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)

at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)

at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)

at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)

at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)

at TempTestCase1558621603763.run(TempTestCase1558621603763.groovy:21)

Caused by: java.net.MalformedURLException: no protocol: /data/config/cr_protocol/register?contents=true

at com.kms.katalon.core.webservice.common.RestfulClient.sendRequest(RestfulClient.java:65)

at com.kms.katalon.core.webservice.common.RestfulClient.send(RestfulClient.java:51)

at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword$_sendRequest_closure1.doCall(SendRequestKeyword.groovy:55)

at com.kms.katalon.core.webservice.keyword.builtin.SendRequestKeyword$_sendRequest_closure1.call(SendRequestKeyword.groovy)

at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:68)

... 17 more

Strange. Just to confirm, you’re running the script with the profile containing this ‘baseURL’ selected in the drop-down menu and ‘baseURL’ is defined with a value? Does it work if instead of GlobalVariable.baseURL, you give it a string value in ’ '?

Face palm! That was the issue.

However, now that it runs, I’m not getting back what I expected. When I set this request to a string and println the string, I get back the response code (200 in my case). How can I get the reponse body back?

Ah, I figured it out. What is returned is not a string, but a response object. The response object has all sorts of stuff in it that I can pull out, inluding the body text.

Eg,

def getVisits = WS.sendRequest(findTestObject('getVisitTemplateVisits', [('baseURL') : GlobalVariable.baseURL]))

bodyContents = getVisits.getResponseBodyContent()
println bodyContents

One last question. Have you (@Justin_Keller) or anyone else been able to paramaratize the authorization for one of these WS requests? Passing variables into the authorization fields doesn’t seem to do the trick.

Implementation depends on what type of authorization you need but check out this example. Basically you need to create an ArrayList of TestObjectProperty and add your token to the request object’s HTTPHeader.