Karate array field become object

核能气质少年 提交于 2019-12-06 09:51:08

You can try to convert the return data in JavaScript function to JSON. Below is explain what happening in karate as my understand:

* def appendList = read('../utils/append-list.js')
* def arr = [{a: 'a'}, {b: 'b'}, {c: 'c'}]
* def arr1 = [{a: 'a'}, {b: 'b'}, {c: 'c'}]
* def arr2 = appendList([arr, arr1]) # return JS[] variable type instead of JSON object
* print 'arr2', arr2
* def xx = { aa: '#(arr2)' } 
* print 'xx', xx
* copy yy = xx
* print 'yy', yy
* match xx == yy

Below is how to fix it:

* def appendList = read('append-list.js')
* json arr = [{a: 'a'}, {b: 'b'}, {c: 'c'}]
* json arr1 = [{a: 'a'}, {b: 'b'}, {c: 'c'}]
* json arr2 = appendList([arr, arr1]) # convert to JSON
* print 'arr2', arr2
* json xx = { aa: '#(arr2)' }
* print 'xx', xx
* copy yy = xx
* print 'yy', yy
* match xx == yy

Log Info:

20:37:02.034 [main] WARN com.intuit.karate - skipping bootstrap configuration: could not find or read file: karate-config.js, prefix: CLASSPATH
20:37:02.139 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.142 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.149 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.149 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.168 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.169 [main] INFO com.intuit.karate - [print] arr2 [
  {
    "a": "a"
  },
  {
    "b": "b"
  },
  {
    "c": "c"
  },
  {
    "a": "a"
  },
  {
    "b": "b"
  },
  {
    "c": "c"
  }
]

20:37:02.173 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.177 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.178 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['aa']
20:37:02.179 [main] DEBUG com.jayway.jsonpath.internal.JsonContext - Set path $['aa'] new value [{a=a}, {b=b}, {c=c}, {a=a}, {b=b}, {c=c}]
20:37:02.182 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.183 [main] INFO com.intuit.karate - [print] xx {
  "aa": [
    {
      "a": "a"
    },
    {
      "b": "b"
    },
    {
      "c": "c"
    },
    {
      "a": "a"
    },
    {
      "b": "b"
    },
    {
      "c": "c"
    }
  ]
}

20:37:02.188 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.188 [main] INFO com.intuit.karate - [print] yy {
  "aa": [
    {
      "a": "a"
    },
    {
      "b": "b"
    },
    {
      "c": "c"
    },
    {
      "a": "a"
    },
    {
      "b": "b"
    },
    {
      "c": "c"
    }
  ]
}

20:37:02.189 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.189 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
1 Scenarios ([32m1 passed[0m)
10 Steps ([32m10 passed[0m)
0m0.680s
html report: (paste into browser to view)

Result

Yes, by default, JSON is internally converted to Java lists which can be confusing. Best practice is to use 'Java style' wherever possible. And when iterating, use for-loops with indexes. For example, try these:

* def first = [{a: 1}, {b: 2}]
* def second = [{c: 3}, {d: 4}]
* eval first.addAll(second)
* print first

This has the same effect as the above, but is more convoluted:

* def append = function(f, s){ for (var i = 0; i < s.length; i++) { f.add(s[i]) }; return f }
* def first = [{a: 1}, {b: 2}]
* def second = [{c: 3}, {d: 4}]
* def result = append(first, second)
* print result

Note that we use the Java List.add() and List.addAll() methods, not the JS push. You can of course write your custom function that does this behind the scenes.

BTW the next version of Karate will introduce karate.forEach and even map and filter operations to make iterating easier. Maybe we need a karate.append() and karate.merge() (for merging 2 JSON objects). Feel free to raise a feature request.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!