如何在SoapUI context.expand表达式中使用嵌套参数?

时间:2016-09-28 01:56:30

标签: groovy soapui

我的用例是我想在多个SoapUI项目中批量更新请求主体。

请求正文的示例。

{
 "name": "${#TestSuite#NameProperty}" 
 "id": "${#TestSuite#IdProperty}"
}

我想通过Groovy扩展属性$ {#TestSuite#NameProperty},并获取存储在TestSuite级别的值,然后根据需要进行修改。

假设我的测试用例中有50个测试步骤,我想从Groovy脚本扩展每个测试步骤的请求。要扩展特定的测试步骤,我会传递测试步骤的名称。例如:

expandedProperty = context.expand('${testStep1#Request}')

但是,如果我想迭代所有50个测试步骤,我怎样才能实现同样的目标?我试图在context.expand表达式中使用嵌套参数,但它不起作用。例如:

currentTestStepName = "TestStep1"
expandedProperty = context.expand('${${currentTestStepName}#Request}')

这只返回了我上面的测试步骤(我正在运行groovy脚本)而不是“TestStep1”步骤的扩展请求。 (这很疯狂!)

此外,context.expand似乎只能在从SoapUI工作区项目通过Groovy脚本执行时工作。是否有其他方法,或类似于context.expand的方法,可以在无头执行期间扩展像“$ {#TestSuite#NameProperty}”这样的属性?例如:在SoapUI中导入的groovy编译的jar文件。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您可以使用context.expand('${${currentTestStepName}#Request}')方式获取它。

还有其他方法,它们不使用context.expand。

为了获得任何给定测试步骤的单个测试步骤请求:

此处用户将步骤名称传递给变量stepName

log.info context.testCase.testSteps[stepName].getPropertyValue('Request')

如果您想获得测试用例的所有请求,可以使用以下脚本的简单方法。

/**
 * This script loops thru the tests steps of SOAP Request steps,
 * Adds the step name, and request to a map.
 * So, that one can query the map to get the request using step name any time later.
 */
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
def requestsMap = [:]
context.testCase.testStepList.each { step ->
    log.info "Looking into soap request step: ${step.name}"
    if (step instanceof WsdlTestRequestStep) {
        log.info "Found a request step of required type "
        requestsMap[step.name] = context.expand(step.getPropertyValue('Request'))
    }
}
log.info requestsMap['TestStep1']

更新 如果您感兴趣的步骤是REST步骤,请在上面使用以下条件而不是WsdlTestRequestStep

if (step instanceof com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep) { //do the stuff }