且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为运行REST请求创建groovy脚本

更新时间:2023-12-05 15:05:34

通常,如果你有一个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,