To extract the response of an HTTP Sampler into a variable for later use
To add regular expression extractor element to a test plan in JMeter:
1. Right click the sampler element (request to the server from which the value needs to be extracted)
2. Select Add option à Post Processors à Regular expression extractor
-----------------------------------------------------------------------------------------------
2. Select Add option à Post Processors à Regular expression extractor
-----------------------------------------------------------------------------------------------
Extracting Single string from the response
Consider an example, where a user Date displayed in a webpage Ex: Date:xxxxxxx
In order to extract the username, below R.E can be used:
Reference Name: Date
Regular Expression: Date\:(.+?)
Template: $1$
Match No. (0 for Random): 1
Default Value: match not found
Note:The special characters above mean:
( ) encloses the portion of the match string to be returned
. -> match for any character. + è one or more times.
? stop when first match is found
Without the ?, the .+ would continue until it finds the last possible match.
----------------------------------------------------------------------------------------------------
In order to extract the username, below R.E can be used:
Reference Name: Date
Regular Expression: Date\:(.+?)
Template: $1$
Match No. (0 for Random): 1
Default Value: match not found
Note:The special characters above mean:
( ) encloses the portion of the match string to be returned
. -> match for any character. + è one or more times.
? stop when first match is found
Without the ?, the .+ would continue until it finds the last possible match.
----------------------------------------------------------------------------------------------------
Extracting Multiple Strings from the response
Consider a scenario where the user wants to extract Date and Time.
(Ex:Date:xxxxxx Time:xxxxxxx) To extract both the ids, the below R.E can be used
Reference Name: Date_time
Regular Expression: Date\: (.+?) Time\:(.+?)
Template: $1$$2$
Match No. (0 for Random): 1
Default Value: match not found
Since we need to extract two values from the response, two groups are created. So the template has $1$$2$. The JMeter Regex Extractor saves the values of the groups in additional variables.
The following variables would be set as:
Date_time-> xxxxxzzzz
Date_time _g0 -> Date:xxxx time:zzzz
Date_time _g1 -> xxxx
Date_time _g2 -> zzzz
These variables can be later referred in the JMeter test plan, as ${Date_time _g1}, ${Date_time _g2}.
-------------------------------------------------------------------------------------------------------
(Ex:Date:xxxxxx Time:xxxxxxx) To extract both the ids, the below R.E can be used
Reference Name: Date_time
Regular Expression: Date\: (.+?) Time\:(.+?)
Template: $1$$2$
Match No. (0 for Random): 1
Default Value: match not found
Since we need to extract two values from the response, two groups are created. So the template has $1$$2$. The JMeter Regex Extractor saves the values of the groups in additional variables.
The following variables would be set as:
Date_time-> xxxxxzzzz
Date_time _g0 -> Date:xxxx time:zzzz
Date_time _g1 -> xxxx
Date_time _g2 -> zzzz
These variables can be later referred in the JMeter test plan, as ${Date_time _g1}, ${Date_time _g2}.
-------------------------------------------------------------------------------------------------------
Extracting only numbers from the String
Consider a case where we need to only extract the numbers, for example price=100$
To extract 100, the below R.E can be used
Reference Name: price
Regular Expression: price= “(.+?)$”
Template: $1$
Match No. (0 for Random): 1
Default Value: match not found
To extract 100, the below R.E can be used
Reference Name: price
Regular Expression: price= “(.+?)$”
Template: $1$
Match No. (0 for Random): 1
Default Value: match not found
No comments:
Post a Comment