Jmeter response message assertion with special characters

谁都会走 提交于 2020-01-17 06:55:03

问题


I'm trying to perform a response message assertion on a response which contains special characters.

I looked at this issue:

Adding / at the start and end of the asserted string in Response Assertion in JMeter

But I still can't get it to match the assertion when it contains special characters. I've tried Text Response/Contains,Substring and Response Message/Contains,Substring, but none of them match.

My pattern to test is:

"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'."


回答1:


As per How to Use JMeter Assertions in Three Easy Steps article:

The Pattern can be either be:

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

In case of Substring it should be pretty simple: just put the whole "pattern" into the "Patterns to test" field as it is and if response contains the pattern - the assertion will pass.

In case of Contains JMeter uses Perl5Matcher.contains() method under the hood so treats the "Pattern" as a regular expression. Therefore you will need to escape the special characters with the back slash like:

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'.

Just in case, when it comes to Perl-Compatible Regular Expressions you need to escape the following characters:

  • .^$*+?()[{\| characters outside character classes
  • ^-]\ characters inside character classes

Also keep in mind that Regular Expressions are very fragile so any extra space, new line, tabulation, whatever will break them so if above recommendations won't help please update your question with at least partial (better full) response and specify what part of it you need to check, it might be the case it is much better to go for XPath Assertion



来源:https://stackoverflow.com/questions/43849721/jmeter-response-message-assertion-with-special-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!