How to add arguments to HTTP Sampler in dynamically in JMeter

Sometimes you need to send the arguments with HTTP Request that will be changing dynamically for each iteration/user (argument names and count).

Lets take an example, where user have to select all the "in stock" products and these items may change randomly for each user or iteration.

During recording,  we got 6 in stock items so JMeter captured 6 products ids as shown in the below image. if we try to replay the script, it may vary. In this case we have to dynamically construct/add the request parameters based on the "in stock" count. We can use JMeter scripting capabilities to achieve this (using any of the BeanShell Preprocessors).






Steps:
1. Capture all the product ids which has "in stock" status using correlation (in order to capture all the occurrences, we will use Match No as "-1")
2. Based on the product ids count we can construct request arguments using BeanShell Preprocessor or JSR223 Preprocessor
3. Add BeanShell Preprocessor to the request and delete the "Product_Id[]" arguments as shown in the below image

4. Now, using sampler.addArgument("Name","Value") function we can add parameters dynamically to HTTP Request

//Get Product Count
int i,j;
int count = Integer.parseInt(vars.get("c_ProductIds_matchNr"));

for(i=0;i<count;i++)

int j = i+1;
//Add each product
sampler.addArgument("Product_Id["+i+"]",vars.get("c_ProductIds_"+j));
}


Happy Learning !!!

Comments

Post a Comment

Popular posts from this blog

Read Data From A CSV File In Random Order - Apache JMeter

How To Simulate Different Network Speeds In JMeter