使用Groovy脚本在SoapUI TestCase请求中设置JSON元素

时间:2015-11-17 03:02:43

标签: json groovy soapui

对于XML请求似乎有很多例子可以执行此操作,规范形式如下:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
holder = groovyUtils.getXmlHolder("MyTestStep#Request" );
holder.setNodeValue( "//foo", "bar");
holder.updateProperty();

如何为JSON请求执行相同的操作?似乎没有' getJsonHolder'。

1 个答案:

答案 0 :(得分:0)

我相信可以有多种方式。 假设有一个带有以下步骤的soapui测试用例

  • Groovy脚本测试步骤 - 将为下一个休息步骤设置json请求
  • 休息请求测试步骤

例如,如果您有完整的字符串格式的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)

现在,尝试运行groovy步骤并查看其余测试步骤的请求体,设置上述请求。

使用属性扩展:  您还可以在休息请求正文中使用Property扩展,如下所示。请注意,可能不需要groovy脚本,因为我们没有在此处设置值,而是定义名为NAME的自定义测试用例级别属性。

{
   "id": "sample id",
   "name": "${#TestCase#NAME}",
   "tags": ["sample tags"],
   "address":    {
      "street": "sample street",
      "zipcode": "sample zipcode",
      "city": "sample city"
   }
}

如果数据以对象形式提供,而不是第一个示例中显示的静态数据,您也可以从对象创建动态请求。