-1

I've noticed that "match contains" for checking if xml structure contains in another xml structure works differently than json counterpart. To give you an example :

          * def expectedResult =
            """
              <?xml version="1.0" encoding="UTF-8"?>
              <response>
                <id>1</id>
                <id>2</id>
                <id>3</id>
              </response>
            """
          * header Accept = xml
      Given path 'some\path'
       When method GET
       Then status 200
       ## If response contains - 
       ## <response><id>1</id></response>
       
       ## The following match will fail with - 
       ## "actual and expected arrays are not the same size - 1:3"
        And match response contains expectedResult

If it's a JSON structure, it will pass because 1 contains in the list of 1,2,3. But in XML, it's still checking array size. Is that how it suppose to work?

Update : This is not the same question as the other question. I'm trying to compare reference XML file that I have with XML structure being returned from the API. The question you linked to is comparing JSON reference file to XML response. So, that begs the question, in karate, do I have to convert my reference XML and response XML to json just to perform "contains"?

KMC
  • 1,677
  • 3
  • 26
  • 55

1 Answers1

1

XML has some fundamental "shape" differences from JSON. I leave it to you to figure out, one of the challenges is the concept of a "root" tag, which JSON does not have. So they will not "match up" the way you may expect.

One tip - convert XML to JSON if in doubt. Here is an example:

* def expected =
"""
<?xml version="1.0" encoding="UTF-8"?>
<response>
  <id>1</id>
  <id>2</id>
  <id>3</id>
</response>
"""
* json temp = $expected/response
* match temp.response.id == ['1', '2', '3']
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I understand the root tag concept. The part that baffles me is that why is XML checking number of elements in addition to if given element(s) contains in the structure? Converting JSON means converting not just the response from API but also the big reference xml file which i use to compare against with response. I prefer not to if can use XML as is – KMC Jun 08 '21 at 12:16