Expressions

VIVIDUS provides a set of pre-defined expressions. Also plugins may define own expressions (they are documented in the corresponding plugins articles).

The expression parameters marked with bold are mandatory.

Data generation

randomInt

Generates a random integer value between the specified origin (minInclusive) and the specified bound (maxInclusive).

#{randomInt($minInclusive, $maxInclusive)}
  • $minInclusive - the least value

  • $maxInclusive - the upper bound

Table 1. Examples of the expressions generating random integer value
Expression Result

#{randomInt(1, 10)}

An integer between 1 and 10

#{randomInt(100, 999)}

An integer between 100 and 999

#{randomInt(-5, 5)}

An integer between -5 and 5

#{randomInt(-5, -2)}

An integer between -5 and -2

#{randomInt(1, 1)}

1

String manipulations

replaceFirstByRegExp / replaceAllByRegExp

Replaces the first / every substring of the input that matches the regular expression with the given replacement string.

Online tools like RegexPlanet or Regex101 can be used to test and debug regular expressions

#{replaceFirstByRegExp($regularExpression, $replacement, $input)}
#{replaceAllByRegExp($regularExpression, $replacement, $input)}
  • $regularExpression - the regular expression to match substring(s)

  • $replacement - the replacement string, it may contain references to captured substrings, e.g. $1 is a reference to the first group

  • $input - any string to be processed

