【问题标题】:How to use a variable in JsonPath filter如何在 JsonPath 过滤器中使用变量
【发布时间】:2019-08-26 22:01:09
【问题描述】:

我正在尝试在空手道测试中使用 JsonPath 过滤器表达式的 RHS 中的变量,类似于:

* def name = 'A Name'
* def ids = $response[?(@.name == '#(name)')].id

如果我在表达式的 RHS 中使用文字字符串 'A Name',它会按预期工作。

我尝试了各种方法来评估变量: '<name>'"#(name)"

我怀疑这是因为我将 JsonPath 解析与空手道解析混合在一起?

【问题讨论】:

    标签: karate


    【解决方案1】:

    首先阅读:https://github.com/intuit/karate#rules-for-embedded-expressions

    您正在寻找的是:https://github.com/intuit/karate#jsonpath-filters

    * def ids = karate.jsonPath(response, "$.kittens[?(@.name=='" + name + "')].id")
    

    【讨论】:

    • 是的,我之前阅读了这两个文件,但我错过了关键的一方:-(: # using the karate object if the expression is dynamic * def temp = karate.jsonPath(cat, "$.kittens[?(@.name=='" + bob.name + "')]") 所以对于这个特定的情况,动态 JsonPath 表达式,空手道对象是必需/推荐的方法吗?
    • @kpschmidt 是的,没错。也看看karate.filter(),它在某些情况下可以更优雅地做同样的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 2015-10-05
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多