@Trong_Bui @devalex88 @kazurayam @Zarashima
******** Is this keyword really working: WS.verifyElementsCount ********* please confirm 
I am using Katalon v6.1.6 Window 10 64 bit
I am testing a Rest API, and when verify the count, I am the below error:
WS.verifyElementsCount(response, ‘total_pages’, 1)
Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to verify element count (Root cause: groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.size() is applicable for argument types: () values:
Possible solutions: is(java.lang.Object), find(), with(groovy.lang.Closure), div(java.lang.Character), times(groovy.lang.Closure), div(java.lang.Number)
Below is my code
// https://reqres.in/api/users?page=2
def response = WS.sendRequest(findTestObject(‘RP/UserRestService/ListUser’))
WS.verifyElementPropertyValue(response, ‘page’, 2)
WS.verifyElementsCount(response, ‘total_pages’, 1)
Thanks for looking into this 
You can read the source code of WS.verifyElementsCount() at
By looking at the source, you would understand why the Exception (groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.size() is applicable for argument types: () values: [] ) occurs.
Well, I do not know. I do not see how this keyword SHOULD work as no specification is published.
Just I guess that your code WS.verifyElementsCount(response, ‘total_pages’, 1) is not what the designer intended to implement.
What did the designer intended? You would get a feel trying the following code:
def response = WS.sendRequest(findTestObject('reqres.in'))
println "${response}"
WS.verifyElementPropertyValue(response, 'page', 2)
//WS.verifyElementsCount(response, 'total_pages', 1) // this is a miss-use
WS.verifyElementPropertyValue(response, 'total_pages', 4)
WS.verifyElementsCount(response, 'data', 3) // size() works
WS.verifyElementsCount(response, 'data.id', 3) // also size() works
Just wanted to know… how to check “total pages” in above example.
Firstly… I sincerely thanks for all your effort. I highly appreciate it.
How to verify total pages.
I have the below response in one of my test, so how do I validate that no of id’s is 3 …?
[
{
“id”: 1,
“code”: “22G0”,
“name”: “20’ DC”,
“isReefer”: true
},
{
“id”: 2,
“code”: “22G1”,
“name”: “20’ DC (Pas. Vents)”,
“isReefer”: false
},
{
“id”: 4,
“code”: “22P3”,
“name”: “20’ Flat (Collapsible)”,
“isReefer”: false
}
]
Your target URL is https://reqres.in/api/users?page=2 . It returned the following JSON.
{
"page": 2,
"per_page": 3,
"total": 12,
"total_pages": 4,
"data": [
{
"id": 4,
"email": "eve.holt@reqres.in",
"first_name": "Eve",
"last_name": "Holt",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
},
{
"id": 5,
"email": "charles.morris@reqres.in",
"first_name": "Charles",
"last_name": "Morris",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
},
{
"id": 6,
"email": "tracey.ramos@reqres.in",
"first_name": "Tracey",
"last_name": "Ramos",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
}
]
}
And @focis.automation’s question is
My answer is
WS.verifyElementsCount(response, 'data', 3)
or
WS.verifyElementsCount(response, 'data.id', 3)
@discover.selenium asked
My answer is
WS.verifyElementPropertyValue(response, 'total_pages', 4)
How to check if ‘total_pages’ count is one.
In the below response how to check the count of ‘id’s’
[
{
“id”: 1,
“code”: “22G0”,
“name”: “20’ DC”,
“isReefer”: true
},
{
“id”: 2,
“code”: “22G1”,
“name”: “20’ DC (Pas. Vents)”,
“isReefer”: false
},
{
“id”: 4,
“code”: “22P3”,
“name”: “20’ Flat (Collapsible)”,
“isReefer”: false
}
]
Please try this
WS.verifyElementsCount(response, '', 3)
This code verifies the number of json objects contained in the root array; which is 3.
This code does not verifies the number of id profiles (which is 3) contained in the json objects contained in the root array.
I say so because I tried the follwing code and it worked.
import com.kms.katalon.core.webservice.helper.WebServiceCommonHelper
def jsonText = '''
[
{
"id": 1,
"code": "22G0",
"name": "20’ DC",
"isReefer": true
},
{
"id": 2,
"code": "22G1",
"name": "20’ DC (Pas. Vents)",
"isReefer": false
},
{
"id": 4,
"code": "22P3",
"name": "20’ Flat (Collapsible)",
"isReefer": false
}
]
'''
String locator = ''
String groovyFunction = 'size()'
Object ret = WebServiceCommonHelper.parseAndExecuteExpressionForJson(locator, "size()", jsonText)
println "ret=${ret}"
1 Like
The following code shows how to verify the size of array elements with id property is 3.
import groovy.json.JsonSlurper
def jsonText = '''
[
{
"id": 1,
"code": "22G0",
"name": "20’ DC",
"isReefer": true
},
{
"id": 2,
"code": "22G1",
"name": "20’ DC (Pas. Vents)",
"isReefer": false
},
{
"id": 4,
"code": "22P3",
"name": "20’ Flat (Collapsible)",
"isReefer": false
}
]
'''
def root = new JsonSlurper().parseText(jsonText)
assert root.findAll({ it.id }).size() == 3
// refer to http://docs.groovy-lang.org/next/html/documentation/core-semantics.html#gpath_expressions for GPath
1 Like