如何检查响应中是否存在特定字符串,并将用户定义的变量设置为 true 或 false

how do I check if a specific string exists in the response, and set a user defined variable to true or false

我想确定特定字符串是否存在于 HTTP 响应中。如果是,我想将用户定义的变量设置为 TRUE,如果不是,我想将其设置为 FALSE。我不想 pass/fail 基于此进行测试。我只想知道响应是否有字符串。然后,一旦我将该答案存储在我的用户定义变量中,我将在 jmeter IF 控制器中使用该变量来根据答案执行其他操作。

我已尝试将 beanshell 断言与以下代码结合使用,但我的预定义用户变量(称为 stringExists)未得到更新以正确反映响应是否包含字符串:

vars.get("stringExists");

if (new String(ResponseData).contains("this is the string I expect")) {
    vars.put("stringExists","TRUE");
}
else {
    vars.put("stringExists","FALSE");
}

我做错了什么?

您的代码没有任何问题,您可以使用 Debug Sampler 来“查看”生成的变量:

另请注意starting with JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting so you should consider switching to the JSR223 Assertion。由于没有 ResponseData shorthand 你需要稍微修改一下代码:

if (prev.getResponseDataAsString().contains("this is the string I expect")) {
    vars.put("stringExists","TRUE");
}
else {
    vars.put("stringExists","FALSE");
}