【发布时间】:2019-08-21 09:11:56
【问题描述】:
我正在使用 SOAP 并由 Rest-Assured 对其进行测试。 我想通过 Rest-Assured 验证响应正文 XML 是否具有预期的节点值。但我无法获得我需要的节点。
这是我的响应 XML。它有几个命名空间。 我想获取这个节点的值 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>
之前我将我的响应保存到一个字符串变量并使用此代码生成我的验证。
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();
此代码有效!回复是 QWERTY
现在我正在尝试通过 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"));
我的回答是
1 expectation failed.
XML path site doesn't match.
Expected: QWERTY
Actual: SiteWD:QWERTYQWERTYtest descriptionAfrica/Abidjan
我尝试将声明的命名空间更改为“ns3”、“ns3:site”。与 xPath 在 body 方法中的情况相同——“ns3”、“ns3:site”、“site”等等。结果是一样的......来自ns3节点的单个文本字符串。
我做错了什么?请帮我找出问题所在。 如何只获得一个节点?我应该改变什么?
【问题讨论】:
标签: java xml xpath soap rest-assured