在SoapUI groovy脚本中获取类型为Rest Request的前一个测试步骤的名称 [英] Getting name of previous test step of type Rest Request in SoapUI groovy script

查看:685
本文介绍了在SoapUI groovy脚本中获取类型为Rest Request的前一个测试步骤的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def setCookie 

我使用groovy脚本从REST请求的响应中转移某个属性。 = 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)

返回cookie

现在我想要做的就是创建上述teststep的名称,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上一步的名称是:$ {previousStepName}

如果其类型为休息请求,则获取上一个步骤名称

  def testStep = context.testCase.testStepList [context.currentStepIndex  -  1] 
def previousStepName
if(testStep instanceof com.eviware.soapui.impl.wsdl.teststeps。 (前一步骤名称){
previousStepName = testStep.name
} else {
log.error上一步不是Rest请求类型
}
if(previousStepName){
log.info上一步的名称是:$ {previousStepName}
}



<






UPDATE

如果type在上述情况下不匹配,它会记录错误消息。 / strong> - 根据此问题作者的最新评论进行更新。下面的代码可以帮助您满足您的所有需求,而上面的代码可能不再需要。


  1. 添加测试用例的自定义属性,名称为 STEP_NAME ,它的值是 http header 需要添加到的测试步骤名称。正如你所评论的,在这种情况下,最后一个测试步骤名称。

  2. 进入请求测试步骤,您将cookie作为响应标题。

  3. 添加脚本声明类型的断言,并具有以下代码。请注意,您需要修改您要添加请求标头的测试步骤名称 Cookie code>。现在使用 占位符
  4. -override> / **下面的脚本应该用作第一个测试请求步骤的脚本断言
    *假设在
    * a下面。测试响应包含名为'Set-Cookie'
    * b的http标头。其他请求需要发送名为'Cookie'的http标题
    *如果两个标题名称有任何变化,您可能需要
    *将其引用改为
    ** /
    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在响应标题中找到Cookie
    def cookiez = messageExchange.responseHeaders [responseCookieKey]
    assert null!= cookiez,Response不包含Cookie
    def headers = [(requestCookieKey):(cookiez)]
    setHttpHeaders (context.testCase.g
    } else {
    log.error未在响应头中找到Cookie
    }


    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!

    解决方案

    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"
    }
    

    这篇关于在SoapUI groovy脚本中获取类型为Rest Request的前一个测试步骤的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