The expression parameters containing commas must be surrounded with triple quotes: """
Description Expression Result

Extract ID from the string

#{replaceFirstByRegExp(product-(\d+), $1, product-86)}

86

Extract ID and build new string with it

#{replaceFirstByRegExp(/user/(\d+), author/$1, /user/21)}

author/21

Extract password from the string with comma

#{replaceFirstByRegExp(.*new password is (\d+), $1, """Updated, new password is qwe123""")}

qwe123

Replace whitespaces with dashes

#{replaceAllByRegExp(\s, -, convert spaces to dashes)}

convert-spaces-to-dashes

Remove all numbers from the string

#{replaceAllByRegExp(\d, """""", a1b2c3d)}

abcd

toLowerCase

Converts an input string to lower case.

#{toLowerCase($input)}
  • $input - any string to be converted lower case

Table 2. Examples of the expressions converting strings to lower case
Expression Result

#{toLowerCase(aBc)}

abc

toUpperCase

Converts an input string to upper case.

#{toUpperCase($input)}
  • $input - any string to be converted upper case

Table 3. Examples of the expressions converting strings to upper case
Expression Result

#{toUpperCase(aBc)}

ABC

capitalizeFirstWord

Capitalizes an input string, changing the first character to title case. No other characters are changed.

#{capitalizeFirstWord($input)}
  • $input - any string to be capitalized

Table 4. Examples of the expressions capitalizing strings
Expression Result

#{capitalizeFirstWord(aBc)}

ABc

capitalizeWords

Capitalizes all the whitespace separated words in the input string. Only the first character of each word is changed.

#{capitalizeWords($input)}
  • $input - any string to be capitalized

Table 5. Examples of the expressions capitalizing words in the string
Expression Result

#{capitalizeWords(aBc dEf)}

ABc DEf

capitalizeWordsFully

Converts all the whitespace separated words in a String into capitalized words, that is each word is made up of a titlecase character and then a series of lowercase characters.

#{capitalizeWordsFully($input)}
  • $input - any string to be capitalized

Table 6. Examples of the expressions capitalizing strings
Expression Result

#{capitalizeWordsFully(aBc dEf)}

Abc Def

uncapitalizeFirstWord

Uncapitalizes an input string, changing the first character to title case. No other characters are changed.

#{uncapitalizeFirstWord($input)}
  • $input - any string to be capitalized

Table 7. Examples of the expressions capitalizing strings
Expression Result

#{uncapitalizeFirstWord(ABc)}

aBc

uncapitalizeWords

Uncapitalizes all the whitespace separated words in the input string. Only the first character of each word is changed.

#{uncapitalizeWords($input)}
  • $input - any string to be capitalized

Table 8. Examples of the expressions capitalizing words in the string
Expression Result

#{uncapitalizeWords(ABc DEf)}

aBc eEf

trim

Trims an input string (removes control characters (chars with code less than or equal to 32) from both ends).

#{trim($input)}
  • $input - any string to be trimmed

Table 9. Examples of the expressions trimming strings
Expression Result

#{trim( a b c )}

a b c

encodeToBase64

Encode the input string to Base64 format

#{encodeToBase64($input)}
  • $input - any string to be encoded to Base64 format

Table 10. Examples of the expressions encoding to Base64
Expression Result

#{encodeToBase64(vividus)}

dml2aWR1cw==

decodeFromBase64

Decodes the input string from Base64 format to the regular string

#{decodeFromBase64($input)}
  • $input - Base64 string to decode

Table 11. Examples of the expressions decoding Base64
Expression Result

#{decodeFromBase64(dml2aWR1cw==)}

vividus

toBase64Gzip

Compress the input string to GZip and encode compressed bytes to Base64 format

#{toBase64Gzip($input)}
  • $input - any string to be compressed and encoded

Table 12. Examples of the expressions compressing and encoding to Base64 GZip
Expression Result

#{toBase64Gzip(vividus)}

H4sIAAAAAAAAACvLLMtMKS0GANIHCdkHAAAA

escapeHTML

Escapes reserved characters in HTML string

#{escapeHTML($input)}
  • $input - any string to be escaped

Table 13. Escape HTML string
Expression Result

#{escapeHTML(M&Ms)}

M&Ms

quoteRegExp

Quotes the input literal so that metacharacters or escape sequences in the input sequence will be given no special meaning in regular expression.

#{quoteRegExp($input)}
  • $input - any string to be quoted

Table 14. Quote regular expression
Expression Result

#{quoteRegExp(Some(Value))}

\QSome(Value)\E

Example 1. Verify the data
Then `${frontEndData}` matches `#{quorePattern(${backEndData})}.*`

Hash calculations

calculateHash

Calculates the hash using the specified hashing algorithm

#{calculateHash($algorithm, $input)}
Table 15. Example of hash calculating for string using MD5
Expression Result

#{calculateHash(MD5, vividus)}

0a05ba6064ae7e5d6ee9818f85b666ad

calculateFileHash

Calculates the resource or file hash using the specified hashing algorithm

#{calculateFileHash($algorithm, $resourceNameOrFilePath)}
Table 16. Example of hash calculating for file using SHA-1
Expression Result

#{calculateFileHash(SHA-1, data/file.txt)}

0a05ba6064ae7e5d6ee9818f85b666ad

Resources

loadResource

Loads the resource by its name and replaces the expression with the content of the resource.

#{loadResource($resourceName)}
  • $resourceName - the name of the resource to load

Example 2. Load data from the project resource
When I initialize the scenario variable `my-data` with value `#{loadResource(/data/body.txt)}`

loadBinaryResource

Loads the resource by its name as bytes. It could be useful for the steps that accepting raw binary data.

#{loadBinaryResource($resourceName)}
  • $resourceName - the name of the resource to load

Example 3. Load data from the project resource as bytes
When I mock HTTP responses with request URL which CONTAINS `frames.html` using response code `200`, content `#{loadBinaryResource(page.html)}` and headers:
|name        |value    |
|Content-Type|text/html|

resourceToBase64

Finds the resource by its name and replaces the expression with the content of the resource in Base64 format.

#{resourceToBase64($resourceName)}
  • $resourceName - the name of the resource to load

Example 4. Load data as Base64 from the project resource
When I initialize the scenario variable `my-data` with value `#{resourceToBase64(/data/body.txt)}`

Script evaluation

eval

Evaluates JEXL script and converts result to a string.

#{eval($script)}
  • $script - valid JEXL script to be evaluated

  • Any Vividus variable is accessible in the JEXL script by its name

Example 5. Evaluate JEXL script
Scenario: Verify eval expression
Then `#{<expression>}` is = `<expected>`
Examples:
|expected          |expression                                                        |
|null              |eval(null)                                                        |
|28                |eval(16 + 2 * 6)                                                  |
|10                |eval(math:abs(-10))                                               |
|here              |eval(stringUtils:substringAfterLast('namescpaces are %here', '%'))|
|108               |eval((16 + 2) * 6)                                                |
|-6                |eval(100 / 5 - 16 * 2 + 6)                                        |
|true              |eval(`string\n1` == `string\n1`)                                  |
|false             |eval(`string\n1` == `string1`)                                    |
|I Am FINE         |eval(wordUtils:capitalize('i am FINE'))                           |
|i am fINE         |eval(wordUtils:uncapitalize('I Am FINE'))                         |
|tHE DOG HAS A bone|eval(wordUtils:swapCase('The dog has a BONE'))                    |
|FRD               |eval(wordUtils:initials('Fus Ro Dah'))                            |

evalGroovy

Evaluates groovy script and converts result to a string.

#{evalGroovy($script)}
  • $script - valid Groovy script to be evaluated

  • Any Vividus variable is accessible in the groovy script by its name

  • One could use any of online groovy evaluators to verify the script. For example see: Evaluator

Example 6. Evaluate Groovy script
When I initialize Scenario variable `listOfMaps` with values:
|key|
|2  |
|1  |
|3  |
Then `1-2-3` is = `#{evalGroovy(return listOfMaps.collect{it['key']}.sort().join('-'))}`

Null value

null

Represents null (a.k.a. NULL). In most case null means no value (see NULL in SQL and null in JSON).

#{null}
Null expression can only be evaluated separately. Strings or other expressions with the nested null expression will be completely ignored and not executed.
Example 7. Validation of JSON element with null value
When I initialize the scenario variable `json` with value `
{
    "persons": {
        "nemo": null
    }
}
`
Then JSON element value from `${json}` by JSON path `$.persons.nemo` is equal to `#{null}`