在SoapUI groovy脚本中获取Rest请求类型的先前测试步骤的名称

时间:2016-01-12 19:36:31

标签: rest groovy properties soapui

我正在使用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())

但我不确定如何实现这一点。

任何帮助都会非常感激!

1 个答案:

答案 0 :(得分:0)

获取上一步的名称

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

获取上一步名称,如果其类型为“休息请求”

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. 添加脚本断言类型的断言,并使用以下代码。请注意,您需要修改要添加请求标头的测试步骤名称 Cookie。暂时使用 占位符
  4. /**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"
    }