数组元素不返回结果,但在匹配条件下通过

时间:2019-08-15 22:35:31

标签: karate

尝试以JSON打印数组元素在空手道DSL中似乎不起作用-想了解我是否错过了这里吗?

尝试运行https://github.com/intuit/karate#json-arrays

中描述的JSON数组部分
Scenario: Test
    Given def cat =
  """
  {
    name: 'Billie',
    kittens: [
      { id: 23, name: 'Bob' },
      { id: 42, name: 'Wild' }
    ]
  }
  """
  * print ('\n')
  * print ('printing all kittens - working')
  * print cat.kittens
  * print ('printing all id of kittens - not working')
  * print cat.kittens[*].id

当执行为java -jar karate-0.9.0.jar test.feature时返回结果为

23:30:29.778 [ForkJoinPool-1-worker-1] WARN  com.intuit.karate - skipping bootstrap configuration: could not find or read file: classpath:karate-config.js
23:30:29.893 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - [print]

23:30:29.899 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - [print] printing all kittens - working
23:30:29.921 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - [print] [
  {
    "id": 23,
    "name": "Bob"
  },
  {
    "id": 42,
    "name": "Wild"
  }
]

23:30:29.935 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - [print] printing all id of kittens - not working
23:30:29.944 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - [print]
23:30:30.022 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate.Runner - <<pass>> feature 1 of 1: test.feature
---------------------------------------------------------
feature: test.feature
report: target\test.json
scenarios:  1 | passed:  1 | failed:  0 | time: 0.1339
---------------------------------------------------------
Karate version: 0.9.0
======================================================
elapsed:   1.28 | threads:    1 | thread time: 0.13
features:     1 | ignored:    0 | efficiency: 0.10
scenarios:    1 | passed:     1 | failed: 0
======================================================

但是我没有看到id元素被打印出来

  • 这是预期的行为吗?
  • 如何实现-只有通过使用* def temp = cat.kittens[*].id将其设置为temp变量才能做到这一点

1 个答案:

答案 0 :(得分:0)

print语句仅支持 JavaScript,不支持JsonPath:

* def ids = $cat.kittens[*].id
* print ids