【问题标题】:How to validate specific node from SOAP XML response with several namespaces using Rest-assured?如何使用 Rest-assured 从具有多个命名空间的 SOAP XML 响应中验证特定节点?
【发布时间】: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


    【解决方案1】:

    根据REST Assured docs,以下内容应该适合您:

    .config(RestAssuredConfig.config()
    .xmlConfig(XmlConfig.xmlConfig()
    .with()
    .namespaceAware(true)
    .declareNamespace("soap-env", "http://schemas.xmlsoap.org/soap/envelope/")
    .declareNamespace("ns4", "someapi:com:plant")
    .declareNamespace("ns3", "http://www.testsite.com/common")))
    .post()
    .then()
    .statusCode(200)
    .body("soap-env:envelope.soap-env:body.ns4:findsiteconfigurationbysmth.ns4:response.ns3:site.text()", equalTo("QWERTY"));
    

    【讨论】:

    • 是的!它有效)非常感谢!但我不明白为什么要声明所需节点的整个路径。
    猜你喜欢
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多