根据匹配计数值,需要将以下值捕获到变量中

based on the match count value the below value needs to be captured into a variable

我有一个要求,比如基于匹配计数值,需要将以下值捕获到变量中并将其传递给下一个请求。

<v13:uniqueTaskIdentifier><v13:taskIdentifier>${Task}</v13:taskIdentifier>

假设我在“${Task_matchNr}”中得到了 3 个值,我的输出应该如下所示:

<v13:uniqueTaskIdentifier><v13:taskIdentifier>**Task123**</v13:taskIdentifier><v13:uniqueTaskIdentifier><v13:taskIdentifier>**Task1234**</v13:taskIdentifier><v13:uniqueTaskIdentifier><v13:taskIdentifier>**Task1235**</v13:taskIdentifier>

我使用 JSR223 采样器:

def resultCount = ${Task_matchNr} as int
def ABC = "";
for(i=1;i<=resultCount;i++)
{
    def var_name="Task_"+i
    ABC = vars.get(var_name)
    def result = '<v13:uniqueTaskIdentifier><v13:taskIdentifier>(vars.get(ABC))</v13:taskIdentifier>'
    log.info(result);
}

但它正在捕获

<v13:uniqueTaskIdentifier><v13:taskIdentifier>(vars.get(ABC))</v13:taskIdentifier>

您需要连接值:

def result = '<v13:uniqueTaskIdentifier><v13:taskIdentifier>' + ABC  + '</v13:taskIdentifier>'
  1. Don't reference JMeter Functions or Variables in Groovy code as ${Task_matchNr} 因为该值将被缓存并用于后续迭代
  2. 您需要使用 string concatenation,因为您当前的方法将始终 return 硬编码值

您需要修改您的代码如下:

def resultCount = vars.get('Task_matchNr') as int
def ABC = "";
for (i = 1; i <= resultCount; i++) {
    def var_name = "Task_" + i
    ABC = vars.get(var_name)
    def result = '<v13:uniqueTaskIdentifier><v13:taskIdentifier>' + ABC + '< /v13:taskIdentifier>'
    log.info(result);
}

更多信息:Apache Groovy - Why and How You Should Use It