【问题标题】:Karate Xml response filtering空手道 Xml 响应过滤
【发布时间】:2022-10-21 14:51:55
【问题描述】:

在我的空手道测试中,我有如下 Xml 响应

<wkfcnf:workflow xmlns:wkfcnf="http://test.com/ri/configuration" name="test" status="ACTIVE" >
  <protocols>
    <protocol name="protocol1" uri="https://test/url/2"/>
    <protocol name="protocol2" uri="https://test/url/2"/>
  </protocols>
  <stages>
    <stage name="stage1" uri="https://stage/1"/>
   <stage name="stage2" uri="https://stage/1"/>
  </stages>
</wkfcnf:workflow>

我想根据协议名称获取协议 Uri。

以下是我尝试过的代码。但没有按预期工作。

* json prepProtocol = $response/workflow/protocols/protocol[(@.name='protocol1')]

对此的任何帮助表示赞赏。

谢谢 须弥陀

【问题讨论】:

    标签: karate


    【解决方案1】:

    XPath 可能很棘手。这对我有用:

    * def prepProtocol = $response//protocol[@name='protocol2']/@uri
    * match prepProtocol == 'https://test/url/2'
    

    编辑:如果您需要使用变量:

    * def name = 'protocol2'
    * def prepProtocol = karate.xmlPath(response, "//protocol[@name='" + name + "']/@uri")
    * match prepProtocol == 'https://test/url/2'
    

    你可以像这样变得更优雅一点:

    * def prepProtocol = karate.xmlPath(response, `//protocol[@name='${name}']/@uri`)
    

    【讨论】:

    • 这工作彼得..谢谢你。略有变化的是 $response//workflow//protocols//protocol[@name='protocol2']/@uri。如果我需要使用变量名而不是“protocol2”,语法是什么
    • @Subitha 进行了编辑以回答您的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多