WS.verifyElementText

Hi there,
I need to verify this S using this:

response = WS.sendRequestAndVerify(findTestObject(‘OR_ws03’))
WS.verifyElementText(response, ‘GenWs03ConsultadeDeudaCC.ExecuteResponse.Buenpaga’, ‘S’)
but the test failed , this is a message: Expected text is ‘S’ but actual element text is unable to verify
Any help? Thank you in advance

<SOAP-ENV:Envelope xmlns:SOAP-ENV=“http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsd=“XML Schema” xmlns:SOAP-ENC=“http://schemas.xmlsoap.org/soap/encoding/” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”>
SOAP-ENV:Body
<GenWs03ConsultadeDeudaCC.ExecuteResponse xmlns=“GeoTribUy”>
S

<SDT_ColeccionCobranzaItem>
N
S
2023
19/07/23
301
CHAPA

703.00
1
</SDT_ColeccionCobranzaItem>

</GenWs03ConsultadeDeudaCC.ExecuteResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

This single line is very ambiguous. I do not see what you want to do.


Your code

WS.verifyElementText(response, ‘GenWs03ConsultadeDeudaCC.ExecuteResponse.Buenpaga’, ‘S’)

expects that your SOAP Response XML would have the code like

...
<GenWs03ConsultadeDeudaCC>
    <ExecuteResponse>
        <Buenpaga>S<Buenpaga>
    </ExecuteResponse>
</GenWs03ConsultadeDeudaCC>
...

but the code you showed to us does not contain such code. So your test fails inevitably.

@daimilamoru2018

You shoud read the doc carefully.

https://docs.katalon.com/docs/create-tests/keywords/keyword-description-in-katalon-studio/web-service-keywords/ws-verify-element-text

Katalon checks if the XML element content text is strictly equal to the expected value. For example, if there is whitespace after the Approved value, you need to add it to the third argument as follows:

I am afraid that this keyword has very limited usability. It may not be useful for you.

Thank you very much for your response. What I want to verify is that the value in this tag is “S”. This is my XML

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SOAP-ENV:Body>
    <GenWs03ConsultadeDeudaCC.ExecuteResponse xmlns="GeoTribUy">
      <Buenpaga>S</Buenpaga>
      <Sdtcoleccioncobranza>
        <SDT_ColeccionCobranzaItem>
          <Paga>N</Paga>
          <Permitido>S</Permitido>
          <Anio>2024</Anio>
          <Vencimiento>20/11/24</Vencimiento>
          <CodigoConcepto>200</CodigoConcepto>
          <Concepto>PATENTE</Concepto>
          <Cuota>6/6</Cuota>
          <Importe>1333.33</Importe>
          <Linea>1</Linea>
        </SDT_ColeccionCobranzaItem>
        <SDT_ColeccionCobranzaItem>
          <Paga>N</Paga>
          <Permitido>N</Permitido>
          <Anio/>
          <Vencimiento/>
          <CodigoConcepto>301</CodigoConcepto>
          <Concepto>ADELANTO DE CUOTAS PAT</Concepto>
          <Cuota/>
          <Importe>-133.33</Importe>
          <Linea>2</Linea>
        </SDT_ColeccionCobranzaItem>
      </Sdtcoleccioncobranza>
      <Auxiliarcobro>1046147</Auxiliarcobro>
      <Codigoretorno>000</Codigoretorno>
      <Mensajeretorno/>
    </GenWs03ConsultadeDeudaCC.ExecuteResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I have already read it and that is exactly what I want to do, checking the value inside this tag:
<Buenpaga>S</Buenpaga>

Your XML has an element of which name contains a dot character (.)

A dot character (.) is valid to be a part of XML Name. It is OK. The spec allows it. But ordinary people rarely use a dot in XML element name. In this sence, your target XML is unusual.

On the other hand, the Katalon keyword uses a notation like GPath. They named it as locator. The documentation gives an example of locator, like

'PreAuthorizeResponse.Receipt.TransactionResult'

You should note that all dot characters ( . ) in a locator stands for the boundary of XML elements. This locator correcponds to the target XML like:

<env:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Body>
    <ns:PreAuthorizeResponse xmlns:ns="beep" xmlns:ns2="bop" xmlns:ns1="foo" >
      <ns:Receipt>
        <ns1:DataKey>123</ns1:DataKey>
        <ns1:CustomerId>12345</ns1:CustomerId>
        <ns1:PaymentId>123456</ns1:PaymentId>
        <ns1:TransactionResult>Approved</ns1:TransactionResult>
...

