You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Regular Expression in Apache-JMeter


Introduction:

Apache JMeter is an open source Java based tool which is mainly used for performing Load testing/Performance testing. The tool supports testing both client-server and web applications that contain both static and dynamic resources even though this tool is considered as a standalone in nature. In this article, we are going to see about the use of Regular Extractor Post Processor in JMeter.

Regular Expression:

Regular Expression in general is a piece of sequence of symbols and characters which expresses a string or pattern to be searched for within a longer piece of text. This will be written in a series of characters that will denote a search pattern. So, the same pattern will be applied on strings to find and extract the match required. Regular expression will be called as Regex in short. The language of regex uses meta characters like. (matches any single character), [] (matches any one character), ^ (matches the start position), $ (matches the end position) and many more to devise a search pattern.

Regular Expression Extractor:

Regular Expression (regex) feature in JMeter is developed and provided by the Jakarta ORO framework. It is modelled on Perl5 regex engine. We can use regex to extract values from the response during test execution, store it in a variable (also called as reference name) and can use further in JMeter. Regular Expression Extractor is a post processor that can be used to apply regex on response data. The matched expression derived on applying the regex can then be used in a different sampler dynamically in the test plan execution.

The Regular expression extractor fields will allow us to configure the below fields.

  • Apply to
  • Field to Check
  • Reference Name
  • Regular Expression
  • Template
  • Match no
  • Default Value

Let us see them in detail each one of them. Below is the image of the Regular Expression   Extractor control panel.

Apply to: Regex extractor are applied to test results which is a response data from the server.  Main sample is the response from the primary request and sub sample will be the sub sample. A user has the option to apply regex to main sample or sub samples or both.

Field to Check: Regex will always be applied to the response data. So, we need to choose what kind of response should match. We have various response indicators or fields available to choose. We can always apply regex to plain response body or a document which will return as a response data. The main thing is we can also parse URL using regex or we can opt them to apply regex on response code.

Reference Name: This is the main field where we can have the name of the variable which can further be referenced in the test plan using ${}. Once we apply regex, the final extracted value will be stored in this variable. There is also a chance like JMeter will generate more than 1 variable depending on the match occurred. If we have defined groups in our regex by providing parentheses (), then that will generate as many variables as number of groups. These variable names will be suffixed with letters _g(n) where n denotes the group number. Variable values captured can be checked using Debug Sampler.

Regular Expression: This is the field where we will be pasting the regex on the response data. A regex may or may not have a group. A group is a subset of string that is extracted from match. For example, if the response data is ‘VolunteerWelcomePage_vf&sid=”abcdefgh”’ and my regex is VolunteerWelcomePage_vf&sid=(.+?)$, then it matches ‘VolunteerWelcomePage_vf&sid=”abcdefgh”’ but extracts the string ‘abcdefgh’. The parenthesis () applied is the group that is been extracted. We can have more than one group in our regex, so which one or how many can be extracted is configured using template.

Template: Templates in JMeter are references or pointers to the groups. As we have explained earlier,  a regex may have more than one group — which means it allows us to specify which group value to extract by specifying the group number as $1$ or $2$ or $1$$2$ –which means extract both groups). A regex without () is matched as $0$(default group). By using these templates specified, that group value can be stored in the variable which is stored as reference name.

Match no.: A regex applied to the response data may have more than one match. You can specify which match should be returned. For example, a value of 2 will indicate that it should return the second match. A value of 0 will indicate any random match to be returned. A negative value will return all the matches.

Default value: The regex match is set to a variable, but what happens when the regex does not match? In such a scenario, the variable is not created or generated. But if you specify a default value and if the regex does not match, then the variable is set to the specified default value. It is recommended to provide a default value; so that you know whether your regex worked or not. It is a useful feature for debugging your test.

Regular Expression Extractor Example:

We are going to see a demo to extracta Salesforce dynamic value to pass and submit a customized Visualforce Lead form. After extracting the dynamic value from the previous response, it will be set using a variable; then, we will be passing those variables to the respected page fields and hence the lead would be submitted successfully.

Configuring Regular Expression Extractor:

Before configuring regular expression extractor, let us create a thread group named Leadform in JMeter with two main samplers called 1. Launch URL, 2. Leadform_Submit.

In the Leadform_Submit sampler, we have three fields to which dynamic values should be passed in order to make the run successful. The three dynamic values are obtained from the previous response (i.e. from LaunchURL Sampler). So, we are going to apply the regex on the response body (Main sample). When the test is executed, it will automatically extract the dynamic value from the previous response.

The Reference name or variable name provided is ‘C_Viewstate’. The regex used is name=”com.salesforce.visualforce.ViewState” value=”(.+?)=” /><input type=”hidden. We will not go into the details of the regex as this is a different discussion thread altogether. In a nutshell, this regex will find the Viewstate value and extract the value from the launch URL sampler. The extractor itself is placed in a parenthesis (), forming our first group. The Template field is set with the value of $1$ that points to our first group and the Match No. field indicates the first match. The Default Value set is the ‘empty. So, if our regex fails to match, then the variable C_Viewstate will hold the value as ‘empty. If the regex makes a successful match, then the C_Viewstate value will be stored in the C_Viewstate variable.

So, we are going to use this Reference Name in another sampler to pass the value for creating the dynamic value automatically. We can see in the below screenshot where ${C_ViewState_1_g1} will be added to a field. So, this field will capture the exact value using regex extractor and populate it in the field.

View Test Results:

We need to configure the View Results Tree Listener to the view the test results. Before doing that, we should also need to add a Debug Sampler that will help us to check the variable and its value generated upon executing the test. This will help us understand whether our regex successfully matched with the expression created or failed to match. To add the Debug sampler, we should use these steps. Right click on Thread Group and Select Add à Sampler à Debug Sampler

Since we want to debug the generated variables, set the JMeter variables field to True. Then, we need to view and verify test results using View Results Tree Listener. So, we are adding a View results tree listener by right clicking on Thread group; Select Add à listener à View Result Tree.

When we look at the output of Debug Sampler response data, it shows our variable C_ViewState_1. The value is the token that we have extracted.

So, the View State value is passed to the corresponding field where dynamic value is generated. Now, using this regex expression extractor the value has been captured from previous sampler and used in the current sampler.

Conclusion:

The regular expression extractor in JMeter is one of the significant features that will help us parse various types of values with different types of response indicators. Using this, we can store a value in a variable that can be used as references in other threads of the test plan. JMeter regular expression extractor has even more powerful feature: the ability to create/use groups in the regex, capturing the portions of matches. This is mainly used when we need to parse the text and apply it dynamically to subsequent threads in our test plan.