Jmeter If condition to check JSR223 variable value not working

31 views Asked by At

I am kind of stuck here, so I am new to Jmeter and trying to perform performance testing on bunch of APIs.

So below is the flow with relevant codes.

> HTTP Authorization Manager 
> HTTP Sampler (Base API) 
    > JSON Extractor
> ForEach Controller
    > JSR223 Postprocessor (change API Path with if condition)
    > If Controller (Check if value is not "true")
  > HTTP Sampler

Below is code for JSR223 (So I am checking if the variable from for loop is equal a regex then append additional parameters) and if the a variable path is null or empty then set a variable as "true"

def currentURL = vars.get("currentURL")
def modifiedURL = ""
    
log.info("currentURL: " + currentURL)
log.info("modifiedURL1: " + modifiedURL)

if (currentURL.equals("example")){
   modifiedURL = 'example?parameter1&parameter2'
   log.info("modifiedURL2: " + modifiedURL)
} else if (currentURL.equals("example2")){
   modifiedURL = 'example2?parameter3&parameter4'
   log.info("modifiedURL3: " + modifiedURL)
}

log.info("modifiedURL4: " + modifiedURL)

vars.put("modifiedURL", modifiedURL)

def ifvar = ${__groovy(vars.get("modifiedURL") != "",)}

def condition = ${__groovy(vars.get("modifiedURL") != null && !vars.get("modifiedURL").isEmpty(),)}

log.info("ifvar: " + ifvar)

log.info("condition: " + condition)

Now the if loop is as below

${__groovy(vars.get("modifiedURL") != "",)}

Now last HTTP Sampler is same as base with added paths as endpoint. e.g for 1st HTTP Sampler path is "https://API.com/path1/" and last one is "https://API.com/path1/${modifiedURL}"

But for some reason last one is running with same path as 1st HTTP sampler "https://API.com/path1/", ideally i would expect it to only run twice https://API.com/path1/example?paramete1&paramter2 and https://API.com/path1/example2?paramete3&paramter4

NOTE: I edited the JSR223 to debug further, for some reason in the logs ifvar and condition shows false, even though modifiedURL is not empty or null
1

There are 1 answers

1
Ivan G On

I fail to see where do write the value of modifiedURL into a JMeter Variable so my expectation is that the last line of your "code" should look like:

vars.put('modifiedURL', modifiedURL)

You might also want to check this modifiedURL variable value using Debug Sampler and View Results Tree listener combination, it might be the case your "condition" doesn't work as you expect