如何在空手道中将XML响应与Json进行比较

时间:2018-11-25 19:38:39

标签: karate

我需要将JSON响应与下游XML响应进行匹配和验证。这是两者的样本响应。

请注意,Json响应参数与XML响应顺序不符。

JSON响应

"Main": {
    "Cd": "ABC",
    "descriptionTxt": "Sample Main",
    "type": "A",
    "codeType": "P",
    "dt": "2018-10-15T00:00:00-05:00",
    "validity": "3",
    "segment": "Personal"
    },
  "testList": [
    {
      "code": "123",
      "descriptionTxt": "My Description",
      "categoryCd": "DUDU"
    },
    {
      "code": "675",
      "descriptionTxt": "His Description"
    },
    {
      "code": "345",
      "descriptionTxt": "Your Description",
      "categoryCd": "BH"
    }
]

XML响应

<S:Body>
<ns4:code>ABC </ns4:code>
    <ns5:description>Sample Main</ns5:description>
    <ns5:Date>2018-10-15</ns5:Date>
    <ns5:Type>A</ns5:Type>
    <ns5:codeType>P</ns5:codeType>
    <ns5:validity>3</ns5:validity >
    <ns5:Segment>PERSONAL  </ns5:Segment>
    <ns5:unwanted>Unwanted XML Parameter</ns5:unwanted>

    <ns4:Test>
      <ns5:code>123   </ns5:code>
      <ns5:description>My Description</ns5:description>
      <ns5:categoryCode>DUDU</ns5:categoryCode>
      <ns5:unwanted>Unwanted XML Parameter</ns5:unwanted>
    </ns4:Test>

    <ns4:Test>
      <ns5:code>345   </ns5:code>
      <ns5:description>Your Description</ns5:description>
      <ns5:categoryCode>BH</ns5:categoryCode>
    </ns4:Test>

    <ns4:Test>
      <ns5:code>675  </ns5:code>
      <ns5:description>His Description</ns5:description>
      <ns5:unwanted>Unwanted XML Parameter</ns5:unwanted>
    </ns4:Test>

1 个答案:

答案 0 :(得分:2)

如果您花时间发布格式正确的JSON和XML,那就太好了,但是无论如何。我在这里集中讨论一个棘手的问题,这是将重复的XML元素映射到JSON,如果将以下内容粘贴到Scenario中,则可以看到它的工作原理:

* def json = 
"""
{
   "Main": {
      "Cd":"ABC",
      "descriptionTxt":"Sample Main",
      "type":"A",
      "codeType":"P",
      "dt":"2018-10-15T00:00:00-05:00",
      "validity":"3",
      "segment":"Personal"
   },
   "testList":[
      {
         "code":"123",
         "descriptionTxt":"My Description",
         "categoryCd":"DUDU"
      },
      {
         "code":"675",
         "descriptionTxt":"His Description"
      },
      {
         "code":"345",
         "descriptionTxt":"Your Description",
         "categoryCd":"BH"
      }
   ]
}
"""
* def xml = 
"""
<ns4:root xmlns:ns4="http://foo.com" xmlns:ns5="http://bar.com">
   <ns4:Test>
      <ns5:code>123</ns5:code>
      <ns5:description>My Description</ns5:description>
      <ns5:categoryCode>DUDU</ns5:categoryCode>
      <ns5:unwanted>Unwanted XML Parameter</ns5:unwanted>
   </ns4:Test>
   <ns4:Test>
      <ns5:code>345</ns5:code>
      <ns5:description>Your Description</ns5:description>
      <ns5:categoryCode>BH</ns5:categoryCode>
   </ns4:Test>
   <ns4:Test>
      <ns5:code>675</ns5:code>
      <ns5:description>His Description</ns5:description>
      <ns5:unwanted>Unwanted XML Parameter</ns5:unwanted>
   </ns4:Test>
</ns4:root>
"""
* def list = $xml/root/Test
* def xpath = function(x, p){ try { return karate.xmlPath(x, p) } catch (e) { return '#notpresent' } }
* def fun = function(x){ return { code: xpath(x, '/Test/code'), descriptionTxt: xpath(x, '/Test/description'), categoryCd: xpath(x, '/Test/categoryCode') } }
* def temp = karate.map(list, fun)
* print temp
* print json.testList
* match json.testList contains temp

映射其余的JSON对您来说是一个练习。请参考文档。另请参阅以下答案以获取更多想法:Karate - Match two dynamic responses