Search code examples
javascriptingjmeterbeanshell

Generating sequence number in Beanshell scripting


I am new to Beanshell scripting. I am trying to generate sequential numbers, the scripting code I tried is as below

File Name: sequence.bsh

string = new String();
Long[] n = new Long[] {1000};
for (i=0; i < n; i++){
    sequence = String.format("%08d", i);
    System.out.println(sequence);
}

When I try to run this code I get the below Error:

Evaluation Error: Sourced file: sequence.bsh : Operator: '"<"' inappropriate for objects : at Line: 3 : in file: sequence.bsh : ;

The above lines of code work as expected in a compiled java program & I get sequence generated from 00000001 to 00009999.

I need to know how to rectify this operator error & assign the result to a variable so that I can use it inside a JMeter test case. something like vars.put("VARNAME", i.toString());

Thanks in advance.


Solution

  • Beanshell is not very Java, I guess you need to use DecimalFormat class instead of String.format() method.

    import java.text.DecimalFormat;
    
    DecimalFormat df = new DecimalFormat( "00000000" );
    
    int  n = 1000;
    
    for (int i=0;i<n;i++)
    {
        String sequence = df.format(i);
        System.out.println(sequence);
    }
    

    There is a nice Beanshell scripting guide that can help a lot