How to get an element property value from an API response?

Hi,

How to get an element property value from an API response?
There is keyword ‘verifyElementPropertyValue’ which verifies an element property value with an expected value. But I want to get an element property value from an API response, save it in a variable and later compare this with a UI value.

Please let me know is there any keyword available to get an element property value.

Thanks,
Ravi B

1 Like

Hi ravi.b23,

Please review this post for your needs. You can get the response data and assign to the global variable.

Example script:

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

1 Like

Hi sang,

Thanks for your solution.
I want to get ‘amount’ value from balance node.
I tried below 2 methods but it is showing error.

String amtValue = parsedJson.get(“balance”.“amount”)
String amtValue = parsedJson.balance.amount
In balance node, there are many sub nodes like amount, type etc…

Please help me on how to get these kind of values.

Thanks,
Ravi B

Hi sang,

Though it is showing as error, I got the value by using below statement.

String amtValue = parsedJson.get(“balance”).get(“amount”)

Thanks,
Ravi B

1 Like

Hi @ravi.b23,
We will support more keywords for getting info from API response in version 7.2.0.
Regards.

1 Like

Are there any more functions to make it easier to extract fields from json responses yet?

1 Like

Hello, how can I use the userId_delete variable as a parameter in my DELETE request?

What I’m trying to do here is to create a user, and using the response body, I’ll get the userId value and delete that user that I just added.

response = WS.sendRequest(findTestObject(‘SMS/User_API/CreateStoreEmployee’))

WS.verifyResponseStatusCode(response, 200)

JsonSlurper slurper = new JsonSlurper()

Map parsedJson = slurper.parseText(response.getResponseText())

String actualUserId = parsedJson.userId

println(actualUserId)

GlobalVariable.userId_delete = actualUserId

println(userId_delete)

WS.sendRequest(findTestObject(‘SMS/User_API/DeleteUser’))

Console log:

2021-09-30 14:44:30.771 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2021-09-30 14:44:30.774 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/User_TestCase/CreateUser
2021-09-30 14:44:31.590 DEBUG testcase.CreateUser - 1: response = sendRequest(findTestObject(“SMS/User_API/CreateStoreEmployee”))
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
2021-09-30 14:44:42.150 INFO c.k.k.core.webservice.common.HarLogger - HAR: /var/folders/6f/s92243px74l5ylp6vql71tt40000gn/T/Katalon/Test Cases/User_TestCase/CreateUser/20210930_144415/requests/main/0.har
2021-09-30 14:44:42.247 DEBUG testcase.CreateUser - 2: verifyResponseStatusCode(response, 200)
2021-09-30 14:44:42.269 DEBUG testcase.CreateUser - 3: slurper = new groovy.json.JsonSlurper()
2021-09-30 14:44:42.281 DEBUG testcase.CreateUser - 4: parsedJson = slurper.parseText(response.getResponseText())
2021-09-30 14:44:42.337 DEBUG testcase.CreateUser - 5: actualUserId = userId
2021-09-30 14:44:42.350 DEBUG testcase.CreateUser - 6: println(actualUserId)
aa2e0c25-f8f2-4b32-b360-33e1c03d7e9f
2021-09-30 14:44:42.359 DEBUG testcase.CreateUser - 7: userId_delete = actualUserId
2021-09-30 14:44:42.360 DEBUG testcase.CreateUser - 8: println(userId_delete)
> 2021-09-30 14:44:42.375 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/User_TestCase/CreateUser FAILED.
> Reason:
> groovy.lang.MissingPropertyException: No such property: userId_delete for class: Script1632929324644
> at CreateUser.run(CreateUser:36)
> 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:430)
> at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:421)
> at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:400)
> at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:392)
> at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:273)
> at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
> at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
> at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
> at TempTestCase1632984255989.run(TempTestCase1632984255989.groovy:25)
**> **
> 2021-09-30 14:44:42.395 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/User_TestCase/CreateUser FAILED.
> Reason:
> groovy.lang.MissingPropertyException: No such property: userId_delete for class: Script1632929324644
> at CreateUser.run(CreateUser:36)
> 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:430)
> at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:421)
> at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:400)
> at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:392)
> at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:273)
> at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
> at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
> at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
> at TempTestCase1632984255989.run(TempTestCase1632984255989.groovy:25)
>
2021-09-30 14:44:42.412 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/User_TestCase/CreateUser

Here’s my DELETE request. I’m trying to use ${GlobalVariable.userId_delete} and I am getting a 500 Internal Server Error.

An empty Global Variable is also added to the profile.

https://${GlobalVariable.prod}/WebPimm5/Rest/SMS/Store/RemoveUserFromStore?userId=${GlobalVariable.userId_delete}&authorization=${GlobalVariable.auth}

The parameters dictionary contains a null entry for parameter ‘userId’ of non-nullable type ‘System.Guid’ for method ‘System.Web.Mvc.JsonResult RemoveUserFromStore(System.Guid)’ in ‘WebPimm.Areas.Rest.Controllers.SMS.StoreController’. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

Parameter name: parameters