Wednesday 3 July 2013

Random string creation in Jmeter using BeanShell Preprocessor


If anyone wants to create a random string in Jmeter and use it for some purpose in your code , you can do that using the BeanShell Preprocessor .

The below code snippet can be used to generate a random string using BeanShell Preprocessor.

import java.util.Random; 

chars = "1234567890abcdefghiklmnopqrstuvwxyz-"; 
int string_length = 36; 
randomstring =""; 
    for (int i=0; i<string_length; i++) { 
        Random randomGenerator = new Random(); 
      int randomInt = randomGenerator.nextInt(chars.length()); 
        randomstring += chars.substring(randomInt,randomInt+1);
    } 

vars.put("VAR_NAME",randomstring); 


Place the random string into a variable of your choice by replacing the VAR_NAME with you variable name.