我如何在soapUI中将json对象的值设置为Request [英] How could I set value of json object as a Request in soapUI

查看:326
本文介绍了我如何在soapUI中将json对象的值设置为Request的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的json请求:

I have a json Request like this:

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

现在我想要的是动态设置我从其他测试步骤的响应中获得的每个键的值.我该如何使用groovy脚本来做到这一点.

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.

预先感谢.

推荐答案

您的问题中缺少一些详细信息,但我想您有两个REST TestSteps,假设第一个称为REST Test Request,第二个称为您可以使用groovy TestStep:

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);

此脚本获取您的json并用第一个testStep响应中的值填充它,然后将此json设置为对第二个testStep的请求.

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.

希望这会有所帮助,

这篇关于我如何在soapUI中将json对象的值设置为Request的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