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.
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 = tg.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();
tg.flow.set("username", data.username);
tg.flow.set("password", data.password);
console.log("Registered:", data.username);
Exporting Reports
Export in afterAllScript
tgrs 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 = tg.export.html();
await fetch("https://reports.example.com/html", {
method: "POST",
headers: { "Content-Type": "text/html" },
body: html,
});
// Export PDF report
var pdf = await tg.export.pdf();
await fetch("https://reports.example.com/pdf", {
method: "POST",
headers: { "Content-Type": "application/pdf" },
body: pdf,
});
// Export JSON report
var json = tg.export.json();
await fetch("https://reports.example.com/json", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: json,
});
Available Export Formats
tg.export.html()Interactive HTML report with embedded chartstg.export.pdf()Printable PDF report (async — requires await)tg.export.json()Structured JSON report for integrationsAvailable APIs
Script API Overview
Scripts have access to the following APIs. These are configured in Tigrister Pro when writing your pre/post scripts and lifecycle scripts.
| API | Description |
|---|---|
| console.log() | Log messages during execution |
| tg.request | Read/modify request (url, method, headers, body) |
| tg.response | Read response (status, headers, body, json()) |
| tg.flow.get() / set() | Get/set flow variables that persist between steps |
| tg.environment.get() / set() | Read/write environment variables |
| tg.export.html() / pdf() / json() | Generate reports (afterAllScript only) |
| fetch() | Send HTTP requests from scripts (lifecycle scripts only) |
Limitations
await and fetch() are only available in lifecycle scripts (beforeAllScript / afterAllScript). Pre/post scripts run synchronously — use flow variables for data passing between steps.