How could I set value of json object as a Request in soapUI

10,083

Some details are missing in your question, but I suppose that you have two REST TestSteps, supposing that the first is called REST Test Request and the second REST Test Request2 you can set the response and get request values for your testSteps using the follow groovy script inside a groovy TestStep:

import groovy.json.JsonSlurper
import groovy.json.JsonOutput

// request
def request = '''{"scopeId":"",
    "scopeType":"",
    "userId":"",
    "fieldToBeEncryptedList":[{"srNo":"","fieldName":"","value":"","echoField":""}]
}'''
// parse the request
def jsonReq = new JsonSlurper().parseText(request);

// get the response where you've the values you want to get
// using the name of your test step
def response = context.expand('${REST Test Request#Response}')
// parse response
def jsonResp = new JsonSlurper().parseText(response)

// get the values from your first test response 
// and set it in the request of the second test
jsonReq.scopeId = jsonResp.someField
jsonReq.scopeType = jsonResp.someObject.someField
// ... 

// parse json to string in order to save it as a property
def jsonReqAsString = JsonOutput.toJson(jsonReq)
// save as request for the next testStep
def restRequest = testRunner.testCase.getTestStepByName('REST Test Request2');
restRequest.setPropertyValue('Request',jsonReqAsString);

This scripts gets your json and fill it with the values from the first testStep response, and then set this json as a request for the second testStep.

Hope this helps,

Share:
10,083
Bugasur
Author by

Bugasur

Updated on June 26, 2022

Comments

  • Bugasur
    Bugasur almost 2 years

    I have a json Request like this:

    {
        "scopeId":"",
        "scopeType":"",
        "userId":"",
        "fieldToBeEncryptedList":[
        {
        "srNo":"",
        "fieldName":"",
        "value":"",
        "echoField":""
        }
    ]
    }
    

    Now what I want is to set the value of each key dynamically which I got from response of other Test Step. How could I do this using groovy scripting.

    Thanks in Advance.

  • Bugasur
    Bugasur almost 9 years
    Garry, this doesn't work for me. your solution did not update the scopeId and all which are in another Test Step. that test Step still holds its previous value which is taking from property value defined on test case level.
  • Garry
    Garry almost 9 years
    Working perfect for me, added the output as well. Try it on groovyconsole.appspot.com
  • albciff
    albciff almost 9 years
    @Garry yes your code works perfect :), however the OP doesn't want only to change a json in groovy, it want also to get the response from a SOAPUI testStep to fill this json and then set the result as request in another testStep.
  • Garry
    Garry almost 9 years
    @albciff thanks, I just want to clear for OP's above comment where he said its not updating the scopeid. Thanks for vote.
  • kinath_ru
    kinath_ru about 7 years
    Thanks a lot for the complete answer. Helps a lot