簡體   English   中英

在SoapUI groovy腳本中獲取類型為Rest Request的先前測試步驟的名稱

[英]Getting name of previous test step of type Rest Request in SoapUI groovy script

我正在使用groovy腳本從REST請求的響應中轉移某些屬性,如下所示:

def setCookie = testRunner.testCase.testSteps["SubmitCompleteDeviceRegistration"].testRequest.response.responseHeaders["Set-Cookie"]
def global = com.eviware.soapui.SoapUI.globalProperties

re = /(SESSION_AUTHENTICATION_TOKEN=[A-Za-z0-9_-]+;)/
matcher = ( setCookie =~ re )
def cookie = matcher[0][0]

global.setPropertyValue("SESSION_AUTHENTICATION_TOKEN","$cookie")

return cookie

現在,我要做的是將上述測試步驟的名稱命名為“ SubmitCompleteDeviceRegistration”,變量,以便可以將傳輸用於各種REST請求。

該變量TestStep的名稱應等於RestRequest類型的先前TestStep的名稱。

如何定義等於此條件的TestStep?

我正在嘗試使用類似

def prevGroovyTestStep =       
testRunner.testCase.findPreviousStepOfType(testRunner.testCase.getTestStepByName
("SubmitCompleteDeviceRegistration"),RestRequest)

log.info(prevGroovyTestStep.getName())

但是我不確定如何實現這一點。

任何幫助將非常感激!

獲取上一步名稱

def previousStepName = context.testCase.testStepList[context.currentStepIndex - 1].name
log.info "Previous step name is : ${previousStepName}"

如果類型為Rest Request,則獲取上一個步驟名稱

def testStep = context.testCase.testStepList[context.currentStepIndex - 1]
def previousStepName
if (testStep instanceof com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep) {
    previousStepName = testStep.name
} else {
    log.error "Previous step is not of Rest Request Type"
}
if (previousStepName) {
    log.info "Previous step name is : ${previousStepName}"
}

如果在上述情況下類型不匹配,它將記錄錯誤消息。


更新 -根據該問題作者的最新評論進行更新。 下面的內容可以滿足您的所有需求,而上面的內容可能不再需要。

  1. 為測試用例添加一個自定義屬性,其名稱為STEP_NAME ,其值是需要添加http標頭測試步驟名稱 正如您所評論的,在這種情況下,最后一個測試步驟的名稱。
  2. 轉到請求測試步驟 ,在該步驟中將Cookie作為響應頭。
  3. 添加一個類型為Script Assertion的斷言,並具有以下代碼。 請注意,您需要修改要添加請求標頭 Cookie測試步驟名稱。 現在使用占位符
/**Below script should be used as script assertion for first test request step
* Assumes below
* a. test response contains http header called 'Set-Cookie'
* b. other request needs to send http header called 'Cookie'
* In case if there is any change in the two header names you may need to 
* change its references below
**/
def responseCookieKey = 'Set-Cookie'
def requestCookieKey = 'Cookie'


def setHttpHeaders(String nextStepName, def headers) {
    def nextRequest = context.testCase.testSteps[nextStepName].httpRequest
    def existingHeaders = nextRequest.requestHeaders
    headers.each {
        existingHeaders[it.key] = it.value
    }
    nextRequest.requestHeaders = existingHeaders
}


if (messageExchange.responseHeaders.containsKey(responseCookieKey)) {
  log.info "Found Cookie in the response headers"
  def cookiez = messageExchange.responseHeaders[responseCookieKey]
  assert null != cookiez, "Response does not contain Cookie"  
  def headers = [(requestCookieKey) : (cookiez)]
  setHttpHeaders(context.testCase.getProvertyValue('STEP_NAME'), headers)
} else {
  log.error "Not Found Cookie in the response headers"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM