1

I am checking a set of XML response values for a larger number of calls clustered in the same feature file. for some tests, the response contains one object, and for others it contains multiple objects. All tests used to pass. That is no longer the case, as now from karate version 1.0.0 the one object response tests fail on this step:

 And match each response/ObjectList/Object/value1== '#present'

This still passes

<ObjectList>
    <Object>
        <value1>6</value1>
    </Object>
    <Object>
        <value1>8</value1>
        <value2>9</value2>
    </Object>
</ObjectList>

This fails

<ObjectList>
        <Object>
            <value1>1</value1>
            <value2>2</value2>
        </Object>
</ObjectList>

Is this now deprecated functionality for XML? Or is this a bug?

Florin
  • 11
  • 1

1 Answers1

0

Your test does not make sense to me. What you are doing is the equivalent of:

* def response = ['foo', 'bar']
* match each response == '#notnull'

* def response = 'foo'
* match each response == '#notnull'

And the second match will fail because each expects an array or list on the Right Hand Side. Maybe there was a bug in previous versions of Karate, but I would rather not get into that now, it will be a waste of time.

Also if you read the docs, #present has no meaning if you use it outside of XML or JSON, and in my opinion - that's what you are doing here. The moment you do each Karate converts the Left Hand Side to a list and then does a match for each item. Maybe the documentation does not explain that nuance well enough, but you are welcome to submit a PR to improve it: https://github.com/intuit/karate#ignore-or-validate

I'll paste below what works for me, and leave it to you how you want to re-write your test:

* def response1 =
"""
<ObjectList>
    <Object>
        <value1>6</value1>
    </Object>
    <Object>
        <value1>8</value1>
        <value2>9</value2>
    </Object>
</ObjectList>
"""
* def temp1 = $response1/ObjectList/Object/value1
* match temp1 == ['6', '8']

* def response2 =
"""
<ObjectList>
    <Object>
        <value1>1</value1>
        <value2>2</value2>
    </Object>
</ObjectList>
"""
* def temp2 = $response2/ObjectList/Object/value1
* match temp2 == '1'

Also see this answer: https://stackoverflow.com/a/54241440/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248