Save and Use Variables
Save variables and substitute them in scripts to increase the flexibility of your tests.
Save Variables
There are a number of commands you can use to either set a literal value or randomly generate a value to save as a variable.
A variable name can be any alphanumeric value with a length greater than 0. Variables are case sensitive. They can be overwritten multiple times and will assume the last value that was saved to it.
Save Literal Values
Command | Examples | Explanation |
---|---|---|
save "<Value>" as <Variable> | save "feedback@toffeetesting.io" as email | Saves a literal value as the stated variable. |
save "<Value>" as masked <Variable> | save "Q@2#asfo$@" as masked password | Saves a literal value as the stated masked variable. Any time the value of the variable would be displayed, a constant-length mask is shown in its place. This is done to prevent the length of the actual value of the variable from being known. |
Save Contents of Elements
Command | Examples | Explanation |
---|---|---|
save value of <Locator> as <Variable> | save value of textbox with id "textboxId" as userInput | Saves the "value" attribute of the element as the stated variable, or if the attribute doesn't exist, the text contained by the element. Requires Toffee Performer version 723 or higher. |
save attribute "<Attribute>" of <Locator> as <Variable> | save attribute "href" of link "Documentation" as DocLink | Saves the specified attribute of the element as the stated variable. If the attribute does not exist for the element, the step will fail. Requires Toffee Performer version 723 or higher. |
Save Random Values
Command | Examples | Explanation | |
---|---|---|---|
save random number from <Start> to <End> as <Variable> | save random number from 1 to 6 as D6roll | Saves a random number between the start and end numbers inclusively as the stated variable. | |
save random [case] <type> string of length <Length> as <Variable> |
| Saves a random grouping of letters and numbers of the specified length as the stated variable. The case is optional and can either be lowercase or uppercase. If the case is not defined, then mixed-case will be used for all types. The four possible types are alpha (only letters), numeric (only numbers), alphanumeric (letters and numbers), and hexadecimal (0-9 and A-F). | |
save random lowercase alphanumeric string of length 8 as AlphaNums | |||
save random uppercase hexadecimal string of length 6 as bgColor | |||
save random [case] <type> string with spaces of length <Length> as <Variable> | save random uppercase alphanumeric string with spaces of length 8 as 09AZwSpaces | Saves a random grouping of letters, numbers, and spaces of the specified length as the stated variable. As above, the case is optional and either lowercase or uppercase, and there are the four types: alpha, numeric, alphanumeric, and hexadecimal. | |
save random regex "<Regular Expression>" as <Variable> | save random regex "([1-9]\d{1,2}) (red|green) apples" as inBasket | Generates a random string that matches the regular expression and saves it as the stated variable. Any grouping that specifies a range without an upper limit defaults to the lower limit + 10. In other words, | |
save random mask "<Mask>" as <Variable> | save random mask "pl'ate '# AAA-####" as plate | Generates a random string that matches the mask given and saves it as the stated variable. Specific characters get replaced by the mask. All other characters do not get replaced by the mask. The characters are as follows: | |
# | Digit (0-9) | ||
a | Lowercase letter (a-z) | ||
A | Uppercase letter (A-Z) | ||
Z | Either-case letter (a-z or A-Z) | ||
n | Lowercase letter or digit (a-z or 0-9) | ||
N | Uppercase letter or digit (A-Z or 0-9) | ||
M | Any letter or digit (a-z, A-Z, or 0-9) | ||
h | Lowercase hexadecimal digit (0-9 or a-f) | ||
H | Uppercase hexadecimal digit (0-9 or A-F) | ||
J | Any hexadecimal digit (0-9, a-f, or A-F) | ||
' | Make the next character definitely a literal |
Use Variables
In order to use a saved variable, they must be substituted into a command. Variables are substituted per each command just before the command is run. There are two forms you can use to substitute in a variable for its value.
You can substitute variables into manual commands as well. For aliases, variables won't be substituted when an alias is created, but they will be substituted both before and after an alias substitution.
Quoted Substitution
To substitute a variable into a command as a quoted string, you can use the ${Variable}
syntax.
Here are some examples of its use:
set textbox with id "lst-ib" to ${query}
click link ${linkLabel}
test that element with id ${newElement} exists
Literal Substitution
Some commands don't allow a certain word to be quoted. You may also want to substitute a variable into a string without quotations, for example, into an XPath expression. To substitute a variable into a command as a literal, you can use the #{Variable}
syntax.
Here are some examples of its use:
start #{browser}
load page at http://#{serverIP}/index.html
A variable may not be substituted if it is not defined (either for aliases or declared variables). If this is the case, then the characters (including the #, $, and brackets) will not be replaced.