为运行REST请求创建groovy脚本 [英] Create groovy script for run REST request

查看:104
本文介绍了为运行REST请求创建groovy脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务来创建将运行REST请求和设置属性的groovy脚本。
我通过脚本设置属性:

  testRunner.testCase.setPropertyValue(ScriptProFrom,BIF)
testRunner.testCase.setPropertyValue(ScriptProTo,STD)

但是我不能找到如何运行REST请求。我尝试这样做:

  myInterface =(RestService)testRunner.testCase.testSuite.project.getInterfaceByName(http: //www.webservicex.net)
myOperation = myInterface.getOperationByName(ConversionRate)
myRequest = myOperation.getRequestByName(Request 1)

并获得Script-result:com.eviware.soapui.impl.RestRequest@6a80901和它很酷,如果我的要求,但如何运行它?
请帮助...

解决方案

通常,如果你有一个testStep,你可以得到它,然后简单地运行它,但是你以另一种方式来做,所以你可以使用 com.eviware.soapui.impl.rest.RestRequest 的方法.soapui.model.iface.SubmitContext,%20boolean%29rel =noreferrer> submit >类。这个方法有两个参数,上下文是 com.eviware.soapui.model.iface.SubmitContext 接口的一个实例,一个 boolean 它表示操作是否是异步的。在你的代码中,这可能是:

  myInterface = testRunner.testCase.testSuite.project.getInterfaceByName(http:// www。 webservicex.net)
myOperation = myInterface.getOperationByName(ConversionRate)
myRequest = myOperation.getRequestByName(Request 1)
//获取上下文
def context = testRunner.getRunContext()
//发送同步请求
myRequest.submit(context,false)

基于OP注释编辑:

submit方法返回一个对象,它是 com.eviware.soapui.impl.wsdl.WsdlSubmit< T> ,那么你可以在这个对象上调用 getResponse()获得另一个对象,它是 com.eviware.soapui.model.iface.Response getContentAsString()来检查响应内容或 getContentType()来检查内容类型等上。请注意,如果您以异步方式调用提交,您必须验证 getStatus()返回 com.eviware.soapui.model.iface.Submit.Status .FINISHED getResponse()之前。我给你举个例子:

  myInterface = testRunner.testCase.testSuite.project.getInterfaceByName(http://www.webservicex .net)
myOperation = myInterface.getOperationByName(ConversionRate)
myRequest = myOperation.getRequestByName(Request 1)
//获取上下文
def context = testRunner .getRunContext()
//发送请求同步
def submitted = myRequest.submit(context,false)
//获取响应
def response = submitted.getResponse()
//将响应内容作为字符串
def content = response.getContentAsString()
//即检查响应是否包含文字'OK'
assert content.contains(' OK'),Response不包含OK文字

希望这有帮助,


I have a task to create groovy script which will run REST request and setup property. I setup property by script:

testRunner.testCase.setPropertyValue( "ScriptProFrom", "BIF" )
testRunner.testCase.setPropertyValue( "ScriptProTo", "STD" )

But I can't find how to run REST request. I tried to do it like this:

myInterface = (RestService) testRunner.testCase.testSuite.project.getInterfaceByName("http://www.webservicex.net")
myOperation = myInterface.getOperationByName("ConversionRate")
myRequest = myOperation.getRequestByName("Request 1")

and get "Script-result: com.eviware.soapui.impl.RestRequest@6a80901" and it cool if it my request, but how to run it? Please, help...

解决方案

Normally if you have a testStep you can get it and then simply run it, however your are doing it in another way so you can use the submit method of com.eviware.soapui.impl.rest.RestRequest class. This method has two parameters, the context which is an instance of com.eviware.soapui.model.iface.SubmitContext interface and a boolean which indicates if the operation is asynchronous. In your code this could be:

myInterface = testRunner.testCase.testSuite.project.getInterfaceByName("http://www.webservicex.net")
myOperation = myInterface.getOperationByName("ConversionRate")
myRequest = myOperation.getRequestByName("Request 1")
// get the context
def context = testRunner.getRunContext()
// send the request synchronous
myRequest.submit(context,false)

EDIT BASED ON OP COMMENT:

The submit method returns an object which is instance of com.eviware.soapui.impl.wsdl.WsdlSubmit<T>, then you can invoke getResponse() on this object an get another object which is instance of com.eviware.soapui.model.iface.Response then from this you can use getContentAsString() to check the response content or getContentType() to check the content type and so on. Please note that if you invoke submit in asynchronous way you must validate that getStatus() returns com.eviware.soapui.model.iface.Submit.Status.FINISHED before getResponse(). I give you an example:

myInterface = testRunner.testCase.testSuite.project.getInterfaceByName("http://www.webservicex.net")
myOperation = myInterface.getOperationByName("ConversionRate")
myRequest = myOperation.getRequestByName("Request 1")
// get the context
def context = testRunner.getRunContext()
// send the request synchronous
def submitted = myRequest.submit(context,false)
// get the response
def response = submitted.getResponse()
// get the response content as string 
def content = response.getContentAsString()
// i.e check that the response contains literal 'OK'
assert content.contains('OK'),"Response not contains OK literal"

Hope this helps,

这篇关于为运行REST请求创建groovy脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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