karate does not match xml with namespaces

梦想与她 提交于 2021-02-17 02:10:10

问题


I have two xml messages with namespaces which are essentially the same xml but match says they are not same.

Please look at the feature below. xmlString1 and xmlString2 are essentially same xmls. Only the namespace prefix is different. I am not sure if match matches xml equality? Is there a way I can achieve xml match with namespaces?

Feature: Test xml match
  Scenario: test xml match with namespaces
    * def xmlString1 =
    """
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:b="anotherUri">
      <soapenv:Header/>
      <soapenv:Body>
        <b:validateEmailAddressRequest>
          <request>
           <email>some@domain.com</email>
          </request>
         </b:validateEmailAddressRequest>
        </soapenv:Body>
     </soapenv:Envelope>
    """

    * def xmlString2 =
    """
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="anotherUri">
      <soapenv:Header/>
      <soapenv:Body>
        <a:validateEmailAddressRequest>
          <request>
           <email>some@domain.com</email>
          </request>
         </a:validateEmailAddressRequest>
        </soapenv:Body>
     </soapenv:Envelope>
    """

    * xml xml1  = xmlString1
    * xml xml2  = xmlString2

    * match xml1 == xml2

16:49:04.363 [main] ERROR com.intuit.karate - assertion failed: path: /soapenv:Envelope/_/soapenv:Body, actual: '<soapenv:Body><b:validateEmailAddressRequest><request><email>some@domain.com</email></request></b:validateEmailAddressRequest></soapenv:Body>', expected: '<soapenv:Body><a:validateEmailAddressRequest><request><email>some@domain.com</email></request></a:validateEmailAddressRequest></soapenv:Body>', reason: all key-values did not match, expected has un-matched keys: [a:validateEmailAddressRequest]
xml-match.feature:34 - path: /soapenv:Envelope/_/soapenv:Body, actual: '<soapenv:Body><b:validateEmailAddressRequest><request><email>some@domain.com</email></request></b:validateEmailAddressRequest></soapenv:Body>', expected: '<soapenv:Body><a:validateEmailAddressRequest><request><email>some@domain.com</email></request></a:validateEmailAddressRequest></soapenv:Body>', reason: all key-values did not match, expected has un-matched keys: [a:validateEmailAddressRequest]

回答1:


You are welcome to contribute a fix for this. Meanwhile you can do this. Note that you don't need to convert to xml, def xml1 is sufficient even if you use the triple-quote multi-line form. You may be confused with text.

* xmlstring xml2 = xml2
* xml xml2 = xml2.replaceAll('(</?)a:', '$1b:').replace('xmlns:a', 'xmlns:b')
* match xml1 == xml2


来源:https://stackoverflow.com/questions/57060082/karate-does-not-match-xml-with-namespaces

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!