Capture Last Dynamic Value in JMeter

Problem:

How to capture the last value using regular expression extractor when the dynamic value count is varied?

Explanation:

Let’s try to understand the scenario with an example. Let’s consider, you got an e-commerce website to test the performance of product purchase functionality. As per the requirement, you have to purchase the last product appeared in the cart. Through regular expression extractor, you can easily capture the IDs of all the products available in the cart and if the product count is constant then you can get the last product by providing the hardcoded value. But, the problem arises when the count of available product in the cart varies, so how will you pick the last product from the cart?

Solution:

To capture the last value from the list of dynamic value, you will need a nested variable function. In JMeter, “__V” is a nested variable function and helps to perform the operations on two variables. Let’s consider, the name of regular expression extractor variable is cartProductID which captures all the product IDs appear in a cart because the value of Match No. is -1. Refer to the screenshot:

Last Value using Regular Expression Extractor in JMeter
Figure 01: Regular Expression Extractor

Now, use the below statement where you want to pass the last value available in the list.

${__V(cartProductID_${cartProductID_matchNr})}

How does it work?

When you use -1 as an input value of Match No. then JMeter creates an additional variable <RegExVariable>_matchNr. You can check the value of this variable in the View Results Tree Listener by adding a Debug Sampler.

Let’s say, the value of cartProductID_matchNr = 10 in above example, so:

=> ${__V(cartProductID_${cartProductID_matchNr})} // Evaluate ${cartProductID_matchNr} and its value is 10
=> ${__V(cartProductID_10)} // Evaluate ${cartProductID_10} and its value is PerfMatrix
=> PerfMatrix

The function __V is used to evaluate the nested variables. Firstly it evaluates the nested variable ${cartProductID_matchNr} and then evaluates the nesting variable ${cartProductID_<ResultOfNestedVariable>} and returns the result.

To brief more, the function __V firstly evaluates the match number variable (value will be 10 i.e. the total cart available of the page) and this value will be used to capture the value of cartProductID variable available at the last position (cartProductID_10 i.e. the value present at 10th position). 

Additional Information:

In Regular Expression Extractor, if Match No. has

  • 0 as an input value then JMeter picks a random value from the identified dynamic values in the response
  • -1 as an input value then JMeter extract all the dynamic values available in the response

Leave a Reply

Your email address will not be published. Required fields are marked *