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

Groovy 如何在SoapUI context.expand表达式中使用嵌套参数?,groovy,soapui,Groovy,Soapui,我的用例是希望在多个SoapUI项目中对请求主体进行批量更新 请求主体的示例 { "name": "${#TestSuite#NameProperty}" "id": "${#TestSuite#IdProperty}" } 我想通过Groovy扩展属性${{#TestSuite#NameProperty},并获取存储在TestSuite级别的值,然后根据需要对其进行修改 假设我的测试用例中有50个测试步骤,我想从Groovy脚本中扩展每个步骤的请求。要展开特定的测试步骤,我将传递测试步

我的用例是希望在多个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的其他方式或方法可以在headless执行期间扩展诸如“${TestSuite#NameProperty}”之类的属性?例如:在SoapUI中导入的groovy编译的jar文件


提前谢谢你的帮助

您可以使用
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 }

我想出的一种方法是构造一个字符串,将其汇总为实际的TestStep名称,它成功了。但是,我希望可能有一种更简单的方法。示例:string=“\$”+“{TestStep1#Request}”,然后展开字符串上下文。展开(string)您的用例是什么?只是想把测试的所有请求都写到某个地方?groovy脚本在同一个测试用例中吗?在原始问题中更新了我的用例。你能检查更新后的答案是否适合你的需要吗。我尝试使用getPropertyValue('Request'),但它返回实际的字符串,包括“${TestSuite#NameProperty}”。但是,我希望实际值在“${#TestSuite#NameProperty}”中,这就是我尝试使用context.expand的原因。为什么要替换值
${TestSuite#NameProperty}
,而不是更改属性值。我正在使用的批量更新实用程序,涉及将JSON请求转换为具有不同结构的新请求。此外,我无法更改属性,因为在更新属性之前,我需要检查一些实现条件。即使请求中存在
${{TestSuite}NameProperty}
,它也会在发送请求时发送实际值,对吗?@Dighate我不知道你想实现什么,但是使用@Rao方法向请求字符串添加
context.expand
可以做到:
requestsMap[step.name]=context.expand(step.getPropertyValue('request'))