【问题标题】:Karate xml bodyPath not being matched in mock: scenario match evaluation failed空手道 xml bodyPath 在模拟中不匹配:场景匹配评估失败
【发布时间】:2019-12-19 17:26:17
【问题描述】:

这是请求的样子:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
   ...
    <SOAP-ENV:Header>
        <ns2:Security SOAP-ENV:mustUnderstand="1">
            <ns2:UsernameToken>
            ...
            </ns2:UsernameToken>
        </ns2:Security>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:customerQualificationRequest>
            <ns1:header>
                ...
            </ns1:header>
            <ns1:creditApplication>
            ...
                <ns1:lastName>Shopping</ns1:lastName>
        ...
      </ns1:creditApplication>
      </ns1:customerQualificationRequest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

还有模拟:

  Scenario: pathMatches('<path>') && requestHeaders['SOAPAction'][0] == '<soapAction>' && bodyPath('/ns1:customerQualificationRequest/ns1:creditApplication/ns1:lastName') == 'Shopping'

如果我只删除 bodyPath,但找不到与 bodyPath 匹配的内容,它会起作用。我需要有几个 lastName 不同的情况,因为答案会不同,所以我需要匹配那个参数。 我做错了什么?

【问题讨论】:

  • bodyPath('//ns1:customerQualificationRequest/ns1:creditApplication/ns1:lastName') 可以通过选择所有 customerQualificationRequest 标记来工作,无论它们在 XML 文档中的什么位置。指定从根开始的具体路径 - /SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:customerQualificationRequest/ns1:creditApplication/ns1:lastName - 是一种“更安全”的方式。不过,在这两种情况下,都必须有一种方法可以为标签前缀(ns1SOAP-ENV)指定命名空间(xmlns

标签: xml soap mocking karate


【解决方案1】:

我希望 XML 更简单。这是我刚想出来的。您可以在 XML 上使用 Json-Path,有时效果会更好。举个例子(普通空手道测试,不是模拟):

* def req =
"""
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
      <ns1:customerQualificationRequest>
        <ns1:creditApplication>
          <ns1:lastName>Shopping</ns1:lastName>
        </ns1:creditApplication>
      </ns1:customerQualificationRequest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
"""
* def temp = karate.get('$req..ns1:lastName')
* match temp == ['Shopping']

这意味着这应该有效:

bodyPath('$..ns1:lastName').length > 0 && bodyPath('$..ns1:lastName')[0] == 'Shopping'

由于这很笨重,您可以在Background 中定义自定义函数:

Background:
* def getLastName = function(){ var temp = karate.get('$request..ns1:lastName'); return temp.length > 0 ? temp[0] : null }

然后:

getLastName() == 'Shopping'

【讨论】:

    【解决方案2】:

    苦苦挣扎了一阵子,终于找到了答案。以下工作: && bodyPath('/Envelope/Body/customerQualificationRequest/creditApplication/lastName')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      相关资源
      最近更新 更多