How to pass one variable into another in JMeter?

Problem:

This is a common scenario in JMeter to pass the value of one variable into another. The value of one variable is used to decide the value of another variable, especially in the regular expression. Let’s try to understand the situation with a real-time example:

Example:

An e-commerce website requires performance testing for the purchase functionality. As per flow, a user adds a product into the cart where a number of products are already available then he needs to purchase the last product in the cart.

Now, how will you select the last product through JMeter script because the number of product present in the cart can vary?

Solution:

Through regular expression extractor (Match No. = -1), you can easily capture the IDs of all the products available in the cart. 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, the below statement will capture and pass the last value available in the list.

${__V(cartProductID_${cartProductID_matchNr})}

The generic syntax is _V(FirstVariable${NestedVariable}).

In the above example, FirstVariable refers to cartProductID and NestedVariable refers to cartProductID_matchNr. Hence the function becomes _V(cartProductID _${cartProductID_matchNr}).

Conclusion:

The function __V is used to evaluate the nested variables and helps to get the desired output. So, keep this important nested variable reference function in mind and use it to make the JMeter scripting easy.


Leave a Reply

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