Getting name of previous test step of type Rest Re

2020-03-26 08:10发布

I'm using groovy script to transfer a certain property from the response of a REST request like this:

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

Now what I want to do is make the name of the above teststep, "SubmitCompleteDeviceRegistration", variable, so I can use the transfer for various REST-Requests.

The name of this variable TestStep should equal the name of the previous TestStep of the RestRequest type.

How can I go about defining the TestStep that equals this condition?

I'm trying to use something like

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

log.info(prevGroovyTestStep.getName())

But I'm not sure how to implement this.

Any help would be really appreciated!

1条回答
家丑人穷心不美
2楼-- · 2020-03-26 09:01

Getting the previous step name

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

Getting the previous step name if its type is 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}"
}

If type does not match in the above case, it will log the error message.


UPDATE - updating as per the latest comments from the author of this question. The below one helps all your need and the above may not needed any more.

  1. Add a custom property for the test case, whose name is STEP_NAME and its value is the test step name to which http header needs to be added. As you commented, the last test step name in this case.
  2. Go the request test step where you are getting the Cookie as response header.
  3. Add an assertion of type Script Assertion and have the below code. Note that, you need to modify the test step name to which you want to add the request header Cookie. Using the place holder for now.
/**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"
}
查看更多
登录 后发表回答