问题
I'm attempting to use a variable in the RHS of an JsonPath filter expression in a Karate test, similar to this:
* def name = 'A Name'
* def ids = $response[?(@.name == '#(name)')].id
If I use the literal string 'A Name'
in the RHS of the expression it works as expected.
I've tried various ways to get the variable to evaluate:
'<name>'
, "#(name)"
, etc.
I suspect it is because I'm mixing JsonPath parsing with Karate parsing perhaps?
回答1:
Read this first: https://github.com/intuit/karate#rules-for-embedded-expressions
And what you are looking for is this: https://github.com/intuit/karate#jsonpath-filters
* def ids = karate.jsonPath(response, "$.kittens[?(@.name=='" + name + "')].id")
来源:https://stackoverflow.com/questions/57665464/how-to-use-a-variable-in-jsonpath-filter