WS.verifyElementsCount - wrong count


#1

Hi, everyone!

I have test-case of REST web-service where i try to count the number of templates(using WS.verifyElementsCount).
REST reponse is in XML:

<ArrayOfTemplate xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <template>
    <start_date>2119-01-04T00:00:00
    </start_date>
    <end_date xsi:nil="true" />
    <generate_blank_url>http://0001archtest01:1778/DocFlowApi/Document/GenerateBlank/PaymentContract_Test?templateDate=04.01.2119
    </generate_blank_url>
  </template>
  <template>
    <start_date>2018-11-06T00:00:00
    </start_date>
    <end_date>2119-01-04T00:00:00
    </end_date>
    <generate_blank_url>http://0001archtest01:1778/DocFlowApi/Document/GenerateBlank/PaymentContract_Test?templateDate=06.11.2018
    </generate_blank_url>
  </template>
</ArrayOfTemplate>

Code of test-case:

'Отправка валидного запроса'
a = WS.sendRequest(findTestObject('DocFlow_WebServices/GetTemplates', [('Category_Code') : 'PaymentContract_Test', ('boolean') : true]))
'Проверка количества шаблонов(2)'
WS.verifyElementsCount(a, 'template', 2)

Failures text:

Test Cases/Test_WebServices/Test_GetTemplates FAILED because (of) (Stack trace: com.kms.katalon.core.exception.StepFailedException: Unable to verify element count (Root cause: Expected elements count 2 is not equal with actual count 1)

What is wrong?


#2

Does anybody face with this problem?


#3

Hi @evgeny_solodukhin,
I’ve just addressed this issue to Katalon team and I’ll inform you as soon as it gets fixed.


#4

I tried to reproduce your problem.

I wrote a line in my testcase as this:

WS.verifyElementsCount(response, '//template', 2)

I guessed that the 2nd argument should be a valid XPath expression, so I wrote //template.

I ran the test case and got error. The message was a interesting one.

Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to verify element count (Root cause: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: unexpected token: def @ line 1, column 1.
    def //template = new XmlSlurper().parseText(xmlText); return //
template.size()
    ^

1 error

I would interpret this as follows:

The line in test case

WS.verifyElementsCount(response, 'template', 2)

will be transformed by Katalon Studio into the following Groovy code:

def template = new XmlSlurper().parseText(xmlText); return template.size()

return template.size() will always return 1 : the number of XML root element <ArrayOfTemplate>.

My conclusion

WS.verifyElementsCount() is poor.

I looked at the documentation https://docs.katalon.com/katalon-studio/docs/ws-verify-elements-count.html and found the specification of WS.veryElementsCount() is totally ambiguous. The document does not tell me how the locator param should be written (syntax) and what value the method should return (semantics).

I expect the keyword is able to process both of XML and JSON response. But obviously a locator for XML and a locator for JSON can not be the same. The locator parameter should be an expression of some Path-language: XPath for XML, JMESPath or JSONPath for JSON.

As the specification does NOT specifies how the keyword should be, the current implementation is just useless.


#5

In fact, we do not need WS.verifyElementsCount() to count the number of <template> element in the target XML document. In a test case, we can use javax.xml.xpath.XPath which is built-in the standard Java8 runtime.