【问题标题】:JMeter BeanShell Assertion encounted "\\" after ""JMeter BeanShell 断言在“”之后遇到“\\”
【发布时间】:2015-04-02 13:25:00
【问题描述】:

我的 BeanShell 断言返回以下结果作为错误:

断言错误:true
断言失败:false
断言失败消息:org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval
源文件:内联评估:``String sentText = \"Changed the TEXT\"; String receivedText = \"更改了文本\"; . . . '' 令牌解析错误:第 2 行第 18 列的词法错误。遇到:“\\”(92),之后:“”

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

${__setProperty(textEdit,\"更改了文本\")}

然后我使用 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;
}

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

【问题讨论】:

    标签: jmeter beanshell


    【解决方案1】:

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

    问题在于属性 textEdit 中的值是\"Changed the TEXT\",因此以反斜杠开头。由于这个反斜杠,当尝试将其分配给 String 变量 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;
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      相关资源
      最近更新 更多