How to assert if jmeter response data is [] - jmeter

On running jmeter script, I am getting Response code as []
How do I perform assert?
I tried Text response contains [] still jmeter could not pass and I got below message
Assertion failure message: Bad test configuration org.apache.oro.text.MalformedCachePatternException: Invalid expression: []
Unmatched [] in expression.

It looks like you're using Response Assertion in "Contains" or "Matches" mode.
According to How to Use JMeter Assertions in Three Easy Steps guide:
The Pattern can be either be:
a “string” for “Equals” or “Substring” clauses
a “Perl5-style” Regular Expression for “Contains” or “Matches” clauses
So you need to:
either switch to "Equals" or "Substring" Pattern matching rule and keep your pattern as []
or if you using "Contains" or "Matches" rule - you need to remember that square brackets are special meta characters which represent character classes so they need to be escaped by forward slash like \[\]

Related

Matching against the value of a Regex group containing an HTML entity

I have the following element on an HTML page:
<option value="52">Engine Specialist</option>
I have a regular expression extractor giving me a variable named SpecialistInfo:
<option value="(\d+)">(.+?)<\/option>
JMeter gives me a match and I can access the matched values using the "group notation" syntax: SpecialistInfo_g1 and SpecialistInfo_g2
In a subsequent test I have a response assertion with a pattern of ${SpecialistInfo_g2} and I'm tyring to match against the following: "This person is an Engine Specialist"
This understandably fails with an assertion error: Assertion failure message:Test failed: text expected to contain /Engine Specialist/
I have tried using a matching pattern of: ${__unescapeHtml(${SpecialistInfo_g2})} which seems to replace the entity with a space but the match still fails.
The assertion error JMeter gives me is Assertion failure message:Test failed: text expected to contain /Engine Specialist/
Does anyone have suggestions on what to try?
My expectation is that __unescapeHtml() function produces something weird and not the space and hence your assertion fails.
Try using the following __groovy() function instead:
${__groovy(vars.get('SpecialistInfo_g2').replace(' '\, '\u0020'),)}
This will guaranteed return "normal" space separator hence your assertion should start working normally

How to use response assertion if pagination is applied and you dont know amongst the 3 pages, where the value will appear?

Q1 Pagination is applied and I need to validate the response through text response , that value is present or not. SO there are 3 pages due to pagination, so value will appear in one of the three pages. So how and where should i add the response assertion?
Q2 Also if I select(pattern matching rules)as "matches" in response assertion, Then even same values appeared in response, but still assertion is getting failed.
For example: Under (pattern to test) I have added text to validate
"document":{"id":8},
Same value appeared in response after execution
"document":{"id":8},
But still assertion failed. why so?
Thanks in Advance
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
So given you chose "Matches" rule you need to use a PCRE, not a string as your input is considered as the Regular Expression
Just switch to "Substring" pattern matching rule and your assertion should start working as expected. If you want to proceed with the regular expression you need to amend your criteria correspondingly:
(?s)^.*"document":{"id":8},.*$

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

Assert given value:
"action":"GET /api/invoice_status","description":"[NO_AUTH] get
invoice statuses"
Assertion failure message:
Test failed: text expected to contain /"action":"GET
/api/invoice_status","description":"[NO_AUTH] get invoice statuses"/
It is adding / slashes just to represent properly but it's not an issue. Cross verify your response after running the script, there might be some difference in the response and the text which you are passing in assertion. Also, there is no need to pass complete response in assertion, just pass some important text like success, passed (if present in response) or any other text available in your response and click on contains radio button inside Response assertion
Got the solution by using the \ forward slash escape character.
As per 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
So if you choose "Contains" mode - JMeter treats pattern as Regular Expression. Either escape meta characters with a backslash - \ or consider switching the Response Assertion into "Substring" mode instear.

Jmeter Extracting response body

my web sampler request has a reponse message of
[Message 1]
{"event":"pusher:connection_established","data":"{\"socket_id\":\"177828.486549\",\"activity_timeout\":120}"}
How I can extract 177828.486549?
Many thanks.
Actually regular expression for this is very simple, you only need to remember that backslash - \ is a special "escape" character hence it needs to be escaped with another backslash
Add Regular Expression Extractor as a child of the element which returns above message
Configure it as follows:
Reference Name: anything making sense, i.e. socket_id
Regular Expression: "socket_id\\":\\"(.+?)\\"
Template: $1$
Refer extracted value as ${socket_id} where required
Demo (you can test your regular expressions right in View Results Tree listener:
References:
Regular Expressions
USING REGULAR EXPRESSION EXTRACTOR

How to extract jmeter response value

I did read few responses but my regular expression extractor is not working.
Mine is a simple case where this is my response
token.id=AQIC5wM2LY4Sfcz4cOT2RrremxWJmM3llZmPl6k0bP_r5D4.AAJTSQACMDUAAlNLABQtNDI1OTg4NzgxODg5MDM1ODU2NQACUzEAAjI3
I am trying to grab the value using this expression
token.id="(.*?)"
which is not saving the value into the variable i assigned. My next request when trying to use the value fails since its not grabbing it.
Can someone let me know what exactly is missing. thanks.
There are few problems with your regular expression:
You need to escape dot between "token" and "id" with backslash as it is a special character. See Literal Characters article for more information.
You don't need the quotations marks as your response doesn't contain them (does it?)
So your regular expression needs to be amended as token\.id=(.*) (however I would rather go for something like token\.id=(\w.+)
You can use View Results Tree listener in "RegExp Tester" mode to test your regular expressions directly against response without having to re-run the request.
See Regular Expressions JMeter documentation chapter and How to debug your Apache JMeter script guide for extended information on the above approaches.

Resources