1

I defined a variable with

* def token = '1bce02b8..'

I would like to retrieve the variable I defined so I can pass this to my SOAP request. How can I make this possible?

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.com">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:isValid>
         <token>$token</token>
      </ws:isValid>
   </soapenv:Body>
</soapenv:Envelope>
Skyx
  • 103
  • 12

1 Answers1

2

Easy, use embedded expressions in the #(foo) form, they work for XML as well:

* def token = 'foo'
* def payload =
"""
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.com">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:isValid>
         <token>#(token)</token>
      </ws:isValid>
   </soapenv:Body>
</soapenv:Envelope>
"""
* print payload

Which prints:

[print] <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.com">
  <soapenv:Header/>
  <soapenv:Body>
    <ws:isValid>
      <token>foo</token>
    </ws:isValid>
  </soapenv:Body>
</soapenv:Envelope>

I suggest you also refer to this set of examples, specifically for XML that will give you more ideas: xml.feature

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Thank you it's working! But I have the token variable from the previous scenario. The variable token is not accessible on the next scenario, how can I make this global? – Skyx Dec 07 '17 at 13:46
  • 1
    Yes, a common question, please refer to this answer, especially the comments: https://stackoverflow.com/a/47630187/143475 – Peter Thomas Dec 07 '17 at 13:47
  • Is it possible to change the global variable in the background? – Skyx Dec 11 '17 at 14:49
  • For example I have defined a global variable in the background * def Test = 'Hello' and I want to change this global variable in other scenario to Test = 'Welcome' – Skyx Dec 11 '17 at 14:54
  • No. the point of a `Background` is to guarantee all `Scenario`-s have the same initial state. It sounds like you really need a single `Scenario`. If you really insist on doing this, just call Java and manage your own singleton. – Peter Thomas Dec 11 '17 at 15:04
  • Thank you for helping me so far. I have one last question. How can I filter only the last variable in the request result. In this case I want the value of the token variable: NUTEST=com.intuit.karate.cucumber.FeatureWrapper@13c3c1e1, responseTime=6, response={soap:Envelope={_={soap:Body={ns2:loginResponse={_={return=a866fb4f-47e4-4895-ac13-2118b6c025c8}, @={xmlns:ns2=http://ws.openkm.com}}}}, @={xmlns:soap=http://schemas.xmlsoap.org/soap/envelope/}}}, responseStatus=200, __loop=-1, responseCookies=null, token=a866fb4f-47e4-4895-ac13-2118b6c025c8} – Skyx Dec 11 '17 at 15:41
  • I posted a new question thanks https://stackoverflow.com/questions/47756777/karate-get-one-variable-from-a-request – Skyx Dec 11 '17 at 16:04