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

theartsycoder

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.

Dmitri T

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);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Jmeter extract a value using BeanShell PostProcessor

Jmeter. BeanShell PostProcessor

JMeter Thread Group Not Getting to BeanShell PostProcessor

Using Beanshell Postprocessor to modify the data extracted from JSON extractor

Beanshell type of variable assertion on Jmeter

How to get value from property in BeanShell (jmeter)

JMeter beanshell string comparison issue

Jmeter : Error when I try to get data from Database using beanshell assertion

Jmeter BeanShell assertion parsing and comparing UTC datetime from json

Empty csv file while using BeanShell Postprocessor

Jmeter - Inline evaluation error in Beanshell Assertion

Store Jmeter sampler properties value using beanshell preprocessor

Unable to append to next line in a file through beanshell assertion sampler in Jmeter

JMeter BeanShell Assertion: Getting error when convert String to Long

Verify the file is exist on a location in jmeter using beanshell

BeanShell Post Processor parse Json using Jmeter

JMeter - Stop Thread from within a BeanShell PreProcessor

Files are overwriting instated of appending in BeanShell PostProcessor Jmeter while running 1000 threads

Jmeter and global Beanshell variables

Update Jmeter variables with beanshell

Jmeter beanshell Post Processor

JMeter simple BeanShell PreProcessor

JMeter BeanShell - integer to string

Jmeter beanshell Encounter ";"

JMeter Beanshell Integer Error

Calling class in Jmeter Beanshell

jmeter beanshell pre processor

JMeter - Exception when using Beanshell as JSR223 Sampler with file

How to get Machine IP Address using beanshell in JMeter?