Variable Usage

Variables are an easy way to create dynamic and powerful scripts !

Variable definition

1) A variable can be predefined (click here to see the list of the predefined variables).

2) A variable can be defined using a setvar action. For example:

- setvar:
    variable: my_var
    expression: "2 * age"    # here 'age' is another variable !

- log:
    message: "this is my age: ${my_age}"

Both the variable and expression keys are mandatory. expression is a string that defines the expression to be evaluated. If the expression is an integer or a float, it will be converted into a string (more on expression syntax here).

3) A variable can be defined when you parse the response of a request. For example, you can apply a regular expression on a HTTP response and create a variable which value is returned by the regexp (click here for more explanations).

Variable usage

Variables can be used in the following action parameters, http.url, http.body, mqtt.url, log.message, mqtt.url, mqtt.topic, mqtt.payload, mongodb.server, mongodb.document, mongodb.filter, sql.statement.

In these cases, the variable names must be enclosed between ${....}.

For example:

- http:
    title: Page 3
    method: GET
    url: http://server/page3.php?name=${name}

- log:
    message: Address value is ${address} (customer=${customer})

# The HTTP_Response variable is always set after a HTTP action
- log:
    message: HTTP return code=${HTTP_Response}

Trying to access an undefined variable produces an error message and makes the VU stop by default.