2
votes

I have a problem with using regexp in response assertions in jmeter as same used in regular expression extractor. Here is the one example I'm having trouble with

Regular expression extractor - extracts sessionId from JSON response "sessionId":"([^"]+)", that extracts the valud between the "" and that currently works.

the same regexp used in Response Assertion does not work.

Generally what I need it s regexp that extracts values between quotes to be used in Response assertions

EDIT

Seems I did not explain the issue clearly, my bad

so, here it is - can it be used regexp for Response assertion like "sessionId":"([^"]+)", meaning that I care about only "sessionId":"somevalue different than null" and if yes what should be the regexp look like

Thanks

3
Generally what I need it s regexp that extracts values between quotes that's it? you only one a regex for this purpose? - Shakiba Moshiri
Generally speaking yes, regexp that extracts the value between quotes from JSON response, but such that works for Response Assertion - Nikolay Marinov
I do not know about Jmeter, AND it is better to edit your tag and add JSON if it relates to JSON - Shakiba Moshiri

3 Answers

5
votes

Response Assertion allows you write perl style regular expressions for Contains, Matches options.

Contains - for partial match Matches - for complete match

Try the following regex:

"sessionid":".*?"

If special characters need an escape, then use \ to escape as follows.

\"sessionid\":\".*?\"

Look after spaces in the regex.

Reference:

  1. http://jmeter.apache.org/usermanual/component_reference.html#Response_Assertion
3
votes

I fail to see how you can use the regular expression in the response assertion as you are supposed to be having some form of "expected result".

Given you are able to extract the sessionId into a JMeter Variable, i.e. ${sessionId} you should be able to use Response Assertion to check whether it is null or not via the following simple configuration:

  • Set "Apply to" field of the Response Assertion to "JMeter Variable" and put the variable reference name there like sessionId

    JMeter Response Assertion Variable

If your Regular Expression won't match anything, the relevant sampler will fail with the Response was null message:

JMeter Variable Assertion Null

See How to Use JMeter Assertions in Three Easy Steps article for comprehensive information on using JMeter assertions to conditionally fail your request basing on various criteria

0
votes

In the response assertion just write ${referenceName}, where referenceName = variable name of the extracted value with the regular expression.