saab613
saab613

Reputation: 213

In JMeter, How do i loop until a result is found

I have a request in Jmeter that I need to loop until I find the result I am looking for. I have read a few bits about a while controller but found them unhelpful as they seem to glance over vital information or are using an older version of Jmeter

I'm currently using Jmeter 5.0, and I have attempted to implement a while controller but failed as I think I do not understand how to properly handle the response, or even grab it, and then use it as a comparison to assert if the item exists.

I get a response from the HTTP request call response that looks a little somthing like this:

{"data":{"getIDs":{"Page": 1,"PageSize": 25,"PageCount": 1,"isFirstPage": true,"batches":[{"id":"b601a257-13fe-4de3-b779-a5922e89765a"},{"id":"b601a257-13fe-4de3-b779-a5922e89765b"},{"id":"b601a257-13fe-4de3-b779-a5922e89765c"}]}}

I need to recall the endpoint until I have found the ID I am looking for or cancel after 10 attempts

Upvotes: 1

Views: 4291

Answers (1)

saab613
saab613

Reputation: 213

So after a bit of fumbling around I, I figured on my own answer that seems to work well. But I would suggest looking at other sources before taking mine as gospel.

The basic structure looks as follows:

Setup of loop

Within a thread I set the variables then create the while loop as the next step. Within the While loop I created a counter then added the request I wanted to loop after. To make the loop work for me, I have three items sat under the request.

  • A Response Assertion: this check for a 200 status as the call should never fail
  • A Constant Timer: There is a delay between polls of the end point
  • A JSR223 Assertion: groovy code used ensure the while loop logic is handled

User Defined Variables:

User Defined Variables

Here i have setup two variables:

  • DONE: Done is a string value I alter if my JSR223 Assertion finds the values I'm looking for in the HTTP request
  • MyValue (this is dynamically driven in my actual test, for demo purpose, I’m just declaring a value to look for i.e. 12345)

While Controller:

While Controller

I still feel i may not understand this correctly, however after some googling I came across the following code that works for me despite some errors in the JMeter console:

${__javaScript(("${DONE}" != "yep" && ${Counter} < 10),)}

This code is saying that the while loop will continue until either of these two conditions are met:

  • DONE, the variable created earlier, is equal to the value yep
  • Counter is less than 10 (Counter is declared beneath the while loop)

Counter:

Counter

this was a simple configuration step that just worked once I understood it needed to be within the while loop, i configured the following:

  • Starting value = 1
  • Increment = 1
  • Exported Variable Name = Counter
  • Ticked 'Track Counter independently for each user'
  • Ticked 'Reset counter on each thread group iteration'

(Exported Variable Name: you can call this whatever you want, I’ve named it counter for the purpose of this demo)

JSR223 Assertion:

JSR223 Assertion

This is a simple script assertion that just uses a Boolean and a couple of if statements to assert the state of the test.

import org.apache.commons.lang3.StringUtils;

def done = vars.get("DONE");
String response = prev.getResponseDataAsString(); //retrieves the http request response as text
String MyValue = vars.get("MyValue");
def counter = vars.get("Counter");


//Find Substring within response string and stor result as boolean
String container = response;
String content = MyValue;
boolean containerContainsContent = StringUtils.containsIgnoreCase(container, content);

//Check if the true condition is met and change the value so the loop exits
if (containerContainsContent == true){
    log.info("------------------------Got Here");
    vars.put("DONE", "yep");

    }

//Force test to fail after 10 loops 
if (Counter.toString() == "10"){
    assert 1 == 2
}

Upvotes: 3

Related Questions