问题
I am using SoapUI to test restful web services. I have a test step which returns a json response like..
{
"createdUser": "CRINFO",
"createdDate": 1254413167000,
"updatedUser": "62041",
"updatedDate": 1452105085000,
"sourceSystem": "CIS",
"versionNumber": 47,
"crimeNo": "000037P/09",
"furtherInvestigation": false
}
I wish to use this response in the body of the next test step request but I want to manipulate a few of the properties first. e.g. set the furtherInvestigation property to TRUE.
{
"createdUser": "CRINFO",
"createdDate": 1254413167000,
"updatedUser": "62041",
"updatedDate": 1452105085000,
"sourceSystem": "CIS",
"versionNumber": 47,
"crimeNo": "000037P/09",
"furtherInvestigation": true
}
I have created a property transfer step and have managed to get the entire response to transfer to the next test step in the request body but can't figure out how to change the property.
In the property transfer step I have the following selections..
Source:previousTestStep Property:Response Path Language:JsonPath
Target:NextTestStep Property:Request Path Language:JsonPath
Whats the best way to do this? I have researched this problem and discovered groovy scripts that could possibly do what I want but thought there must be an easier way. Many thanks in advance for any help..
回答1:
You can simply achieve this with following steps.
1) Add script assertion for the source test step
2) Add the below lines in script assertion
def response = messageExchange.responseContent
response = response.replace("'furtherInvestigation': false","'furtherInvestigation': true")
messageExchange.modelItem.testStep.testCase.setPropertyValue("jsonResponse",response)
3) For destination test step, keep ${#TestCase#jsonResponse} for request body instead of actual request body
回答2:
If you like to do in Groovy, you can use these steps
- Use
JsonSlurper
to parse Json - Manipulate Json
3.Build Json again
def builder = new JsonBuilder(your_changed_json)
def json = builder.toPrettyString()
json = groovy.json.StringEscapeUtils.unescapeJava(json)
set the above json to your request
step.testRequest.setRequestContent(json)
来源:https://stackoverflow.com/questions/34642429/soapui-manipulation-of-json-response