1
votes

I'm trying to set a variable in JMter with the value in a List that I have in JSR223 Processor (Groovy). For that, I'm using the method vars.putObject, but when I try to use this variable in a ForEach Controller the loop doesn't execute.

My PostProcessor has the following flow:

  • Get a list of strings that were generated by a Regular Expression Extractor
  • Create a List with the valid values for the test (filter some values)
  • Add the result in a JMter variable vars.putObject

import org.apache.jmeter.services.FileServer

int requestAssetsCount = vars.get("CatalogAssetIds_matchNr").toInteger()
int maxAssetsNumbers = vars.get("NumberAssets").toInteger()
List<String> validAssets = new ArrayList<String>()

def assetsBlackListCsv = FileServer.getFileServer().getBaseDir() + "\\\assets-blacklist.csv"
File assetsBlackListFile = new File(assetsBlackListCsv)
List<String> assetsBlackList = new ArrayList<String>()

log.info("Loading assets black list. File: ${assetsBlackListCsv}")
if (assetsBlackListFile.exists()) {
    assetsBlackListFile.eachLine { line ->
        assetsBlackList.add(line)
    }
}
else {
    log.info("Black list file doesn't exist. File: ${assetsBlackListCsv}")
}

log.info("Verifying valid assets")
for (def i = 1; i < requestAssetsCount; i++) {
    def assetId = vars.get("CatalogAssetIds_${i}_g1")
    if (!assetsBlackList.contains(assetId)) {
        validAssets.add(assetId)
    }
    else {  
        log.info("Found a blacklisted asset. Skipping it. Asset ID: ${assetId}")    
    }
    if (validAssets.size() >= maxAssetsNumbers) {
        break
    }
}

I've tried (like regular extractor):

log.info("Storing valid assets list")
vars.putObject("ValidCatalogAssetIds_matchNr",validAssets.size())
for(def i = 0; i < validAssets.size(); i++) {
    vars.putObject("ValidAssetIds_${i+1}_g",1)
    vars.putObject("ValidAssetIds_${i+1}_g0","\"id\":\"${validAssets[i]}\"")
    vars.putObject("ValidAssetIds_${i+1}_g1",validAssets[i])
}

I've tried (set list value):

log.info("Storing valid assets list")
vars.putObject("ValidAssetIds",validAssets)
1
could you show how you do this: when I try to use this variable in a Loop Controller the loop doesn't execute. - daggett
In for each controller (field Input variable prefix) I put ValidAssetIds. The name that I used in vars.putObject - Thiago de Castro

1 Answers

0
votes

Concat strings as "+ (i+1) + "

vars.putObject("ValidCatalogAssetIds_"+ (i+1) + "_g",1) 
vars.putObject("ValidAssetIds_"+ (i+1) + "_g0","\"id\":\"${validAssets[i]}\"")

vars.putObject("ValiAssetIds_"+ (i+1) + "_g1",validAssets[i])

Don't use ${} syntax in JSR223 scripts because it will initialize values before script executed and not as expected