SoapUI如何验证json字符串是一个数组,所有元素都是相同的值

SoapUI how to verify json string is an array with all element are the same value

我使用 SoapUi 5.3.0 测试 REST API。

我的要求return Json如下:

[
   {
      "name": "John",
      "online_status": "online"
   },
   {
      "name": "Marry",
      "online_status": "online"
   }
]

如何使用 Json路径表达式来验证所有 "online_status" 是否在线,而不是其他

您可以使用 Script Assertion 进行相同的 REST 请求测试步骤。

给你:

assert context.response, 'Response is null or empty'
def json = new groovy.json.JsonSlurper().parseText(context.response)
//Get the online_status as Set; so unique value are stored
def result = json.collect{it.'online_status'} as Set
//It should be only one as expected
assert 1 == result.size()
//It should only have online
assert result == ['online'] as Set

如果您想快速试用脚本,请检查 Demo