Jmeter - JMETER REGULAR EXPRESSIONSRegular expressions are used to search and manipulate text, based on patterns. JMeter interprets forms of regular expressions or patterns being used throughout a JMeter test plan, by including the pattern matching software Apache Jakarta ORO. With the use of regular expressions, we can certainly save a lot of time and achieve greater flexibility as we create or enhance a Test Plan. 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.
To use regular expressions in your test plan, you need to use the Regular Expression Extractor of JMeter. You can place regular expressions in any component in a Test Plan. It is worth stressing the difference between contains and matches, as used on the Response Assertion test element:
name="file" value="readme.txt"
And you want to extract readme.txt. A suitable regular expression would be:
name="file" value="(.+?)">
The special characters above are:
Let us understand the use of Regular expressions in the Regular Expression Extractor—a Post-Processor Element by writing a test plan. This element extracts text from the current page using a Regular Expression to identify the text pattern that a desired element conforms with. First we write an HTML page which a list of people and their email IDs. We deploy it to our tomcat server. The contents of html (index.html) 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;">name</th><th style="border: 1px solid #000000;">Email</th> <tr><td id="ID" style="border: 1px solid #000000;">3</td><td id="Name" style="border: 1px solid #000000;">Manisha</td><td id="Email" style="border: 1px solid #000000;">manisha@domain.com</td></tr> <tr><td id="ID" style="border: 1px solid #000000;">4</td><td id="Name" style="border: 1px solid #000000;">joe</td><td id="Email" style="border: 1px solid #000000;">joe@domain.com</td></tr> </table> </body> </html>
In our test plan, we will select the person in the first row of the person table seen in the person list page above. To capture the ID of this person, let us first determine the pattern where we will find the person in the second row. As can be seen in the following snapshot, the ID of the second person is surrounded by <td id="ID"> and </td >, and it is the second row of data having this pattern. We can use this to match the exact pattern that we want to extract information from. As we want to extract two pieces of information from this page, the person ID and the person name, the fields are defined as follows: Start JMeter, add a Thread group Test Plan > Add> Threads(Users)> Thread Group. Next add a sampler HTTP Request, select the test plan, right click Add > Sampler > HTTP Request and enter the details as shown below:
The following table provides a description of the fields used in the above screenshot:
Save the test plan as reg_express_test.jmx and run the test. The output would be a success as shown in the following screenshot: |