Regular Expression Extractor Example

Regular Expression Extractor Example
Today, we will use the Regular Expression Extractor component in JMeter to extract server responses to make our test script dynamic in nature.
The script recorded below is for a site that uses Cross-Site Request Forger (CSRF) to prevent against malicious attacks that prey on user vulnerability.
A token is attached to each user's session that is then sent along with every request from that user.
Each user gets their own unique token, and therefore, using the same token for two users flags an error on the server and the request is denied. 
We will use Regular Expression to extract the CSRF token and correctly sent the unique token for the rest of the requests for that user in the test script.
Steps:
  1. Launch JMeter.
  2. Create a Script as below:
    1. 2 HTTP Requests, 1 with GET and 1 with POST
    2. POST Request with Body data as {"value":"hello"}
    3. Debug Sampler
    4. View Results Tree
    5. HTTP Cookie Manager


  1. Run the script.
  2. When we run the script as above, and observer the View Results Tree the script will fail at Regular Expression_Post with below Response code
Body size in bytes: 1081
Sample Count: 1
Error Count: 1
Response code: 403
Response message: Forbidden
  1. If we observer the request data, it has the below details.
POST data:
{"value":"hello"}
Cookie Data:
XSRF-TOKEN=8qxW-YJ3OwA782MfIRAgs6RH;
Note: This token is changing for each user.
  1. We observer a Forbidden stack in the Response data
  2. Now Let us add “Regular Expression Extractor” as below:

8.    All the requests pass in the View Results Tree
9.    If we compare the Debug Sampler requests, we observe addition of token variables
JMeterVariables:
JMeterThread.last_sample_ok=true
JMeterThread.pack=org.apache.jmeter.threads.SamplePackage@16cc34c
START.HMS=155212
START.MS=1429611732154
START.YMD=20150421
TESTSTART.MS=1429696159316
token=tTgfZSJsFjxVjom57uXflYcL
token_g=1
token_g0=XSRF-TOKEN=tTgfZSJsFjxVjom57uXflYcL;
token_g1=tTgfZSJsFjxVjom57uXflYcL


Comments

Popular posts from this blog

Steps to Analyze AWR Report in Oracle

Vmstat Output explained

Verifications and Error Handling in LoadRunner *Web_reg_find and Web_reg_save_param*