0

would you please have look at my issue and see if you can help me? this is the Json response which includes the delays added on a task. I want to create a script assertion in SoapUI to check whether "taskid" in response is equal TaskId value in the testcase property or not?

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

my script assertion:

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

I got this error :

assert 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]]

it compares the [1163] with 1163 so result is false, how can I convert them to the same type?

Thank you

1 Answer 1

2

The reason that you get an array [1163] instead of the number 1163 is because you are parsing JSON array (note the [...] in the response), and then you are fetching the field taskid from all of the elements of the JSON array, If you try with the following input:

[{
   "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"
}]

You will see that parsedJsonResponse.taskid is [1163, 1164]

Since you check if the response is not empty, you can either do

parsedJsonResponse[0].taskid==1163

or

parsedJsonResponse.taskid[0]==1163

Both will work, however I suggest preparing for multiple elements in the response and do:

parsedJsonResponse.taskid.contains(1163)

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @GergelyToth , thanks for your help : yes exactly, so what I did,I thought what if I add more than one delay on this task? so I wrote this script : ` for( i=0; i<parsedJsonResponse.size;i++)` ` { ` assert parsedJsonResponse[i].taskid == tcTaskId.toInteger(), "Delay not added!"; break; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.