2013-04-12

How to parameterize jmeter?

In this article we are going to see how can we parameterize jmeter. That means , we will see how can we use variable passing values in jmeter. 
Mainly, this article is for novice like me. There are plenty of resource you may find from google. I am just making this for learning quickly.

So, first we have to
1. Declare the variable in some place in test plan
2. We have to use the variable in the test plan
As, jmeter test step will need the value during execution, so we can input value in 3 ways
1. Directly provide value(static)
3. Provide the value using txt/csv file
3. Provide value from previously performed step(ex-from any response data of previous test/step run)

1. Directly Value Insertion :(Static value)
Step 1: Go to test plan Insert
Step 2: Add a user define variable
Step 3 : Provide a variable name and its value.
Name section is the name of the variable, and value section is for value of the variable to use.
 
Step 4: Go to your request( i have used post http request) and use in following way
${NameOfTheVariable}. For example , I use NAME as name of my variable, so ${NAME}.
What will happen?.. Jmeter will send request as replacing the NAME with its value(in this case shantonu)


Standard Uses :
- Variable to send static requests
- Variable as name of the domain(to control main domain centrally)
- Variable as predefined data for comparison or validation
- Variable for debugging test cases

We can also  assign the user define variable from inside the Test Plan.
Add > config elements > User Defined Variables
 
This does the same as from Test Plan. But, we have to ensure that this should be assign before the step where that values can be used. It means , variable should be present before use either as parent or as ahead step.

Google plugin : jp@gc - Parameterized Controller does the same thing.


To add this , Add>Logic Controller >jp@gc - Parameterized Controller.
Well this plug in mainly used for referring other variables. We can use multiple jp@gc - Parameterized Controller. We can make a tree type reference relation between variables(one variable will point another , like that) with this. But, we need to keep careful about sequence of using this controller. In a test plan, this controller work sequentially(variable will pass one to another)

2. Value from CSV/TXT : 
Step 1: Add a  config element CSV Data Set Config under test plan
Step 2: Define the file from where data will be fetched(variable name and value)
Step 3: We can use some function like_CSVRead() and_StringFromFile()

I will Make a separate post on how to work with CSV Data Set Config. I am just showing a screen shot.

There is one issues with this ,  It can pass values to only one other config element which is Authentication manager.It does not pass value to other config elements.
So, mostly we use Google plug in.

Google Plugins :  jp@gc - Variables From CSV File

I get those variable with values from the text File mention in right portion. We can check by clicking  "Test CSV". In here I use comma "," as separator. You can use ; , : or other symbols.
 To add this Add > config elements > jp@gc - Variables From CSV File
It is best to use google plugins as we can test values during test case making.

We can also use Function Helper . It is in the Option menu.


Step 1 : Open Function helper
Step 2 : click _CSVRead , you will get the helper name & value column.
Step 3 : Add the file path where is your file( if you don't want to provide full path, keep the file in the bin folder and provide the file name only.
Step 4 : Click Generate, and you will get the reference function variable. Copy that.
Step 5 : Add a pre processor "User Parameters"  where you will use the variable
[Keep eye in my blog as I am going post detail on pre processors]
Step 6 : Add a variable
Step 7 : Add the variable name and assign user_1 as copied function variable.
This will take the value from the file and assign to NAME.
Note : This is used for specific cases where we need to define variable separate from each requests as well as thread.
I will make a different post on Function  Helper and its capabilities.

3. Value from previously performed step : 
The main concept is, we will fetch variable dynamically (depend on previous response data) in run time. Main cause for that, we will get some variable changing over time like token, cookie, JSON, session etc. If we use dynamic parametrization, we can send our requests properly. To do this
Step 1 : Add a suitable post processor with the response request where the value can be present
Step 2 : Make a variable declaration in the post processor
Step 3 : Use the variable with the new requests.
To know more about post processors , see my previous post.

I will try more examples in future.

Thanks...:)

4 comments:

  1. Hi shantonu
    When I am recording script my login information goes in PostBody.
    I have tried adding the same data in Parameter tab but it is giving warning.
    Can u plz help out me how to parametrise in this case

    ReplyDelete
    Replies
    1. Actually, when you log in, browser sets cookie in session for that particular credential. When you record and playback later, the session become invalid. So, you have get the step where the session is assigned. add a post processor(regular expression is the best) that records that session and keep in a variable. And, you have to use the variable for the steps which uses the session. (in summary, you are making a static value into dynamic parametrize value)

      Use regular expression extractor and you may read regular expression post to get basic idea.

      Delete
  2. hi shantonu,
    My test scenario is to run 100 thread with login credential for a website and after one or two second add another 100 thread with their value.Could u tell me which will be my steps in test plan.Thanks in advance and really appreciate ur contribution in jemeter.

    ReplyDelete
    Replies
    1. you need to know a lot about step by step learning.. https://jmeter.apache.org/usermanual/build-web-test-plan.html

      Delete