Trying to Use getElementsCount Keyword

When calling an API the response consists of a collection of Transactions. Each collection consists of 4 elements (Balance, Transaction Date, Type, and Amount)

[
{
“balance”: “404.01”,
“transactionDate”: “2020-02-19”,
“type”: “WITHDRAWAL”,
“amount”: “-40”
},
{
“balance”: “444.01”,
“transactionDate”: “2020-02-19”,
“type”: “WITHDRAWAL”,
“amount”: “-55.99”
}
]

Using JSONPathFinder I can see that each collection is assigned a “position”, for example the first is set consists of [0].balance, [0].transactionDate, [0].type, and [0].amount

I need to loop thru the returned response collection and then verify each element in each set.
I attempted to use the Web services keyword of getElementCount to determine the outer loop count but got an error on the call after I set it up

def slurper = new groovy.json.JsonSlurper()
def responseObj = slurper.parseText(requestObj.getResponseBodyContent())

TransCount = WS.getElementsCount(responseObj, null)

This is the error received

groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords.getElementsCount() is applicable for argument types: (java.util.ArrayList, null) values: [[[amount:-40, balance:404.01, transactionDate:2020-02-19, type:WITHDRAWAL], …], …]
Possible solutions: getElementsCount(com.kms.katalon.core.testobject.ResponseObject, java.lang.String), getElementsCount(com.kms.katalon.core.testobject.ResponseObject, java.lang.String, com.kms.katalon.core.model.FailureHandling)

There doesn’t seem to be any Help info on this call that I could find…has anyone successfully used this in setting up a loop?

1 Like

Hi @pamela.wittman,
The first parameter of getElementsCount must be a ResponseObject instance. And the second one is a string that describes your locator. We are sorry that the docs has not been updated with guidance for this keyword. However, you can refer to the doc of keyword verifyElementsCount, with similar parameters, to know the meaning and what value to define for each parameter (https://docs.katalon.com/katalon-studio/docs/ws-verify-elements-count.html#description)

thanks for the info. my 1st param is the response object but the API does not show a “locator” in the returned response - it is returned EXACTLY as shown below, so what can be used in this scenario? I tried “null” but that did not work…

[
{
“balance”: “404.01”,
“transactionDate”: “2020-02-19”,
“type”: “WITHDRAWAL”,
“amount”: “-40”
},
{
“balance”: “444.01”,
“transactionDate”: “2020-02-19”,
“type”: “WITHDRAWAL”,
“amount”: “-55.99”
},
{
“balance”: “500”,
“transactionDate”: “2020-02-19”,
“type”: “DEPOSIT”,
“amount”: “500”
}
]

Hi @pamela.wittman,
Try passing an empty string ("") as your locator.

this will return an ‘array’ of objects (in fact a list)

so, responseObj.size() should give you the number of objects, no need for locators.

Did anyone get an answer for this? There’s not much online about getElementsCount() method.

Thanks

You can check the Javadoc at:

https://docs.katalon.com/javadoc/com/kms/katalon/core/webservice/keyword/builtin/GetElementsCountKeyword.html

So, this keyword it is similar with VerifyElementCount in usage (as already mentioned), you have to provide and response object and an locator in the method signature (and optional an FailureHandling constant).
This keyword will return an integer.

For VerifyElementCount you have to provide in addition an expected value and will return a boolean.