柑橘 XPath 验证无法找到元素 [英] Citrus XPath Validation Cannot Find Element

查看:27
本文介绍了柑橘 XPath 验证无法找到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 ValidationFault 元素的 XML 负载.我的部分验证是确认 ValidationFault 元素在 XML 负载中仅出现一次.使用以下 Citrus Java DSL:

I have an XML payload with the element ValidationFault. Part of my validation is to confirm the ValidationFault element occurs only once in the XML payload. Using the following Citrus Java DSL:

runner.receive(action -> action.endpoint(endpointName)
      .validate("number://ValidationFault", 1));

我没有得到期望值 1,而是 0:

I do not get back the expected value of 1, instead 0:

com.consol.citrus.exceptions.TestCaseFailedException: Validation failed: Values not equal for element '//ValidationFault', expected '1' but was '0'

我已手动确认响应负载确实包含相关元素.我还使用外部工具验证了 XPath,发现 XPath 应该是正确的.我也尝试过使用名称空间 //soapenv:ValidationFault//:ValidationFault,但我收到了相同的异常.

I have manually confirmed that the response payload does contain the element in question. I also validated the XPath with an external tool and found that the XPath should be correct. I have tried with the namespaces as well, //soapenv:ValidationFault and //:ValidationFault, but I receive the same exception.

这是接收到的 XML 负载(删除了一些数据):

This is the XML payload that is received (with some data removed):

<?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
             xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
     <env:Header/>
     <SOAP-ENV:Body
              xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
              xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <soapenv:Fault>
         <faultcode>soapenv:Client</faultcode>
         <faultstring>ValidationFault</faultstring>
         <faultactor>GetSalutation</faultactor>
         <detail>
             <ValidationFault
                 fault:retryable="false"
                 xmlns="http://domain/fault/2.0/"
                 xmlns:fault="domain/fault/2.0/">
             <ErrorCode>flt-00001</ErrorCode>
             <DescriptionText>A Schema Validation Failed</DescriptionText>
              <Details>
                  <Text></Text>
              </Details
              <TransactionControlIdentificationID>
                  TBD
              </TransactionControlIdentificationID>
              <ZuluDateTime><ZuluDateTime>
          </ValidationFault>
      </detail>
  </soapenv:Fault>
  </SOAP-ENV:Body>
</soapenv:Envelope>

推荐答案

您需要使用命名空间上下文来声明 Xpath 表达式计算的命名空间前缀:

You need to use a namespace context that declares the namespace prefixes for the Xpath expression evaluation:

receive(action -> action.endpoint(fooChannel)
        .namespace("ns", "http://domain/fault/2.0/")
        .validate("number:count(//ns:ValidationFault)", 1));

Xpath 表达式默认计算为节点值.所以请确保使用 count() 函数来评估元素的数量.

The Xpath expression evaluates to the node values by default. So please make sure to use count() function in order to evaluate the number of elements.

作为替代方案,您可以评估节点集并使用 Hamcrest 匹配器 hasSize():

As an alternative to that you could evaluate to the node-set and use the Hamcrest matcher hasSize():

receive(action -> action.endpoint(fooChannel)
        .namespace("ns", "http://domain/fault/2.0/")
        .validate("node-set://ns:ValidationFault", Matchers.hasSize(1)));

这篇关于柑橘 XPath 验证无法找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