Now, you should note that the locator syntax implicitly assumes that the target XML document has NO elements that contain dot characters in the element name.


Now let me review your case.

Your code

WS.verifyElementText(response, ‘GenWs03ConsultadeDeudaCC.ExecuteResponse.Buenpaga’, ‘S’)

This expects a XML element structure to be something like this:

...
<GenWs03ConsultadeDeudaCC>
    <ExecuteResponse>
        <Buenpaga>S</Buenpaga>
    ...

This structure is different from your target XML actually is. Therefore, Katalon’s keyword WS.verifyElementText(response, locator, expectedValue) can NEVER be correct against your target XML which uses a dot character (.) in the Name.

Any workaround? — You can not rely on WS.xxx keywords. So you need to write a tons of XML processing in Groovy. You would want to start learning the doc

http://groovy-lang.org/processing-xml.html

and develop an art. You have to avoid the friendly “GPath” features. Possibly you would want to use XPath 1.0 paying enough attention to XML Namespace. I am afraid, this would be a difficult objective. I can not give you a quick and easy answer (as there is none).

My conclusion: Your target XML is unusual. It’s too difficult to process using WS.* keywords. You had better abandun your objective in Katalon Studio.

If your target XML is something like:

    <GenWs03ConsultadeDeudaCC_ExecuteResponse xmlns="GeoTribUy">
      <Buenpaga>S</Buenpaga>

Then the following code would work fine:

WS.verifyElementText(response, ‘GenWs03ConsultadeDeudaCC_ExecuteResponse.Buenpaga’, ‘S’)

Please note an underline ( _ ) is used instead of a dot ( . ) in the element name.

But actually your case is different. It is not as easy as the assumed case unfortunately.

If possible, you should ask the developer(s) of the SOAP application to refrain from a dot character in XML Name as it makes many things difficult. I guess, they would be able to change a dot to an underbar with ease.

Thank you very much for your response. That xml is the one used in my work and it works fine, but in the automation it has brought me that problem I mentioned before.
I will try to find a solution to this problem.

Let me show you my art of processing a XML document using the DOM & XPath API, which is runnable in Katalon Studio but uses none of Katalon’s features.

I saved the xml you presented above into a file <projectDir>/response.xml in a Katalon project.

I made a Test Case “TC1” like this:

import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.Files

import javax.xml.namespace.NamespaceContext
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.XPath
import javax.xml.xpath.XPathConstants
import javax.xml.xpath.XPathFactory

import org.w3c.dom.Document
import org.w3c.dom.NodeList

import com.kms.katalon.core.configuration.RunConfiguration

Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path xml = projectDir.resolve("response.xml")
assert Files.exists(xml)

FileInputStream fileIS = new FileInputStream(xml.toFile())
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance()
builderFactory.setNamespaceAware(true)
DocumentBuilder builder = builderFactory.newDocumentBuilder()
Document xmlDocument = builder.parse(fileIS)

XPath xPath = XPathFactory.newInstance().newXPath()
xPath.setNamespaceContext(new NamespaceContext() {
	@Override
	public Iterator getPrefixes(String arg0) {
		return null;
	}
	@Override
	public String getPrefix(String arg0) {
		return null;
	}
	@Override
	public String getNamespaceURI(String arg0) {
		if ("my".equals(arg0)) {
			return "GeoTribUy"
		} else if ("SOAP-ENV".equals(arg0)) {
			return "http://schemas.xmlsoap.org/soap/envelope/"
		}
		return null
	}
})

String expr1 = "/SOAP-ENV:Envelope/SOAP-ENV:Body"
NodeList nodeList = (NodeList) xPath.compile(expr1).evaluate(xmlDocument, XPathConstants.NODESET)
println "nodeList.getLength()=" + nodeList.getLength()
assert nodeList.getLength() > 0


String expr2 = "/SOAP-ENV:Envelope/SOAP-ENV:Body/my:GenWs03ConsultadeDeudaCC.ExecuteResponse/my:Buenpaga";
String s = (String) xPath.compile(expr2).evaluate(xmlDocument, XPathConstants.STRING);
println s
assert s == "S"

I ran it and got the following output in the console:

