Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
After we have finished the deployment, the output would come out to be like this-
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.
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
Next, add a Regular Expression Extractor. Select the HTTP Request Sampler (CodingNinjas), right-click Add > Post Processor > Regular Expression Extractor.
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.
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-