1

I have this xml below which I setup in the Background:

* def Request = 
"""
                    <new1:Account>
                        <shar:PaidMode>#(PaidMode)</shar:PaidMode>
                        <shar:BillCycleCredit>#(BillCycleCredit)</shar:BillCycleCredit>
                        <shar:CreditCtrlMode>#(CreditCtrlMode)</shar:CreditCtrlMode>
                        <new1:BillCycleType>#(BillCycleType)</new1:BillCycleType>
                    </new1:Account>
"""

With my test as follows:

Scenario: Create first subscriber
    * def PaidMode = '0'
    And request Request 
    When method Post
    Then status 200
    * print Request

But I cant seem to get this PaidMode to be 0 :( I've read this

Karate API pass def variable in XML

which led me to this (thank you Peter!)

https://github.com/karatelabs/karate#embedded-expressions

its simple, I'm sure, its just not obvious to me what I'm doing wrong

Filburt
  • 17,626
  • 12
  • 64
  • 115
imp
  • 435
  • 6
  • 20

1 Answers1

1

Everything looks fine to me. But: make sure you set the variable 'paidMode' before you declare the xml variable 'body' ! Maybe that's what you missed.

Try this example, which should work:

* def paidMode = 'foo'
* def body = 
"""
<new1:Account>
    <shar:PaidMode>#(paidMode)</shar:PaidMode>
</new1:Account>
"""


* url 'https://httpbin.org/anything'
* request body
* method post

Now, if you really have a need to have the XML "pre set" you can use the set keyword to make an update like this:

* def body = 
"""
<new1:Account>
    <shar:PaidMode></shar:PaidMode>
</new1:Account>
"""
* set body/Account/PaidMode = 'foo'

One trick is to read the XML from a file, so you get the benefit of re-use which you seem to be going for:

* def paidMode = 'foo'
* def body = read('some.xml')
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I have almost 30 scenarios on the same request, reuse is exactly what I am going for Peter :) I'm going to play with this external file, as that xml was just an excerpt I have 9 to set per scenario making the 'set' way a little uglier to read than the 'read' way. – imp May 22 '22 at 08:39
  • just an fyi i had to keep `#(paidMode)` in the xml file but doing it that way works like a charm – imp May 22 '22 at 08:50
  • 1
    @imp have a look at this for more ideas: https://github.com/karatelabs/karate/blob/v1.2.0/karate-junit4/src/test/java/com/intuit/karate/junit4/xml/xml.feature – Peter Thomas May 22 '22 at 08:59
  • 1
    Wow. That table method looks amazing to create a customer base in a very readable format :) @Peter I've used a few automation tools (and created some frameworks using soapui,selenium,rest-assured and then moved it all to python :( ) and i must say this framework of yours is more amazing everytime i look at it :) Thank you for the link – imp May 22 '22 at 09:17