带有特殊字符的 Jmeter 响应消息断言

Jmeter response message assertion with special characters

我正在尝试对包含特殊字符的响应执行响应消息断言。

我看了这个问题:

但是当它包含特殊字符时,我仍然无法让它与断言相匹配。我试过 Text Response/Contains,Substring 和 Response Message/Contains,Substring,但是 none 它们匹配。

我要测试的模式是:

"v1.Accounts.Accounts.UpdateAccount failed due to validation errors:[MobileNumber] The field MobileNumber must be a string or array type with a maximum length of '20'."

根据 How to Use JMeter Assertions in Three Easy Steps 文章:

The Pattern can be either be:

  • a “string” for “Equals” or “Substring” clauses
  • a “Perl5-style” Regular Expression for “Contains” or “Matches” clauses

Substring 的情况下,它应该非常简单:只需将整个 "pattern" 按原样放入 "Patterns to test" 字段中,如果响应包含pattern - 断言将通过。

Contains 的情况下,JMeter 在后台使用 Perl5Matcher.contains() method,因此将 "Pattern" 视为正则表达式。因此,您需要使用反斜杠转义特殊字符,例如:

v1.Accounts.Accounts.UpdateAccount failed due to validation errors:\[MobileNumber\] The field MobileNumber must be a string or array type with a maximum length of '20'.

为了以防万一,当涉及到Perl-Compatible Regular Expressions时,您需要转义以下字符:

  • .^$*+?()[{\|字符外字符类
  • ^-]\ 字符内字符 类

还要记住,正则表达式非常脆弱,所以任何额外的 space、换行、制表,无论什么都会破坏它们,所以如果以上建议无济于事,请更新您的问题,至少部分 (更好的完整)响应并指定您需要检查的部分,可能情况下选择 XPath Assertion

会更好