theartsycoder
theartsycoder

Reputation: 13

JMeter: Count comparison of value fetched from Beanshell Postprocessor, using the Beanshell assertion

I have a JMeter Test which sends a GET request to fetch certain filenames. I was trying to get the count of the number of files and compare it with an integer value.
If the count is greater than the integer, the test should pass.

For this, I used a Beanshell PostProcessor to get the count of the string obtained in the response.

import org.apache.commons.lang.StringUtils;

String response = new String(data);
int count = StringUtils.countMatches(response, ".xml");

However, was hoping to use this count value in the Beanshell assertion to validate it with a number. What is the right way of doing this? I am newbie to JMeter. Please Help. Thanks.

Upvotes: 0

Views: 656

Answers (1)

Dmitri T
Dmitri T

Reputation: 168002

You can do everything in a single assertion. Be aware that starting from JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting so I would suggest going for JSR223 Assertion instead.

Example code

String response = prev.getResponseDataAsString();
int count = StringUtils.countMatches(response, ".xml");

int expectedCount = 10; //change it to your own expected value

if (count != expectedCount) {
    AssertionResult.setFailure(true);
    AssertionResult.setFailureMessage("Expected count to be " + expectedCount + ", actually got: " + count);
}

Upvotes: 1

Related Questions