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.

4 comments:

  1. Perfect. It Works like a Jem

    ReplyDelete
  2. import org.apache.commons.lang.RandomStringUtils;

    String AssetProductCode = RandomStringUtils.randomAlphanumeric(06).toUpperCase();

    vars.put("AssetProductCode", AssetProductCode);

    You can access above variable using: ${AssetProductCode}

    ReplyDelete
  3. I really like what you guys tend to be up too. Such clever work and reporting! Keep up the wonderful works guys I've included you guys to my own blogroll.

    ReplyDelete