Karate API在XML中传递def变量

时间:2017-12-07 12:44:37

标签: xml api soap karate

我用

定义了一个变量
* def token = '1bce02b8..'

我想检索我定义的变量,以便将其传递给我的SOAP请求。我怎样才能做到这一点?

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

1 个答案:

答案 0 :(得分:1)

很简单,在#(foo)表单中使用embedded expressions,它们也适用于XML:

* 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

打印哪些:

[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>

我建议您也参考这组示例,特别是XML,它会为您提供更多想法:xml.feature