为运行REST请求创建groovy脚本

时间:2014-06-29 16:14:25

标签: rest groovy soapui

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

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"如果我的请求很酷,但如何运行它? 请帮忙......

1 个答案:

答案 0 :(得分:5)

通常,如果你有一个testStep,你可以得到它,然后只需运行它,但是你是以另一种方式做到这一点,所以你可以使用com.eviware.soapui.impl.rest.RestRequest类的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")
// get the context
def context = testRunner.getRunContext()
// send the request synchronous
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")
// 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"

希望这有帮助,