You showed an XML like this:
The text “A2” is followed by a new line character. It is 0x41 0x32 0x0D 0x0A
in hex-decimal representation.
If it is really the case, your Custom Keyword had better to “normalize” whitespaces in the XML content text before testing equality to the text you expect; like this
(original)
return res && BuiltinKeywords.verifyEqual(
getXmlNodeText(responseText, locator),
text, flowControl)
(proposed)
return res && BuiltinKeywords.verifyEqual(
getXmlNodeText(responseText, locator).trim().replaceAll("\\s+", " "),
text, flowControl)
Here is my alternative test case implementation which uses javax.xml.xpath.XPath
rather than Grovvy’s GPath.
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
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 com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
ResponseObject response = WS.sendRequest((RequestObject)findTestObject('Stock-A25-XML-CAT - tuned')) // (1)
WS.verifyResponseStatusCode(response, 200)
WS.comment(response.getResponseText())
Document doc = createDocument(response.getResponseText())
WebUI.verifyEqual(evaluateXPathToString( doc, "normalize-space(//DocumentID[1])"), "A2")
/**
* create a W3C Document object from a XML text
*/
Document createDocument(String xml) {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
InputStream inputStream = new ByteArrayInputStream( xml.bytes )
return builder.parse(inputStream)
}
/**
* evaluate a XPath expression against the document, return a String
*/
String evaluateXPathToString(Document doc, String xpathExpression) {
XPath xpath = XPathFactory.newInstance().newXPath()
def result = xpath.evaluate( xpathExpression, doc, XPathConstants.STRING )
}
/**
* evaluate a XPath expression against the document, return a list of Strings
*/
List<String> evaluateXPathToListOfString(Document doc, String xpathExpression ) {
XPath xpath = XPathFactory.newInstance().newXPath()
def nodes = xpath.evaluate( xpathExpression, doc, XPathConstants.NODESET )
return nodes.collect { node -> node.textContent }
}
I think that this code is better than the one which uses GPath because I can utilise full power of the XPath technology, which is natively designed to process XML documents. For example, the XPath-builtin normalize-space()
function solves the aforementioned “redundant white spaces” problem cleanly.