[英]Set JSON element in SoapUI TestCase request using Groovy script
There seems to be lots of examples to do this for an XML request, the canonical form being something like: 似乎有很多示例针对XML请求执行此操作,规范形式如下:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
holder = groovyUtils.getXmlHolder("MyTestStep#Request" );
holder.setNodeValue( "//foo", "bar");
holder.updateProperty();
How can I do that same for a JSON request? 如何对JSON请求执行相同操作? There does not seem to be a 'getJsonHolder'. 似乎没有“ getJsonHolder”。
There can be multiple ways, I belive. 我相信可以有多种方式。 Assuming there is a soapui test case with below steps 假设有一个包含以下步骤的soapui测试用例
For instance, if you have full json request in string format, here is the code snippet that goes into groovy script 例如,如果您具有字符串格式的完整json请求,那么以下是进入groovy脚本的代码段
def stepName='restStep'
def request = context.testCase.getTestStepByName(stepName).getTestRequest()
def jsonText = '''
{
"id" : "sample id",
"name" : "sample name",
"tags" : [ "sample tags" ],
"address" : {
"street" : "sample street",
"zipcode" : "sample zipcode",
"city" : "sample city"
}
}
'''
request.setRequestContent(jsonText)
Now, try running the groovy step and see the rest test step's request body, the above request is set. 现在,尝试运行groovy步骤并查看其余测试步骤的请求主体,上面的请求已设置。
Using Property Expansion: You may also use the Property expansion in the rest request body like shown below. 使用属性扩展:您也可以在其余请求正文中使用属性扩展,如下所示。 Note that groovy script may not be required as we are not setting value here, instead define a custom test case level property called NAME. 请注意,可能不需要使用Groovy脚本,因为我们不在此处设置值,而是定义一个名为NAME的自定义测试用例级别属性。
{
"id": "sample id",
"name": "${#TestCase#NAME}",
"tags": ["sample tags"],
"address": {
"street": "sample street",
"zipcode": "sample zipcode",
"city": "sample city"
}
}
You may also create the dynamic request from objects as well if the data is available in object form instead of static data shown in first example. 如果数据以对象形式(而不是第一个示例中显示的静态数据)可用,那么您也可以从对象创建动态请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.