Run massively scalable performance tests on web, mobile, and APIs

Request a Demo
Jul. 14th, 2013

Using Regular Expressions in JMeter

Regular expressions provide a simple method to get information from pages when it is impossible or very hard to predict an outcome.
 
A standard usage example of using expressions is to get a session ID from the server response. If the server returns a unique session key we can easily get it using expressions in our load script.

Using Regular Expressions (Regex) in Script Development

To use regular expressions in your test plan, you need to use the Regular Expression Extractor in JMeter.
 
If we need to simulate user activity on websites for example, then to do this, we need to get links which are specifically related to the site being tested. Simply exclude all links that lead to other resources. The easiest way is to use regular expressions to extract all links from the page.

Creating Load Test Examples

Let's start to create our sample test. Build the test plan as usual. For example, here we want to open all links from BlazeMeter.com's home page. 
 
Under thread group, add the HTTP request to get the main web page, and then call the context menu from this element to add Regular Expression Extractor as shown.
 
Add the HTTP request to the thread group
 
Now we can configure the Extractor and set the rule that would extract all necessary links.

Under parameters we need to specify where JMeter should search data, name of array with the found data, template to extract, number of matches and default value.
 
Default value of the regex extractor in JMeter
 
To extract all parameters, set parameter Match No. to -1. 
Our regex for extracting all links is:
 
RegEx for extracting all links on BlazeMeter
 
Now we have an array named URLs and can use the ForEach Controller to loop through them. Add it to the test plan ForEach Controller and set its settings as on the image below.

Under the controller setting, we need to set the input variable prefix and output variable name which will be available on each iteration. All found links will be named as urls_X, where X – index number of found by regexp piece of data. That’s why we may set an input prefix as the url parameter.
 
 ForEach Controller on JMeter
 
The variable ${url} will contain one of the found links on each iteration.
To open every page by found address, just add the HTTP Sampler inside the ForEach Controller. As path set /${url} - found relative link.
 
Add the HTTP Sampler inside the ForEach Controller on JMeter
 
As you can see, regular expressions may be used when we cannot anticipate all possible parameters while the test runs or collect them before the test execution (e.g., as a session ID).
 
It is also an easy way to get all necessary data from the server's responses. Sometimes we need to specify large arrays of data, (e.g., all links from an opened page), and getting them automatically is a better idea than writing them by hand (who has the time?).
 
In fact, instead of describing the set of all possible links, it is more than enough (and a lot more simple) to write a correct regex expression.

When You Can Develop without JMeter's Regular Expressions

Sometimes the direct receipt of information during the test run does not require the usage of regular expressions. Time that was spent  on their development and validation may be of long duration. Resources that were spent on development of such a script won't be beneficial.
 
In cases where we do not work with large data arrays, we can use other ways to specify operating parameters in the load script.
 
For example, we can specify CVS data to be set with all needed parameters, (using CVS Data Set Config), or set it directly in the script source code.
 
Also if parameters are predictable, (counter, material ID in its URL, etc.), you can use other configuration elements provided by JMeter instead of the Regular Expression Extractor like the loop controller, counter or other random variables. In the development process it is necessary to select the most suitable method for each step.
 
**Please also note - JMeter uses Jakarta ORO for regular expressions processing. You can quickly test your regular expressions using Jakarta ORO Demonstration Applet which is the fastest way of seeing result matches/groups/etc.
 
 
It's all about code clarity and simple debugging.

 

You might also find these useful:

Interested in writing for our Blog? Send us a pitch!