【问题标题】:Getting name of previous test step of type Rest Request in SoapUI groovy script在 SoapUI groovy 脚本中获取类型为 Rest Request 的先前测试步骤的名称
【发布时间】:2016-01-15 21:57:06
【问题描述】:

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

但我不确定如何实现。

任何帮助将不胜感激!

【问题讨论】:

  • @Jose Kivits,您只需要上一步吗?还是应该是上一步和匹配的休息步骤?如果上一步不是 Rest Step 类型,你想要什么?

标签: rest groovy properties soapui


【解决方案1】:

获取上一步名称

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

如果在上述情况下类型不匹配,则会记录错误消息。


UPDATE - 根据该问题作者的最新 cmets 进行更新。以下内容可满足您的所有需求,而上述内容可能不再需要。

  1. 为测试用例添加一个自定义属性,其名称为STEP_NAME,其值为http header需要添加的测试步骤名称。正如您所评论的那样,在这种情况下,最后一个测试步骤名称。
  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"
}

【讨论】:

  • 您好 Rao,感谢您的回复。对不起,我可能没有足够清楚地定义我需要什么。我需要的是休息请求类型的最后执行的测试步骤,即它可以从常规测试步骤中删除 3 个步骤,只要它是一个休息请求。我认为脚本应该从上一步开始,如果这一步不是休息请求,它会继续上一步,直到满足条件。如果条件不满足,它应该继续而不记录错误。您能否调整脚本以满足这些要求?
  • 您能解释一下您的用例是什么,或者您想要实现什么,可能存在其他或更好的解决方案吗?
  • 好的,看起来问题的摘要导致了混乱。如果您需要提取标头值(可以是会话 ID 或 cookie)并将其分配给某个全局属性。这就是你的问题吗?
  • 是的,我确实在提取标头值(cookie)并将其分配给属性。我想要做的是从 REST 测试步骤的响应中提取一个 cookie,以便我可以在以下 REST 请求中使用它。我正在使用 groovy 脚本来实现这一点,但是每次我将此测试步骤复制到一个新的测试用例时,我都必须根据需要从中提取 cookie 的测试步骤的名称来调整脚本。此测试步骤将始终是最后执行的 REST 测试步骤,这就是为什么我试图找到一种方法来轻松识别此测试步骤的名称并将其用作脚本中的变量。
  • 理想情况下,我什至不必将 groovy 测试步骤复制到每个测试用例,而只需将其添加为可以从每个测试用例中运行的独立测试用例(运行测试用例 > 指向 groovy 测试步骤),但其他方法也可能同样有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多