如何编写 Bean Shell 脚本来比较两个变量并打印输出。杰米特

How to write Bean Shell Script to compare two variables and print the output. JMETER

我有两个变量 1)Expected(取自 excel 文件)& 2)Actual(来自结果的正则表达式) 现在我尝试使用 bean Shell Post 处理器脚本来比较这两个变量。

if(("${Expected}").equals("${Actual}"))
{
IsSuccess = true;
}
else
{
IsSuccess = false;
}

实际上应该根据比较将样本标记为通过和失败。 但是当比较 returns false 时,我找不到标记为失败的采样器。 他们对我的情况有什么错误吗? 另外请帮助将输出打印到控制台 感谢帮助。 谢谢

  1. 不要将 JMeter Variables or Functions 内联到 Beanshell(或任何其他)脚本中,它们可能会解析为导致编译失败的内容
  2. 如果你想从 Post-Processor 中将采样器标记为通过或失败,你应该做一些不同的事情

示例建议代码:

if (vars.get("Expected").equals(vars.get("Actual"))) {
    prev.setSuccessful(true);
}
else {
    prev.setSuccessful(false);
}

其他推荐:

  1. 今后考虑切换到 JSR223 PostProcessor and Groovy language as Groovy performance is much higher 以实现任何脚本
  2. 您无需通过 "normal" 响应断言编写一行代码即可实现相同的效果,示例配置如下:

    请参阅 How to Use JMeter Assertions in Three Easy Steps 文章,了解有关有条件地 passing/failing 您的 JMeter 采样器使用断言的更多信息。