Random ruminations of a java developer

Saturday, May 05, 2007

Using JMeter regular expressions with regexFunction

This short tutorial will help you use JMeter regular expressions using regexFunction.

RegexFunction is a useful helper designed to aid in parsing values from the previous response using regular expressions. I've found it useful to use regexFunction to follow a particular link when the link href values are dynamic values, which is normal in web applications. The simplest usage is to create a http request sampler and add the regexFunction to the path field, this allows you to follow complex links from the tested web application.

The basic form when using the regex function is this:

${__regexFunction(the_regexp_goes_here,$1$,1,,,)}

This will insert the result of the regexp match as a variable to this position (for example the path textbox in http sampler). Note that this example does not use arguments 4,5 and 6, they are given no values with the ",,,".

Arguments to the regexFunction

Argument #1: the regular expression to be used
for example:

"(.*)">this is a link

Matches a link of this form <a href="someurl">this is a link</a> AND stores the url of the link as the result. This is useful when following links which have dynamic content such as session id's etc.

Argument #2: The template string which will be used to refer to the evaluation result, the syntax is $[text]$, for example $1$.

Argument #3: This is the match number which JMeter should be using. The value can be:

* Any integer, this is what I've used almost always.
* RAND which means "select one of the matches randomly".
* ALL which uses all of the matches and concatenates them together.
* float from range of 0-1 which instructs jmeter to select the match number using the following formula: number_of_matches * supplied_number


Argument #4: If ALL was used in argument #3 this tells the delimiter to use between matches. (I've never used this)

Argument #5: Default value to be returned if nothing matches.

Argument #6: The reference name for values parsed by this function. (I've never used this either)

Thats it, this is the basic usage of the regexFunction in JMeter.