Karate 空手道:有没有办法从示例变量中获取JSON对象键的长度?

Karate 空手道:有没有办法从示例变量中获取JSON对象键的长度?,karate,Karate,我通过了空手道考试: Scenario Outline: Find all the the prime factors in the range from <start> to <end> are <result> Given path '/primefactors' And param start = <start> And param end = <end> When method get

我通过了空手道考试:

  Scenario Outline: Find all the the prime factors in the range from <start> to <end> are <result>
    Given path '/primefactors'
    And param start = <start>
    And param end = <end>
    When method get
    Then status 200
    And match header content-type contains 'application/json'
    And match header content-type contains 'charset=utf-8'
    And match response == {numbers:<result>, start:<start>, end:<end>, count:<count>, type:PrimeFactors}
    Examples:
      | start | end | result                                       | count
      | 8     | 10  | {8: [2,2,2], 9:[3,3], 10:[2,5]}              | 3
      | 13    | 16  | {13: [13], 14:[2,7], 15:[3,5], 16:[2,2,2,2]} | 4
我做错了什么?正确的方法是什么

更新(2019年4月9日) 以下工作成功:

  Background:
    * url baseUrl
    * configure lowerCaseResponseHeaders = true
    * def keys = function(o){ return o.keySet() }
    * def values = function(o){ return o.values() }
    * def size = function(o){ return o.size() }

  Scenario Outline: Find all the the prime factors in the range from <start> to <end> are <result>
    Given path '/primefactors'
    And param start = <start>
    And param end = <end>
    When method get
    Then status 200
    And match header content-type contains 'application/json'
    And match header content-type contains 'charset=utf-8'
    And def result = <result>
    And match response == {numbers:<result>, start:<start>, end:<end>, count: '#(size(result))', type:PrimeFactors}
    Examples:
      | start | end | result
      | 8     | 10  | {8: [2,2,2], 9:[3,3], 10:[2,5]}
      | 13    | 16  | {13: [13], 14:[2,7], 15:[3,5], 16:[2,2,2,2]}
背景:
*基于url的url
*配置LowercasereResponseHeaders=true
*def keys=函数(o){return o.keySet()}
*def values=函数(o){返回o.values()}
*def size=function(o){return o.size()}
情景大纲:找出从到范围内的所有主要因素
给定路径“/primefactors”
和参数开始=
和参数end=
当方法得到
然后状态200
匹配头内容类型包含“application/json”
匹配头内容类型包含“charset=utf-8”
和def结果=
和match response=={numbers:,start:,end:,count:“#(size(result))”,type:PrimeFactors}
示例:
|开始|结束|结果
| 8     | 10  | {8: [2,2,2], 9:[3,3], 10:[2,5]}
| 13    | 16  | {13: [13], 14:[2,7], 15:[3,5], 16:[2,2,2,2]}

是的,空手道中的JS并不是你在野外看到的JS,当我们移动到

同时,请使用此技巧从JSON获取键、大小(和值):

Scenario: json behaves like a java map within functions
  * def payload = { a: 1, b: 2 }
  * def keys = function(o){ return o.keySet() }
  * def values = function(o){ return o.values() }
  * def size = function(o){ return o.size() }
  * json result = keys(payload)
  * match result == ['a', 'b']
  * json result = values(payload)
  * match result == [1, 2]
  * def length = size(payload)
  * match length == 2
您应该能够在嵌入式表达式中使用函数,例如:
“#(keys(foo))”


未来,我们计划添加一个
karate.keysOf()
karate.sizeOf()
API,以简化此过程。

()=>doWindmillKick();根据你的建议,我可以得到键和值,但我似乎无法得到键数组的长度,是的,这是有效的。多谢各位。解决方案被接受,我用更新的测试更新了原始帖子。
result = {8: [2,2,2], 9:[3,3], 10:[2,5]} 
{8: Array(3), 9: Array(2), 10: Array(2)}
Object.keys(result).length
3
  Background:
    * url baseUrl
    * configure lowerCaseResponseHeaders = true
    * def keys = function(o){ return o.keySet() }
    * def values = function(o){ return o.values() }
    * def size = function(o){ return o.size() }

  Scenario Outline: Find all the the prime factors in the range from <start> to <end> are <result>
    Given path '/primefactors'
    And param start = <start>
    And param end = <end>
    When method get
    Then status 200
    And match header content-type contains 'application/json'
    And match header content-type contains 'charset=utf-8'
    And def result = <result>
    And match response == {numbers:<result>, start:<start>, end:<end>, count: '#(size(result))', type:PrimeFactors}
    Examples:
      | start | end | result
      | 8     | 10  | {8: [2,2,2], 9:[3,3], 10:[2,5]}
      | 13    | 16  | {13: [13], 14:[2,7], 15:[3,5], 16:[2,2,2,2]}
Scenario: json behaves like a java map within functions
  * def payload = { a: 1, b: 2 }
  * def keys = function(o){ return o.keySet() }
  * def values = function(o){ return o.values() }
  * def size = function(o){ return o.size() }
  * json result = keys(payload)
  * match result == ['a', 'b']
  * json result = values(payload)
  * match result == [1, 2]
  * def length = size(payload)
  * match length == 2