If you need to extract information from a text response, the easiest way is to use Regular Expressions. The matching pattern is very similar to the one used in Perl. Let’s assume we want to test a flight ticket purchase workflow. The first step is to submit the purchase operation. The next step is to ensure we are able to verify all the details by using the purchase ID, which should be returned for the first request. Let’s imagine the first request returns a html page with this type of ID that we need to extract:
<div class="container">
<div class="container hero-unit">
<h1>Thank you for you purchse today!</h1>
<table class="table">
<tr>
<td>Id</td>
<td>Your purchase id is 1484697832391</td>
</tr>
<tr>
<td>Status</td>
<td>Pending</td>
</tr>
<tr>
<td>Amount</td>
<td>120 USD</td>
</tr>
</table>
</div>
</div>
This kind of situation is the best candidate for using the JMeter Regular Expression extractor. Regular Expression is a special text string for describing a search pattern. There are lots of online resources that help writing and testing Regular Expressions. One of them is https://regex101.com/.
To use this component, open the JMeter menu and: Add -> Post Processors -> Regular Expression Extractor
The Regular Expression Extractor contains these fields:
The “Apply to” checkbox deals with samples that make requests for embedded resources. This parameter defines whether Regular Expression will be applied to the main sample results or to all requests, including embedded resources. There are several options for this param:
The “Field to check” checkbox enables choosing which field the Regular Expression should be applied to. Almost all parameters are self descriptive:
After the expression is extracted, it can be used in subsequent requests by using the ${purchaseId} variable.
This table contains all the contractions that are supported by JMeter Regular Expressions: