Based on patterns, Regular expressions are being used for searching and manipulating the texts.  Regular expressions help in saving time for the user while providing greater flexibility for creating and enhancing a test plan. JMeter uses Apache Jakarta ORO pattern matching software for interpreting forms of regular patterns or expressions used in a JMeter test plan. When it is difficult or almost impossible to predict an outcome, regular expressions makes it easier to get information from pages.

Regular Expression Extractor of JMeter can be used for using regular expressions in a test plan and these regular expressions can be placed in any component in a Test Plan.

While using the Response Assertion test element, there is a difference between matches and contains. Matches means that the regular expression matched the whole text/content while contains as it name specifies, atleast some part of the text/content is available in the regular expression. So ‘alphabet’ “contains” ‘ph.b.’ as the regular expression matches the substring ‘phabe’ and the ‘alphabet’ is “matched” by ‘al.*t’.

For example, in a webpage, if you want to match the following portion and extract readme.txt then

name=”testfile” value=”readmefile.txt”

Regular expression that is to be used is

name=”testfile” value=”(.+?)”>

Use of Regular Expression in a Test Plan

It is easier to understand the use of Regular expressions in the Post-Processor Element (Regular Expression Extractor) by creating a test plan. Regular Expression Extractor uses a regular expression to recognise and extract the pattern of the Text from the current page that conforms to the desired element.

Here is an example to understand better. Let us create an HTML file that the list of persons and their email ids. We will be deploying this to our Tomcat Server. The HTML(test.html) page contents are as follows:

<html>

<head>

</head>

<body>

<table style=”border: 1px solid #000000;”>

<th style=”border: 1px solid #000000;”>ID</th>

<th style=”border: 1px solid #000000;”>Person-name</th>

<th style=”border: 1px solid #000000;”>Person-Email</th>

<tr>

<td id=”ID” style=”border: 1px solid #000000;”>1</td>

<td id=”Name” style=”border: 1px solid #000000;”>Susan</td>

<td id=”Email” style=”border: 1px solid #000000;”>Susan@yahoo.com</td>

</tr>

<tr>

<td id=”ID” style=”border: 1px solid #000000;”>2</td>

<td id=”Name” style=”border: 1px solid #000000;”>Craig</td>

<td id=”Email” style=”border: 1px solid #000000;”>Craig@yahoo.com</td>

</tr>

</table>

</body>

</html>

 

 

Upon deploying this HTML page on the tomcat server, this page will look like as

In our test plan, let us select from the person table, the person in the first row seen in the person list page above. In order to capture this person’s ID, we have to determine the pattern by finding the person in the second row and as seen in the following screenshot, the ID of the second person is surrounded by <td id=”ID”> and </td >, which is the second row of data with this pattern.

Using this, we can match the exact pattern that we want information to be extracted from. Here aim is to extract the person ID and the person name from this page and the fields can be defined as

To begin with, start the JMeter, then add a Thread group by right clicking on Test Plan, then click on Add> Threads, then click on Thread Group. Add a sampler HTTP Request by selecting the test plan then right click on Add, then click on Sampler, then click on the HTTP Request and enter the following details

  • Name− Manage
  • Server Name or IP− localhost
  • Port Number− 8080
  • Protocol− Keep this filed blank to indicate we want HTTP as the protocol.
  • Path− jmeter/test.html

In the next step, add a Regular Expression Extractor by Right clicking on Manage, then click on Add, then click on Post Processor and then Regular Expression Extractor.