JMeter:使用 Beanshell 断言计算从 Beanshell 后处理器获取的值的比较

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

我有一个 JMeter 测试,它发送一个获取特定文件名的 GET 请求。 我试图获取文件数量并将其与整数值进行比较。
如果计数大于整数,则测试应该通过。

为此,我使用了 Beanshell 后处理器来获取响应中获得的字符串的计数。

import org.apache.commons.lang.StringUtils;

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

但是,希望在Beanshell断言中使用这个计数值来用一个数字来验证它。这样做的正确方法是什么? 我是 JMeter 的新手。请帮忙。谢谢

您可以在一个断言中完成所有事情。请注意 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 而不是。

示例代码

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