Soapui manipulation of json response

时光总嘲笑我的痴心妄想 提交于 2019-12-11 23:19:49

问题


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

  1. Use JsonSlurper to parse Json
  2. Manipulate Json

3.Build Json again

  def builder = new JsonBuilder(your_changed_json)
  def json = builder.toPrettyString()
  json = groovy.json.StringEscapeUtils.unescapeJava(json)
  1. set the above json to your request

    step.testRequest.setRequestContent(json)
    


来源:https://stackoverflow.com/questions/34642429/soapui-manipulation-of-json-response

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!