Regular Expression Extractor in JMeter

JMeter, the most popular open-source performance testing tool, can work with regular expressions, with the Regular Expression Extractor.

Regular expressions are used to Match/extract a required part of the text

  • Regular expressions are popular when testing web applications because they can be used to validate and to perform operations on a response from a web application.
  • In JMeter, the Regular Expression Extractor is useful for extracting information from the Response.
  • For example, when you request a page and then need to get a link from the page that was downloaded.
  • Another use case is saving the extracted information to a variable, so it can be used later on in the performance test, for example, when testing an application that uses token authentication, like CSRF/XSRF.

Extracting Using Regular Expression

The Following Example Demonstrates the extraction of Username and Password from the web Application :

  • Open the JMeter in your system and then add Thread Group to the Test Plan.
  • After that, add HTTP request sampler to the Thread Group and then save the Test Plan.
  • Now, enter the website address inside the HTTP Request and the Login path as below.
  • Add View Results Tree Listener to the Thread Group.
  • Now save and run the Test Plan; you will see that the test plan has been executed.
  • Click on the HTTP Request and then click on the Response Body and Response Data.
  • So, what we need to do is, in the response data, we need to find whether the register link is present or not.
  • Next, we are going to use the Register Link.
  • That means we are going to capture the Register link URL from the Response data and going to pass this URL in my next request.
  • We capture the Register value by using the Post Processor request.
  • Add Post Processor Request(Regular Expression Extractor) to the Thread Group, as shown below.
  • The Post Processor Regular Expression page will look like as below.
  • In the Regular Expression page, we have to give Variable Name, Regular expression, Templates, and Match No.
  • Let us give the Variable Name as RegisterLink, as shown below.
  • Now we need to capture the Register link from the Response data; for that, we have a website called. regexr.
  • The page will look like as below, in that you can paste the Response data to check whether it is in the correct pattern or not.
  • I am going to paste the copied code below, in the Text tab as shown below.
  • We are actually looking at the below link, and we need to write the regular expression which exactly matches the link.
    <a href="">Register</a>‚Äč

  • I am writing the Regular expression, which matches the register link below. We got two links that match the regular expression.
  • Enter this regular expression inside the JMeter, as shown below.
  • But here we have two links that match with regular expression, so we need to mention the Template to capture the required link.
  • We can refer to each group with a number. So I am entering the Template as $1$ because we are referring to the second link with the first group.
  • If you click on the List in the RegExr website, you will find the two Links, and you can understand if you see that below image.
  • And the Match number is 2 as entered inside the JMeter as below.
  • The Regular Expression will execute after the First Request because this is a post-processor request.
  • So the regular expression will execute after the first request, On this particular Response using the regular expression, it will capture the registration value.
  • Refer to the Created variable in the next request.
  • Create a new HTTP request and then add variable inside the path with Curly braces, as shown below.
  • Now save and run the Test Plan.
  • The HTTP Request1 will send the request to the server, will get the Response, from that Response, we will extract the value that is the registration link.
  • We are using the same variable as a parameter to the HTTP request2.
  • After the execution, you will see that the HTTP Request1 is using the Login link.
  • And the HTTP request2 is using the RegisterLink as below.
  • Even we can use the View Result Tree to test whether the Regular expression is right or wrong.
  • Copy the Regular Expression from the Regular Expression Extractor and go to the View Result Tree.
  • And then, select request1 and click on the Triangle symbol, as shown below.
  • After selecting the RegExp Tester, paste the copied expression and click on the Test button. After Testing the Expression, you can see the result below as it shows that there two links match the given phrase.
  • Where the RegisterLink number will be 2, as shown below, that is the reason we gave Match No as 2.
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions