Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

* 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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading