Search code examples
jmeterassertionsbeanshell

How to extract number of occurances of a reponse string in Jmeter and fail the test if the occurances does not match a number?


I am trying to count the number of occurrences of a response in Jmeter and if the count is not equal to 4, the test should fail.

How can i do that?

I used this in beanshell assertion:

Import org.apache.commons.lang3.StringUtils;

int matches = StringUtils.countMatches(new String(data), "itemname"); 
vars.put("ItemNameVar_matchNr", String.valueOf(matches));
assert matches == 5;

It. is not working. It shows error:

Typed variable declaration : Undefined argument: data


Solution

    1. You don't have data in the Beanshell Assertion, there is a pre-defined variable ResponseData which is byte array.
    2. You don't have assert in Beanshell, you have Failure pre-defined boolean which indicates whether sampler(s) in scope should be successful or not.

    3. Full code:

      import org.apache.commons.lang3.StringUtils;
      
      int matches = StringUtils.countMatches(new String(ResponseData), "itemname"); 
      vars.put("ItemNameVar_matchNr", String.valueOf(matches));
      Failure = matches != 5;
      
    4. References: