Possible to use getresponsetext and assertion without jsonslurper jsonoutput ?
i noticed jsonslurper and jsonparse tend to give me response in different structure.
i would like to use getresponsetext to get raw response.
the raw response is in array format. assertThat(test1List.data[0].testId).isEqualTo(“FF1”)
which i use the format assertThat(test1List.data[0].testId).isEqualTo(“FF1”)
however the assertion keep complaining the data[0] cannot be used.
No such property: data for class: java.lang.String
test scripts:
import static org.assertj.core.api.Assertions.*
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import internal.GlobalVariable
def SiteHostName = GlobalVariable.SiteHostName
def Port = GlobalVariable.Port
String requestMethod = “GET”
String endpoint1 = “http://${SiteHostName}:${Port}/api/items/test?testId=FF1/M2/P2”
RequestObject ro1 = new RequestObject()
ro1.setRestUrl(endpoint1)
ro1.setHttpHeaderProperties()
ro1.setRestRequestMethod(“GET”)
def test1 = WS.sendRequest(ro1)
WS.verifyResponseStatusCode(test1, 200)
def test1List = test1.getResponseText()
println(‘response text Test 1: \n’ + (test1List))
assertThat(test1List.data[0].testId).isEqualTo(“FF1”)
response attached here
response raw.txt (252 Bytes)
No is not. Is an object. Inside that object you have the key data, which is indeed an array.
you cannot use such assert on a string (getResponseText() is returning a string representation of your JSON response).
To reach a given element of the response, you have to parse it into a collection, therefore you need jsonslurper (or any other string to map/array library). If jsonslurper is not at your taste you can try google’s gson library.
Other option is to use the verifyElementPropertyValue keyword straight on the response object (without using getResponseText)
https://docs.katalon.com/katalon-studio/docs/ws-verify-element-property-value.html#description
I have tried WS.verifyElementPropertyValue
But it does not work. So I though I must use getresponsetext()
Or anything I missed syntax?
WS.verifyElementPropertyValue(test1, 'data[0].testId', 'FF1')
what is the error you get?
the syntax looks ok for my eyes
Reason:
groovy.json.JsonException: Expected no arguments, a single map, a single closure, or a map and closure as arguments.
i removed json slurper and json output. cleanup the code.
just purely uses below and no error now.
but i am not sure if the json response structure is always consistent or not.
the jsonslurper always give me different structure and assertion failed earlier.
thanks @anon46315158 I will monitor and report if again the structure changed.
def test11 = WS.sendRequest(ro11)
WS.verifyElementPropertyValue(test11, ‘data[0].drugId’, ‘FF1’)