Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 731
Bugasur Avatar asked Jul 01 '15 12:07

Bugasur


People also ask

How do I make a request for SoapUI?

To do that, click Create SOAP Request in the operation editor. The Open Request dialog will appear. Use it to open one of the existing requests in the project – that is, select a request from the drop-down list, then click OK.

Can we use JSON in SoapUI?

SoapUI parses the REST messages for you, and makes it very easy to view and edit the request and response headers as well as the JSON and XML payloads.

How does SoapUI store request and response?

Saving Responses in SoapUI Select the request you want to store responses from. In the "Request Properties" panel window scroll down to the property "Dump File", and enter a path for "Dump File". In the name, you could leverage Property Expansion to make the name or path dynamic.


1 Answers

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,

like image 128
albciff Avatar answered Oct 11 '22 02:10

albciff