Scripting

Write JavaScript lifecycle scripts to set up test data, export reports, and send notifications. Available in both flow and load test specs.

Lifecycle Scripts

When Scripts Run

beforeAllScript

Runs once before the entire test or flow starts. Use for setup — register users, create test data, set flow variables.

afterAllScript

Runs once after the entire test or flow completes. Use for teardown — export reports, send notifications, clean up data.

Pre-Script / Post-Script

Run before and after each individual step. Use for dynamic headers, token extraction, and logging.

Note: All script types are available in both flows and load tests. Scripts are written in Atrahasis Pro and executed by atra at runtime. For the full Scripting API reference, see the Scripting API section in the Atrahasis Pro guide.

Setup with beforeAllScript

Register a User Before Testing

Use beforeAllScript to set up test data before the flow or load test starts.

// Register a user and store credentials for later steps

var baseUrl = at.environment.get("base_url");

var res = await fetch(baseUrl + "/register", {

method: "POST",

headers: { "Content-Type": "application/json" },

body: JSON.stringify({

username: "{{random.string}}",

password: "{{random.string}}",

email: "{{random.email}}"

}) });

var data = await res.json();

at.flow.set("username", data.username);

at.flow.set("password", data.password);

console.log("Registered:", data.username);

Exporting Reports

Export in afterAllScript

atra does not have separate export flags — reports are generated and sent via afterAllScript. This gives you full control over where and how reports are delivered.

// Export HTML report and send to a server

var html = at.export.html();

await fetch("https://reports.example.com/html", {

method: "POST",

headers: { "Content-Type": "text/html" },

body: html,

});

// Export PDF report

var pdf = await at.export.pdf();

await fetch("https://reports.example.com/pdf", {

method: "POST",

headers: { "Content-Type": "application/pdf" },

body: pdf,

});

// Export JSON report

var json = at.export.json();

await fetch("https://reports.example.com/json", {

method: "POST",

headers: { "Content-Type": "application/json" },

body: json,

});

Available Export Formats

at.export.html()Interactive HTML report with embedded charts
at.export.pdf()Printable PDF report (async — requires await)
at.export.json()Structured JSON report for integrations

Available APIs

Script API Overview

Scripts have access to the following APIs. These are configured in Atrahasis Pro when writing your pre/post scripts and lifecycle scripts.

APIDescription
console.log()Log messages during execution
at.requestRead/modify request (url, method, headers, body)
at.responseRead response (status, headers, body, json())
at.flow.get() / set()Get/set flow variables that persist between steps
at.environment.get() / set()Read/write environment variables
at.export.html() / pdf() / json()Generate reports (afterAllScript only)
fetch()Send HTTP requests from scripts (lifecycle scripts only)

Limitations

Limitation: Top-level await and fetch() are only available in lifecycle scripts (beforeAllScript / afterAllScript). Pre/post scripts run synchronously — use flow variables for data passing between steps.