Favourite Quote

Every artist was first an amateur. - Ralph Waldo Emerson The more difficulties one has to encounter, within and without, the more significant and the higher in inspiration his life will be. - Horace Bushnell

Wednesday, February 22, 2012

How to build a request dynamically in - JMeter

Most often we end up dealing with how to dynamically build our request.

Also how to fetch values received from HTTP request-1 & pass them as parameters to HTTTP request-2

We can achieve this by writing a simple script within JMeter.
for ex:
Let's take a scenario of amazon where we have to add items to the shopping basket

where items are onsale (These items onsale vary each day)

Therefore we don't want to be updating our data scripts each day

though we are reading from external files.

Let's get started to handle such situation.

1.Within a Testplan add a Thread group

2.Add a HTTP request sampler - This is our request-1

2.a We have to retrieve the item/product id's from this request so we need to add a xpath extractor as a post processor event.

Have a look at the screen shot.

Add a Debug sampler so that you can verify if the xpath extractor does retrieve the value.

To add a debug sampler right click on the Thread Group > Add > Sampler > Debug Sampler

To view contents of sampler you will have to add Listener.

Right Click on the Thread Group > Add > listener > View results tree

3.Add a HTTP request sampler - This is our request-2

3.aNow that we have retrieved id's from our previous request we need those to send as a request, Hence we need to Pre Process before a request is made.

3.b After adding your HTTP request sampler-2

3.c Right Click on the HTTP Sampler > Add > Pre-Processors > Beanshell Pre processor
Have a look at the figure

3.d Now let's take a closer look at the BeanShell PreProcessor

3.e Give a meaningful name for the PreProcessor just like i have given.

3.f Now add the script where you want to build your request.

int count = Integer.parseInt(vars.get("GET_EVENT_ID_matchNr"));

for(int i=1;i<=count;i++) {

String prefix = "eventInTopPackage[";

sampler.addArgument( prefix+vars.get("GET_EVENT_ID_" + i)+"]", vars.get("GET_EVENT_ID_" + i) );


Let's understand what we are achieving in the above script.

3.g Get count of the matched id's that were retrived from Xpath Extractor.

prefix - Is constant/static value

param - Will now have eventInTopPackage[] event_id_value

Have a look at the screen shot how request will look
automationwithselenium.blogspot.com-Google pagerank and Worth

No comments:

Post a Comment