I'm using SOAP and testing it by Rest-Assured. I want to validate response body XML by Rest-Assured that it has expected node value. But I can't get node I need.
Here is my response XML. It has several namespaces. I want to get the value of this node ns3:site
<soap-env:envelope xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap-env:body>
<ns4:findsiteconfigurationbysmth xmlns:ns3="http://www.testsite.com/common" xmlns:ns2="http://www.testsite.com/plant" xmlns:ns4="someapi:com:plant" xmlns:ns5="someapi:com:reasoncode">
<ns4:response>
<ns2:ref>SiteWD:QWERTY</ns2:ref>
<ns3:site>QWERTY</ns3:site>
<ns3:description>test description</ns3:description>
<ns3:timezone>Africa/Abidjan</ns3:timezone>
</ns4:response>
</ns4:findsiteconfigurationbysmth>
</soap-env:body>
</soap-env:envelope>
Previously I saved my response to a String variable and produce my validation with this code.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document myXml = builder.parse(new InputSource(new StringReader(myStringXml)));
NodeList node = myXml.getDocumentElement()
.getElementsByTagNameNS("http://www.testsite.com/common", "site");
node.item(0).getTextContent();
This code works! Response is QWERTY
Now I'm trying to validate it by Rest-Assured.
.spec(defaultRequestSpecification(mySpec))
.config(RestAssuredConfig.config()
.xmlConfig(XmlConfig.xmlConfig()
.with().namespaceAware(true)
.declareNamespace("site", "http://www.testsite.com/common")))
.post()
.then()
.statusCode(200)
.body("site", equalTo("QWERTY"));
And my response is
1 expectation failed.
XML path site doesn't match.
Expected: QWERTY
Actual: SiteWD:QWERTYQWERTYtest descriptionAfrica/Abidjan
I tried to change declared namespace to "ns3", "ns3:site". And the same story with xPath in a body method - "ns3", "ns3:site", "site" and so on. The result is the same... A single string of text from ns3 nodes.
What I'm doing wrong? Please help me to figure out where is the problem. How to get only one node? What should I change?