Json Script断言

时间:2018-02-06 08:15:13

标签: json groovy soapui

请你看看我的问题,看看你是否可以帮助我? 这是Json响应,包括在任务上添加的延迟。我想在SoapUI中创建一个脚本断言来检查" taskid"在响应中是否在testcase属性中具有相等的TaskId值?

[{
   "delayid": 7,
   "delaytypeid": 1,
   "autogrowminutes": 0,
   "seconds": 1800,
   "versionautoid": 10001308,
   "deleted": false,
   "taskid": 1163,
   "isprestartdelay": false,
   "starttime": "2018-02-06 09:30:00"
}]  

我的脚本断言:

import groovy.json.JsonSlurper;
def slurper = new JsonSlurper();
def response = messageExchange.response.responseContent;
def parsedJsonResponse = slurper.parseText(response);
def tcTaskId =messageExchange.modelItem.testCase.getPropertyValue("taskId"); 
assert !(parsedJsonResponse.isEmpty())
assert parsedJsonResponse.taskid==tcTaskId

我收到了这个错误:

  

断言parsedJsonResponse.taskid == tcTaskId | | | | | [1163] | 1163 | false [[autogrowminutes:0,delayid:7,delaytypeid:1,deleted:false,isprestartdelay:false,seconds:1800,starttime:2018-02-06 09:30:00,taskid:1163,versionautoid:10001308]] < / p>

它将[1163]与1163进行比较,因此结果为false,如何将它们转换为相同类型?

谢谢

1 个答案:

答案 0 :(得分:2)

您获取数组[1163]而不是数字1163的原因是因为您正在解析JSON数组(请注意响应中的[...]),然后您将获取来自所有的JSON数组元素的字段taskid,如果您尝试使用以下输入:

[{
   "delayid": 7,
   "delaytypeid": 1,
   "autogrowminutes": 0,
   "seconds": 1800,
   "versionautoid": 10001308,
   "deleted": false,
   "taskid": 1163,
   "isprestartdelay": false,
   "starttime": "2018-02-06 09:30:00"
},
{
   "delayid": 7,
   "delaytypeid": 1,
   "autogrowminutes": 0,
   "seconds": 1800,
   "versionautoid": 10001308,
   "deleted": false,
   "taskid": 1164,
   "isprestartdelay": false,
   "starttime": "2018-02-06 09:30:00"
}]

您会看到parsedJsonResponse.taskid[1163, 1164]

由于您检查响应是否为空,您可以执行

parsedJsonResponse[0].taskid==1163

parsedJsonResponse.taskid[0]==1163

两者都有效,但我建议在响应中准备多个元素并执行:

parsedJsonResponse.taskid.contains(1163)