Script actions variables and metrics

Exchange variables with the test, create custom metrics for Analytics, and attach artifacts from a script.

Deprecated. Script actions are legacy. Existing ones keep running, but for new tests prefer the visual builder. Migrate when you can.

Script actions can read the test's input variables, write output variables for later actions, push custom metrics that behave like any built-in metric in Analytics, and attach arbitrary files to the results.

Variables

const value = test.variables.get("INPUT_VARIABLE_NAME");
test.variables.set("OUTPUT_VARIABLE_NAME", "output variable value");

Calling get automatically declares the input variable on the test, so you can fill it from the test form, a campaign override, or a previous action. Calling set declares an output variable, available to every later action.

Custom metrics

describe('My test suite', function () {
  it('Create a custom metric', function () {
    test.metrics.create("my_custom_metric",
      {
        duration: 1.85,            // numbers, strings, or objects
        status_text: "ok"
      },
      {
        service: "checkout"        // tags (optional)
      }
    );
  });
});

test.metrics.create(measurement, fields, tags) sends a measurement to Analytics exactly like a built-in metric: chart it in dashboards, alert on it, export it. The results screen does not show that a metric was created; check your dashboards.

Combine with the Timer to produce meaningful values:

it('Measure login time', function () {
  const timer = new Timer();
  // ... perform the login ...
  test.metrics.create("login", { duration_seconds: timer.stop() });
});

Artifacts

test.artifacts.create(name, content, { encoding, type });

Attach any content to the step's results, next to the automatic artifacts:

ParameterDescription
nameFile name with extension (the extension is stripped in the UI): report.json, payload.txt.
contentA string, an object (stringified), a number, or a base64 payload.
encodingOptional. Set to base64 to decode the content before storing, for binary files.
typeOptional. Free label used to influence how the artifact is displayed.
test.artifacts.create("hello_world.txt", "Hello world!");
test.artifacts.create("data.json", { key: "value", array: [1, 2, 3] });

What's next?

Script actions

Structure, parameters, hooks, and timers

Analytics

Where custom metrics show up

Last updated on