Skip to content

TestScript Variables

Variables extract and store values from responses for reuse in subsequent operations and assertions.

Basic Variable Extraction

Extract from Response

{
  "variable": [
    {
      "name": "patientId",
      "expression": "Patient.id",
      "sourceId": "createResponse"
    }
  ]
}

Use in Operation

{
  "operation": {
    "type": {"code": "read"},
    "resource": "Patient",
    "params": "/${patientId}"
  }
}

FHIRPath Expressions

Simple Path

{
  "name": "familyName",
  "expression": "Patient.name.first().family",
  "sourceId": "patientResponse"
}

Identifier Value

{
  "name": "medicareNumber",
  "expression": "Patient.identifier.where(system='http://ns.electronichealth.net.au/id/medicare-number').value",
  "sourceId": "patientResponse"
}

Reference ID

{
  "name": "subjectId",
  "expression": "Observation.subject.reference.replace('Patient/', '')",
  "sourceId": "observationResponse"
}

Variable Scope

Test-Level Variables

Defined in test setup, available throughout the test:

{
  "test": [
    {
      "name": "Patient CRUD",
      "action": [
        {
          "operation": {
            "type": {"code": "create"},
            "resource": "Patient",
            "sourceId": "patient-fixture",
            "responseId": "createResponse"
          }
        },
        {
          "assert": {"responseCode": "201"}
        }
      ]
    }
  ],
  "variable": [
    {
      "name": "patientId",
      "expression": "Patient.id",
      "sourceId": "createResponse"
    }
  ]
}

Setup Variables

Defined in setup, available to all tests:

{
  "setup": {
    "action": [
      {
        "operation": {
          "type": {"code": "create"},
          "resource": "Patient",
          "sourceId": "patient-fixture",
          "responseId": "setupPatient"
        }
      }
    ]
  },
  "variable": [
    {
      "name": "sharedPatientId",
      "expression": "Patient.id",
      "sourceId": "setupPatient"
    }
  ]
}

Common Patterns

Extract ID from Location Header

{
  "name": "createdId",
  "headerField": "Location",
  "expression": "substring-after(headerField, 'Patient/')"
}

Extract from Bundle

{
  "name": "firstPatientId",
  "expression": "Bundle.entry.first().resource.id",
  "sourceId": "searchResponse"
}

Extract Multiple Values

{
  "variable": [
    {
      "name": "patientId",
      "expression": "Patient.id",
      "sourceId": "createResponse"
    },
    {
      "name": "patientVersion",
      "expression": "Patient.meta.versionId",
      "sourceId": "createResponse"
    },
    {
      "name": "patientLastUpdated",
      "expression": "Patient.meta.lastUpdated",
      "sourceId": "createResponse"
    }
  ]
}

Variable Usage

In URL Parameters

{
  "operation": {
    "type": {"code": "read"},
    "resource": "Patient",
    "params": "/${patientId}"
  }
}

In Search Parameters

{
  "operation": {
    "type": {"code": "search"},
    "resource": "Observation",
    "params": "?subject=Patient/${patientId}"
  }
}

In Assertions

{
  "assert": {
    "expression": "Patient.id = '${patientId}'"
  }
}

In Fixtures (Dynamic)

Variables can be used in fixture references:

{
  "operation": {
    "type": {"code": "update"},
    "resource": "Patient",
    "params": "/${patientId}",
    "sourceId": "updated-patient-${patientId}"
  }
}

Default Values

Provide fallback values:

{
  "name": "serverBase",
  "defaultValue": "http://localhost:8080/fhir"
}

Complex Extractions

Nested Resources

{
  "name": "practitionerId",
  "expression": "Bundle.entry.where(resource.resourceType='Practitioner').resource.id.first()",
  "sourceId": "transactionResponse"
}

Conditional Extraction

{
  "name": "activeStatus",
  "expression": "Patient.active.toString()",
  "sourceId": "patientResponse"
}

Date/Time Manipulation

{
  "name": "observationDate",
  "expression": "Observation.effectiveDateTime.substring(0, 10)",
  "sourceId": "observationResponse"
}

Variable Diagram

Diagram

Best Practices

  1. Use descriptive names - patientId not id1
  2. Extract early - Get IDs immediately after creation
  3. Validate extraction - Assert variable exists before using
  4. Use FHIRPath - More reliable than path-based extraction
  5. Document complex expressions - Add comments explaining logic

Validation Pattern

Always validate extracted variables:

{
  "action": [
    {
      "operation": {
        "type": {"code": "create"},
        "resource": "Patient",
        "sourceId": "patient-fixture",
        "responseId": "createResponse"
      }
    },
    {
      "assert": {
        "expression": "Patient.id.exists()",
        "description": "Patient ID must be present for extraction"
      }
    }
  ]
},
{
  "variable": [
    {
      "name": "patientId",
      "expression": "Patient.id",
      "sourceId": "createResponse"
    }
  ]
}

Next Steps