空手道:有没有办法从示例变量中获取JSON对象键长度? [英] Karate: Is there a way of getting JSON object keys length from an Examples variable?
本文介绍了空手道:有没有办法从示例变量中获取JSON对象键长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的空手道考试通过了:
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
然而,我想做的不是使用Examples:部分中的Count变量,而只是以这种方式从Result对象变量中的键数量的长度派生出计数:
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:'#(Object.keys(result).length)', 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]}
当我尝试此操作时,出现错误:
primefactors.feature:33 - javascript evaluation failed: Object.keys(result).length, TypeError: {8=[2,2,2], 9=[3,3], 10=[2,5]} is not an Object in <eval> at line number 1
并且测试失败。
假设Object.keys(result).length
是有效的JS(使用Chrome dev控制台):
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
我做错了什么?执行此操作的正确方法是什么?
更新(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]}
推荐答案
是的,空手道中的JS不是你在野外看到的JS,当我们搬到Graal
时,这一点可能会改变同时,请使用此技巧从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()
接口以简化此操作。
这篇关于空手道:有没有办法从示例变量中获取JSON对象键长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文