Friday, June 26, 2015

Create RandonString using Jmeter

I want to pass randomString to my request body , below is the request body

{
"http_session_id":"${RanDomCookie}",
"Name":"APP"
}


We have two ways ,
1. Using BeanShell scripting.
2. Using JMeter __RandomString()

1: Using BeanShell Scripting

Below are the Steps:
1. Add Http Sampler and add Add beanShell Preprocessor as child and enter below code.


import java.util.Random;


String str="abcdefghijklmnopqrst1234567890";
int String_Length=32;
String randomSting="";
for(int i=1;i<=String_Length;i++){
Random randomVal=new Random();
int randomInt=randomVal.nextInt(str.length());
randomSting+=str.substring(randomInt, randomInt+1);

       }
     
vars.put("RanDomCookie",randomSting);


Below is Output :

POST data:
{
"http_session_id":"qoxbkopzsendueghuivujhlyljystsik",
"Name":"APP"
}




2: Using JMeter "__RandomString()"

Below are the Steps:

1. Add Http Sampler and add __RandomString() as shown below


{
"http_session_id":"${__RandomString(32,abcdefghijklmnopqrstvuwxyz,0123456789)}",
"Name":"APP"
}

Explanation about RandomString():

Syntax:__RandomString(Perm1, Perm2 ,Perm3 )
Perm1: Length of the String , In my case , needed 32 character . So I passed "32", in about post method.

Perm2: Which values , server accepts, In my case lower case alphabets and numerics.

Perm3:  Name of variable in which to store the result ,Optional parameter (In my case),


No comments: