1
votes

I have a Thread that does an http POST, each time giving back a response with a unique ID. I have a while controller setup with a counter, a json extractor everything seems great until my json extractor variable keeps getting overwritten by the previous POST.

i took the while controller setup code straight from the website below https://octoperf.com/blog/2017/11/30/how-to-use-jmeter-while-controller/

I have it set to run 5 times which it does, but my Unique ID gets overwritten

-Thread
    - JSR223 Name: initCounter
        vars.put("counter","1");
 - While Controller counter less than or equal to 5
    - HTTP Request
      - JSON Extractor
         names of create variables: Id
         JSON Path Expressions: Id
         Match No. (0 for Random): -1
    - JSR223 Name: incrementCounter
           int counter = Integer.parseInt(vars.get("counter")) +1;
           vars.put("counter",Integer.toString(counter));

Debug Sampler:

accountId_matchNr=1
counter=2```

```accountId_1=b39c34cd-aa44-452f-8e2c-d123123123
accountId_matchNr=1
counter=3```

```accountId_1=b39c34cd-aa44-452f-8e2c-dsfssdfdfs
accountId_matchNr=1
counter=4```


```accountId_1=b39c34cd-aa44-452f-8e2c-235534
accountId_matchNr=1
counter=5```

```accountId_1=bfc0a3c3-5eab-443c-bd44-dfgdgdfg
accountId_matchNr=1
counter=6```

How I can get variables for each and every accountId_1 (Eg: accountId_1 accountId_2 accountId_3 etc etc)

So each time it hits the POST i store the Id in var1 var2 var3 etc etc

images of my IDE setup:
  [1]: https://i.stack.imgur.com/XGUgV.png
  [2]: https://i.stack.imgur.com/RWv64.png
2

2 Answers

0
votes

Add current value to new variable with name "var" concatenated with counter value:

vars.put("var" +vars.get("counter"), vars.get("accountId_1"));
0
votes

I've done something similar to what I think you're asking. My need was for a place to store properties that test fragments would need to communicate between threads. I didn't know the name of the properties ahead of time, and didn't know how many I would need.

In a setup JSR223 early in the test suite, I put this:

props.put("runtimeProps", new ArrayList());

When I want to add a dynamic property to that array, I put this in a JSR223 sampler in the particular test fragment (in this case, I was storing a large JSON string which varied by product type, but only had a few variations, so I could parse it once, and the next time a thread needed it for that product type, it wouldn't need to parse it again):

propertyName = vars.get("TopSampleName") + "." + vars.get("Category") + "." + vars.get("Option");
id = props.get(propertyName);
if(id != null) { // we've already parsed it, no need to parse again.
    vars.put("ExID", id);
} else { // We haven't parsed this product type yet, so parse it, get the ID, and make it available to the thread (vars) and other threads (props).
    json = props.get(vars.get("TopSampleName") + ".AllJSON");
    id = com.jayway.jsonpath.JsonPath.read(json, '$..[?(@.text=="' + vars.get("Category") + '")].menu.items[?(@.text=="' + vars.get("Option") + '")].id').get(0).toString();

    vars.put("ExID", id);
    props.put(propertyName, id);
    propsList = props.get("runtimeProps");
    propsList.add(propertyName);
    props.put("runtimeProps", propsList);
}

This adds the property with the dynamically-defined name to the properties list, and adds the name of the property to the runtimeProps array.

Because I was using properties instead of variables, I need to clean them up at the end of the execution, so this went into a teardown JSR223 at the end of the suite:

propsList = props.get("runtimeProps");
log.info("propsList = " + propsList);
for (String property : propsList) {
    props.remove(property);
}
props.remove("runtimeProps");

Properties persist across executions, whereas variables do not. If you are using variables, you don't need the runtimeProps arraylist, or the clean-up step. You can just store your variable (vars.put, not props.put), but the consumer will need to know or regenerate the dynamic name, if you use a dynamic name.