-
Notifications
You must be signed in to change notification settings - Fork 11
Beanshell
Beanshell commands can be used via variables. Most of this section does not rely on programming knowledge. However, if you wish to extend the list of commands, some programming knowledge may be helpful.
Here is a list of all available commands:
-
${NOW.toString()}: Returns the current time in milliseconds -
${RANDOM.Number(int)}: Returns a number between 0 and the provided argument (inclusive) -
${RANDOM.Number(int, int)}: Returns a number between the first and second argument (inclusive) -
${RANDOM.String(int)}: Returns a string with the length of the provided argument. Characters are chosen from the Latin alphabetic characters (a-z, A-Z). -
${RANDOM.String(String, int}: Returns a string with the length of the provided argument. Characters are chosen from the String argument. -
${RANDOM.DigitString(int)}: Returns a string of numeric characters (0-9) with the length of the provided argument. -
${RANDOM.Email()}: Returns a string, that specifies an email. The local-part consists of 5 random alphabetic characters. The domain also consists of 5 random alphabetic characters and ends with ".com" at the end. -
${RANDOM.Email(int)}: Like ${RANDOM.Email()}, but the number of random alphabetic characters for the local-part and domain is the same as the provided argument. -
${RANDOM.Email(int, int)}: Like ${RANDOM.Email()}, but the number of random alphabetic characters for the local-part is the same as the first argument, and the number of random alphabetic characters for the domain is the same as the last argument. -
${DATA.}: For methods starting with DATA, take a look at the GeneralDataProvider. All methods are useable by simply writing${DATA.<methodName>}and exchanging with the name of the method you want to use. Don't forget the parameters! -
${DATE.}: Methods starting with DATE rely on java.util.Date. Therefore, take a look at the class to see all available methods. - Other static commands, that are defined in some library, can also be used as long as the full qualified name of the class is specified. Therefore, to access
Random.nextInt()of thejava.utilpackage, you would have to use the expression${java.util.Random.nextInt()}.
To add new commands, you need a class, that adds the functionality as static method, and register it with Beanshell Interpreter.
Assume, that you need a method, that returns a random number between a minimum and a maximum. You want to receive a random number with the following command: ${MYRANDOMNUMBER.getRandomNumberBetween(minimum, maximum)}
- Writing the class
For this, you would create a class and name it, for example, RandomNumberGenerator. The class could look something like this:
package com.xceptance.xlt.nocoding.util.variableResolver;
public class RandomNumberGenerator
{
public int getRandomNumberBetween(int min, int max) {
// ...
}
}- Register with Beanshell
To register the new class with its method in the beanshell interpreter, you need to open the file com.xceptance.xlt.nocoding.util.variableResolver.VariableResolver.
Search the constructor _VariableResolver(GeneralDataProvider), which looks something like this:
public VariableResolver(final GeneralDataProvider dataProvider)
{
interpreter = new Interpreter();
try
{
interpreter.set("NOW", new ParameterInterpreterNow());
interpreter.set("RANDOM", new ParameterInterpreterRandom());
interpreter.set("DATE", new Date());
interpreter.set("DATA", dataProvider);
}
catch (final EvalError e)
{
e.printStackTrace();
}
}Now you simply need to add this line in the try-block:
interpreter.set("MYRANDOMNUMBER", new RandomNumberGenerator());
With this, you can now get a random number with ${MYRANDOMNUMBER.getRandomNumberBetween(minimum, maximum)}.