如何在每个方案的XML中设置变量

发布于 2025-01-31 15:54:05 字数 1122 浏览 0 评论 0 原文

我在下面设置了此XML:

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

测试如下:

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

但是我似乎无法将此付费模式归于0 :(我已经阅读了

xml中的空手道API Pass Def

变量你peter!)

https://github.com/karatebs/karatelabs/karate#embedded-eppressions

简单,我敢肯定,这对我来说并不明显

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难得心□动 2025-02-07 15:54:05

一切对我来说都很好。但是:在声明XML变量“正文”之前,请确保您设置变量“付费模式”!也许这就是你错过的。

尝试此示例,该示例应该有效:

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


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

现在,如果您确实需要拥有XML“ pre set”,则可以使用 set 关键字进行这样的更新:

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

一个技巧是读取来自文件的XML,因此您将获得重新使用的好处,您似乎要追求:

* def paidMode = 'foo'
* def body = read('some.xml')

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文