[WS] Get Element Property Value
Description
getElementPropertyValue(ResponseObject to, String string, FailureHandling flowControl)
Get the property value of an element (defined by a locator) in the data returned by a web service request.
Parameters
Parameter | Parameter Type | Mandatory | Description |
---|---|---|---|
response | ResponseObject | Required | The object that represents an HTTP response. |
string | String | Required | The element locator that Katalon uses to look for the expected data. To learn more about element locators, you can refer to this document: |
flowControl | FailureHandling | Optional | Specify failure handling schema to determine whether the execution should be allowed to continue or stop. |
Returns
Return type | Description |
---|---|
String | A String object that stores the property value. |
null | A null value if the element cannot be located. |
Example
The code snippet below shows you how to get the value of a property in a JSON object.
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.core.testobject.ConditionType as ConditionType
import com.kms.katalon.core.testobject.RequestObject as RequestObject
import com.kms.katalon.core.testobject.TestObjectProperty as TestObjectProperty
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WebAPI
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
// Send a REST API request and receive the response
def response = WS.sendRequest(findTestObject('Object Repository/GET user by id'))
// JSON in response: {"id":1,"username":"John Smith","password":"123","gender":"MALE","age":25,"avatar":null}
// Get the value of a property called 'username' in the JSON response
WS.getElementPropertyValue(response, 'username')