Table of contents
1.
Introduction
2.
What are Regular Expressions?
3.
How to Create a JMeter Test Plan?
3.1.
Reference Name
3.2.
Regular Expression
3.3.
Template
3.4.
Match Number
3.5.
Default
4.
Frequently Asked Questions 
4.1.
Differentiate between matches and contain keywords.
4.2.
What do you mean by reference name?
4.3.
What does Regular Expression Extractor do?
4.4.
Why do we leave the protocol field blank?
4.5.
How to add a Regular Expression Extractor?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Introduction to JMeter ─ Regular Expressions

Author Geetika Dua
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Before jumping to Regular Expressions in JMeter, let’s understand what JMeter is.JMeter is an open-source Java application to analyze the performance of web applications. 

Intro image

Let us now deep dive into the Regular Expressions in JMeter. Follow this blog to get your concepts cleared!

What are Regular Expressions?

Depending on its patterns, regular expressions are mainly utilized to search and manipulate the text. JMeter understands and interprets the various forms of regular expressions or practices used during a JMeter test plan.

We have to use the Regular Expression Extractor of JMeter to use regular expressions in our test plan. 

Before we move on with this topic, let us first understand the difference between contains and matches used on the Response Assertion Test Element.

  • The word contains means that the regular expression matches some parts of the target.
  • The word matches mean that the regular expression completely matches the whole target. 

How to Create a JMeter Test Plan?

We will now understand the use of Regular Expressions in the Regular Expression Extractor. It extracts text using a regular expression to identify the pattern with the required element.

First, we will write an HTML page as follows. Then, we deploy it to the tomcat server.

<html>
   <head>
   </head>
   
   <body>
      <table style = "border: 1px solid #000000;">
       
         <th style = "border: 1px solid #000000;"> StudentID</th>
         <th style = "border: 1px solid #000000;"> Name</th>
         <th style = "border: 1px solid #000000;">Course</th>
           
         <tr>
            <td id = "StudentID" style = "border: 1px solid #000000;">100</td>
            <td id = "StudentName" style = "border: 1px solid #000000;">Aman</td>
            <td id = "Course" style = "border: 1px solid #000000;">DSA with Coding Ninjas</td>
         </tr>
           
         <tr>
            <td id = "StudentID" style = "border: 1px solid #000000;">101</td>
            <td id = "StudentName" style = "border: 1px solid #000000;">Rahul</td>
            <td id = "Course" style = "border: 1px solid #000000;">Web Development with Coding Ninjas</td>
         </tr>
           
      </table>
   </body>
</html>


After we have finished the deployment, the output would come out to be like this-

deployed html code

We can see the pattern in IDs if we inspect the page. We can use this information to match the exact pattern that we require. We want to extract two types of information from this page, the Student ID and the Student name; we shall define the fields as Student ID and Student Name.

inspect code

Follow the below steps to start JMeter-

  • Add a Thread group Test Plan 
  • Click on Add 
  • Navigate to Threads(Users) 
  • Name it Thread Group Coding Ninjas.

Next, the job is to add a sampler HTTP Request. To do so, select the test plan, right-click Add, select Sampler and navigate to HTTP Request. It will demand some details like those shown below −

  • Name − CodingNinjas
  • Server Name or IP − localhost
  • Port Number − 8080
  • Protocol − Keep this blank. It implies that we want HTTP as the protocol.
  • Path −myapp/cn.html
Details

Next, add a Regular Expression Extractor. Select the HTTP Request Sampler (CodingNinjas), right-click Add > Post Processor > Regular Expression Extractor.

expression extract

Reference Name

The reference name is the variable's name in which we will store the retrieved test(refname).

Regular Expression

By regular expression, we mean the pattern with which the extracted test is matched by us. The extracted text groups are enclosed by the characters '(' and ')'. To indicate a single instance of the text inside the table data tags (<td> and </td>), we will use '.+?'.

Template

Each group of retrieved text is placed as a member of the variable named Person. The order of each group of patterns is enclosed by '(' and ')'.

Match Number

This number indicates the sequence of matching. Imagine we only want the first occurrence; the value will be 1. For matching randomly, we will assign the value 0. Negative values are used with ForEach Controller.

Default

This is an optional field. We can also leave it untouched. This value is only used if the item is not found.

We have to include a listener to record the results obtained from the test plan. To do so, we will follow these steps-

Right-click the Thread Group and select Add, followed by Listener. Check the "View Results Tree" option to add the listener to the test.

Now, Save the test plan as codingninjas_test.jmx and run the test.

Run the test

Frequently Asked Questions 

Differentiate between matches and contain keywords.

The word contains means that the regular expression matches some parts of the target while the word matches mean that the regular expression completely matches the whole target. 

What do you mean by reference name?

The reference name is the variable's name in which we will store the retrieved test(refname).

What does Regular Expression Extractor do?

Regular Expression Extractor extracts text using a regular expression to identify the pattern with the required element.

Why do we leave the protocol field blank?

Keeping the protocol field blank implies that we want HTTP as the protocol.

How to add a Regular Expression Extractor?

We have to right-click on Add button, then navigate to  Post Processor under the HTTP Sampler Request to add Regular Expression Extractor.

Conclusion

This blog discussed the use of Regular Expressions in JMeter. We learned how to create a JMeter Test Plan using Regular Expressions. We also deployed a JMeter Test Plan on the tomcat server.

For more information on JMeter, Refer to our other articles-

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass