Using Variables

Variables are a key feature of Badboy. They let you customize how script items are played back at run time so that you can create scripts that behave intelligently rather than simply repeating the same action over and over again. This section describes how you can add and use variables in Badboy scripts.

Adding Variables

There are several ways to add variables in Badboy. First, you can add them directly by right-clicking in the variable window at the bottom left corner of the screen.

In order to make adding variables even easier Badboy offers a couple of other ways to do it:

  • right click on a parameter in the script tree and select "Add Variable..." or "Add Linked Variable". The first of these will simply add a variable with the same name as the parameter. The second will add a variable and search for the value throughout all the other parameters in the script, linking all parameters with the same value as the variable to it.
  • Use the "Tools->Create Hostname Variable" option in the menu. This is specifically for creating a variable to control the host name of all your requests.

Viewing Variables

You can see all the variables you have added to your script in the Variable View. This view is a tab in the Summary View which is normally at the bottom left hand corner of your Badboy Window (although you can move it around by dragging it). The figure below shows how the Variable View looks:

Editing Variables

Editing the value of a variable can be performed by opening it's properties dialog - just right click on the variable and select "Properties".

Using Variables

You can use variables nearly anywhere that you can enter a value into Badboy. To use a variable, refer to it by placing it inside curly brackets with a dollar sign in front. For example, to use a variable "foo", you could set a field in Badboy to be "${foo}". You can also use a variable embedded in the middle of other text. For example, if a variable is called "animal" and has a value "tree frog", then I can make a sentence "the ${animal} is green" using the variable. When Badboy plays your script it will substitute the values of variables into where you have put variable references.

Variable Value Lists

Variables in Badboy have two components:

  • The current value (this is the value that is used when the variable is evaluated in your script)
  • A list of future values

You can set the list of values that a variable has by opening the variable properties dialog (double click in the Variable View or right click and select "Properties"). When you do this you can add a list of values to the variable. If you want to make an expression that refers to a particular value in a variable's value list then you can place the index of the value in square brackets after the variable's name. For example, ${animal[3]} would refer to the fourth value in the value list for the variable "animal". (note that variable values are numbered starting at zero!)

Incrementing Variables

A powerful way of using variables becomes apparent when you combine them with "Increments". Incrementing simply means to change the value of a variable to a new value, based on a strategy that you can specify. By default, Badboy will first look for the next value in a the variables Value List (see below). If the variable doesn't have a list of values set then it will just assign a random value to the variable. See Incrementing Variables for more information about using Increments.

Setting Variables as Part of your Script

Sometimes you may want to set a variable as part of playing your script. Often it is useful for a variable to contain the result of an operation that your script has performed so that it can be used elsewhere. For example, if your script creates an "Order", you might make a variable to contain the order number so that it can be used in other parts of your script later on. To help you do this, Badboy provides a "Variable Setter" item in the Toolbox.

The Variable Setter properties dialog offers a number of ways for you to set the value for a variable. These include:

  • Setting a list of fixed values separated by commas
  • Setting values by extracting content from the browser window (based on a regular expression match)
  • Loading content from a file

The figure below shows the Variable Setter Properties dialog and the options that are available:

Variable Setter

Regular Expression Notes

Regular expressions are a particularly powerful way to set variables, because they allow you to scan the contents of the browser window and pick specific elements into your variable. The variable can then be used in playing other items later in the script. If the expression matches multiple times then you can loop over the values of the variable by setting the properties in a Step to iterate over the variable.

Sometimes you want to set a variable "opportunistically". That is, you don't know when a value will appear on the page, but if it is you want to store it. To do this, you can select the "Do Not Overwrite with Blank" option so that Badboy will only update the value and not set it to blank if a matching value is not found on the page.

Good references on Regular Expressions can be found by searching the internet, however some simple notes on how Badboy implements the expressions are as follows:

  • Brackets "(" and ")" are automatically special characters. If you really want to match a bracket then you must escape it with a back slash as "\)"
  • If you provide a sub-expression using brackets then Badboy will populate the variable with the first subexpression. If you do not provide a sub-expression then Badboy will place the match of the whole expression into the variable. For example, assume your content is "tree frogs live long". The expression "tree.*long" will return a match of the whole sentence because there is no subexpression. However the match "tree (.*) long" will return the match of the sub-expression in brackets, "frogs live".
  • By default Badboy will match the regular expression against the main Badboy window. If you want to match against a popup window, set the "target window" in the Play Properties for the Variable Setter.

Automatic Variables

Badboy has a special kind of variable called an "Automatic Variable". See the section Automatic Variables for more information on these.


Badboy Documentation Generated on Mon Dec 29 22:28:43 EST 2008