2015-04-02 150 views
1

我的BeanShell断言返回以下结果为错误:的JMeter BeanShell的声明中遇到“\”后“”

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 Assertion将该正则表达式的结果放入属性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; 
} 

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

回答

0

我在为其他事情做出一些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; 
} 

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