JMeter BeanShell 断言在“”之后遇到“\\”

JMeter BeanShell Assertion encounted "\\" after ""

我的 BeanShell 断言 returns 以下结果为错误:

Assertion error: true
Assertion failure: false
Assertion failure message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval
Sourced file: inline evaluation of: `` String sentText = \"Changed the TEXT\"; String receivedText = \"Changed the TEXT\"; . . . '' Token Parsing Error: Lexical error at line 2, column 18. Encountered: "\" (92), after : ""

我使用 BeanShell 预处理器设置 属性 如下,我在编辑中使用它,效果很好。

${__setProperty(textEdit,\"Changed the TEXT\")}

然后我使用 GET 调用获取信息,并使用以下正则表达式取回该特定信息。

\"edittedText\":(\".*?\")}

然后我使用 BeanShell 断言将该正则表达式的结果放在 属性 textEditPost 中,就像这样。在那个 BeanShell 断言中,我还检查更改后的值是否是新值。

${__setProperty(textEditPost,${textEditPost})}
String sentText = ${__property(textEdit)};
String receivedText = ${__property(textEditPost)};

if (sentText.equals(receivedText))
{
    Failure = false;
}
else
{
    Failure = true;
    FailureMessage = "The Text does not match, expected: " + sentText + " but found: " + receivedText;
}

我完全不知道遇到两个反斜杠的错误是从哪里来的,因为两个字符串包含相同的数据。 有谁知道为什么会发生这种情况以及可能的解决方案吗?

我在为其他东西做了一些 BeanShell 断言后发现了问题。而且我现在也觉得很愚蠢,因为没有早点意识到这一点...

问题是 属性 textEdit 中的值是 \"Changed the TEXT\",因此以反斜杠开头。由于这个反斜杠,当试图将它分配给字符串变量 sentText 或直接在 if 语句中使用 属性 时,程序不知道如何处理它。

通过将 属性 调用放在引号之间,程序可以将其正确保存在 String 变量中。像这样:

${__setProperty(textEditPost,${textEditPost})}
String sentText = "${__property(textEdit)}";
String receivedText = "${__property(textEditPost)}";

if (sentText.equals(receivedText))
{
    Failure = false;
}
else
{
    Failure = true;
    FailureMessage = "The Text does not match, expected: " + sentText + " but found: " + receivedText;
}

我希望这也能帮助其他有类似问题的人。