2023-05-19 10:23:39.032 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2023-05-19 10:23:39.036 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/TC1
2023-05-19 10:23:40.565 DEBUG testcase.TC1                             - 1: projectDir = Paths.get(getProjectDir())
2023-05-19 10:23:40.580 DEBUG testcase.TC1                             - 2: xml = projectDir.resolve("response.xml")
2023-05-19 10:23:40.599 DEBUG testcase.TC1                             - 3: assert Files.exists(xml)
2023-05-19 10:23:40.637 DEBUG testcase.TC1                             - 4: fileIS = new java.io.FileInputStream(xml.toFile())
2023-05-19 10:23:40.682 DEBUG testcase.TC1                             - 5: builderFactory = DocumentBuilderFactory.newInstance()
2023-05-19 10:23:40.708 DEBUG testcase.TC1                             - 6: builderFactory.setNamespaceAware(true)
2023-05-19 10:23:40.727 DEBUG testcase.TC1                             - 7: builder = builderFactory.newDocumentBuilder()
2023-05-19 10:23:40.773 DEBUG testcase.TC1                             - 8: xmlDocument = builder.parse(fileIS)
2023-05-19 10:23:40.837 DEBUG testcase.TC1                             - 9: xPath = newInstance().newXPath()
2023-05-19 10:23:40.858 DEBUG testcase.TC1                             - 10: xPath.setNamespaceContext(new Script1684456048423$1(this))
2023-05-19 10:23:40.876 DEBUG testcase.TC1                             - 11: expr1 = "/SOAP-ENV:Envelope/SOAP-ENV:Body"
2023-05-19 10:23:40.887 DEBUG testcase.TC1                             - 12: nodeList = compile(expr1).evaluate(xmlDocument, NODESET)
2023-05-19 10:23:40.981 DEBUG testcase.TC1                             - 13: println("nodeList.getLength()=" + nodeList.getLength())
nodeList.getLength()=1
2023-05-19 10:23:41.028 DEBUG testcase.TC1                             - 14: assert nodeList.getLength() > 0
2023-05-19 10:23:41.032 DEBUG testcase.TC1                             - 15: expr2 = "/SOAP-ENV:Envelope/SOAP-ENV:Body/my:GenWs03ConsultadeDeudaCC.ExecuteResponse/my:Buenpaga"
2023-05-19 10:23:41.034 DEBUG testcase.TC1                             - 16: s = compile(expr2).evaluate(xmlDocument, STRING)
2023-05-19 10:23:41.085 DEBUG testcase.TC1                             - 17: println(s)
S
2023-05-19 10:23:41.094 DEBUG testcase.TC1                             - 18: assert s == "S"
2023-05-19 10:23:41.147 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/TC1

I could get access to the element and asserted its content text being “S”.

I appreciate your time in helping me. So it looks like I will have to do a lot of coding to automate the checks on that xml. I don’t know why they did it this way

Yes. Quite a lot to do.

This is given. All you could do is to embrace it.

Or, if you could change the dot character ( . ) to an uderbar ( _ ), then things will be far easier.

You can find the source code of Katalon Studio published at GitHub.

I could find the source of WS.verifyElementText() keyword and misc helpers. One of them is WebServiceCommonHelper#parseAndExecuteExpressionForXml(String locator, ...) at LINE#94

public static Object parseAndExecuteExpressionForXml(String locator, String groovyFunction, String xmlText){
		String[] tokens = locator.split("\\.");
        ...

This code will parse the given locator string

GenWs03ConsultadeDeudaCC.ExecuteResponse.Buenpaga

into an array of String:

[ "GenWs03ConsultadeDeudaCC", "ExecuteResponse", "Buenpaga" ]

This array is no longer relevant to the structure of the target XML document. So, the WS.verifyElementText keyword is not applicable to the XML document which @daimilamoru2018 wants to verify.


I wrote those posts to this issue because I think that the Webservice features of Katalon Studio is carelessly designed, poorly implemented. You shouldn’t waste your time trying it.

I had a look at the document of Postman

I found a sample code that verifies a SOAP response:

pm.test("InvertStringCase: AbCdE gets inverted to aBcDe", () => {
  // Convert XML to JSON

  var jsonObject = xml2Json(responseBody);

  // Move through the JSON tree to find the object that contains the inverted cased string

  var numberToWord = jsonObject["soap:Envelope"]["soap:Body"]["m:InvertStringCaseResponse"]["m:InvertStringCaseResult"];

  // Return a boolean where True means the test passed and False means the test failed

  return numberToWord == "aBcDe";
});

I guess, the Javascript code would work even if the target XML has an element of which name contains a dot character. Possibly @daimilamoru2018 would be able to write:

  var s = jsonObject["soap:Envelope"]["soap:Body"]["GenWs03ConsultadeDeudaCC.ExecuteResponse"]["Buenpaga"];

A dot character in XML element name would not hurt you in Postman.

Thank you so much again. If you have any private contact, please share it with me in order to discuss with more details.

No, I am not willing to exchange private messages. I prefer to make all my posts exposed public.