[英]Create groovy script for run REST request
我有一個任務來創建將運行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"
,如果我的請求很酷,但是如何運行呢? 請幫忙...
一般來說,如果你有一個一步步測試,你可以得到它,然后只需運行它,但你以另一種方式做,這樣你就可以使用提交的方法com.eviware.soapui.impl.rest.RestRequest
類。 此方法有兩個參數,上下文是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)
根據操作注釋進行編輯:
com.eviware.soapui.impl.wsdl.WsdlSubmit<T>
方法返回一個對象,該對象是com.eviware.soapui.impl.wsdl.WsdlSubmit<T>
實例,然后您可以對此對象調用getResponse()
來獲取另一個對象,它是com.eviware.soapui.model.iface.Response
實例com.eviware.soapui.model.iface.Response
然后您可以使用getContentAsString()
檢查響應內容,或使用getContentType()
檢查內容類型,依此類推。 請注意,如果以異步方式調用com.eviware.soapui.model.iface.Submit.Status.FINISHED
,則必須驗證getStatus()
在getResponse()
之前返回com.eviware.soapui.model.iface.Submit.Status.FINISHED
。 我舉一個例子:
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"
希望這可以幫助,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.