Karate - How do I save XML response as XML to be used later?

Viewed 16

I am sending a request that returns a response in XML. When the response first comes back I can extract values from the XML as shown in the example below.

* def balanceAmount = $response/Envelope/Body/balanceAmount/amount
And print balanceAmount

However I then send another request so the above response is overwritten - so I need to save it as XML - so I can recall the values later on in the test.

I have tried many ways of doing this including the following:

* def xmlResponse = $response
* def xmlResponse = response
* xml xmlResponse = $response
* xml xmlResponse = response

But I can no longer access the values in the XML response like I could earlier.

I've tried these:

* def balanceAmount = xmlResponse/Envelope/Body/balanceAmount/amount
And print balanceAmount

* def balanceAmount = $xmlResponse/Envelope/Body/balanceAmount/amount
And print balanceAmount

None of the above works.

So how can I save the xml response as xml so I can extract values from it later on in the test after sending a different request?

1 Answers

It works fine for me, here try this stand-alone feature:

Feature:

  Scenario:
    * url 'https://httpbin.org/xml'
    * method get
    * def firstResponse = response

    * url 'https://httpbin.org/get'
    * method get

    * match firstResponse ==
    """
    <slideshow
        title="Sample Slide Show"
        date="Date of publication"
        author="Yours Truly">
        <slide type="all">
          <title>Wake up to WonderWidgets!</title>
        </slide>
        <slide type="all">
            <title>Overview</title>
            <item>Why <em>WonderWidgets</em> are great</item>
            <item/>
            <item>Who <em>buys</em> WonderWidgets</item>
        </slide>
    </slideshow>
    """
    * def firstSlide = $firstResponse/slideshow/slide[1]
    * match firstSlide == <slide type="all"><title>Wake up to WonderWidgets!</title></slide>

If you are still stuck, follow this process: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue

Related