Variables in API Testing

Variables in Robonito allow you to capture dynamic values from an API response and reuse them across subsequent API requests or UI test steps. Using variables eliminates fragile hardcoding and enables dynamic, data-driven test automation.


Why Use Variables in API Testing?

Modern web APIs rely heavily on dynamic identifiers and session state:

  • Authentication Tokens: Extracting Bearer JWTs or OAuth tokens from login endpoints to authenticate downstream calls.
  • Auto-Generated IDs: Capturing generated database IDs (e.g., userId, orderId, invoiceNumber) from creation endpoints.
  • Workflow State: Carrying status tokens or correlation IDs across microservice boundaries.
  • Environment Portability: Decoupling tests from hardcoded hostnames so they run seamlessly across local, staging, and production environments.

Variable Types & Scopes

Robonito supports three levels of variables in API testing:

Variable TypeScopeHow It Is DefinedExample Use
Step-Extracted VariablesCurrent test case and downstream steps.Extracted from an API response body or header via the Variables tab.{{newUserId}}, {{authToken}}
Global / Environment VariablesEntire workspace and all suites.Configured in Workspace Settings → Variables.{{baseUrl}}, {{adminPassword}}
Dynamic Random GeneratorsRe-evaluated on every run.Built-in template tags provided by Robonito.{{$randomEmail}}, {{$randomName}}

Extracting Variables from an API Response

To extract a dynamic value from a response:

  1. Open the API test case step that produces the data.
  2. Navigate to the Variables tab in the step configuration pane.
  3. Click Extract Variable.
  4. Configure the extraction parameters:
    • Variable Name: Enter an identifier (e.g., accessToken or userId). You will use this identifier downstream as {{accessToken}}.
    • Source:
      • Response Body (JSON) — Extract values from structured JSON payloads using JSONPath.
      • Response Header — Extract values from HTTP response headers (e.g., Set-Cookie, Location).
      • Status Code — Capture the numeric HTTP status code.
    • JSONPath Expression: Provide a path that isolates the exact property.

Variable Extractions Config


JSONPath Syntax Reference

When extracting from a JSON response body, Robonito uses standard JSONPath expressions:

JSON Response StructureJSONPath ExpressionExtracted Value
{ "token": "xyz123" }$.token"xyz123"
{ "user": { "id": 42 } }$.user.id42
{ "items": ["apple", "banana"] }$.items[0]"apple"
{ "data": [{ "id": 10 }, { "id": 20 }] }$.data[1].id20
{ "meta": { "total": 150 } }$.meta.total150

JSONPath Best Practice:

Always use specific paths (e.g., $.data.user.id) rather than recursive wildcards ($..id) to prevent extraction ambiguity when response structures contain multiple nested objects with similar keys.


Injecting Variables Downstream

Once extracted, a variable is immediately available to all subsequent steps in the test case. You inject it by enclosing the variable name in double curly braces: {{variableName}}.

Supported Injection Locations

  1. Endpoint URLs:
    https://api.example.com/v1/users/{{userId}}/orders
    
  2. Query Parameters:
    Key: search
    Value: {{searchTerm}}
    
  3. HTTP Headers:
    Key: Authorization
    Value: Bearer {{authToken}}
    
  4. JSON Request Body:
    {
      "customerId": "{{userId}}",
      "items": [
        { "productId": "{{productId}}", "quantity": 1 }
      ],
      "email": "{{userEmail}}"
    }
    

Variable Propagation to Web UI Tests

Robonito's architecture allows API test cases to be embedded directly inside Web UI test flows using the Reuse Test step:

  1. Run an API authentication test case to extract {{authToken}}.
  2. In subsequent Web test steps, the token is available to local scripts or storage state injectors.
  3. This saves significant execution time by allowing test suites to bypass repetitive web login forms.