← Back to Overview

TESTING

Test Scripts

JavaScript that runs after the response is received — assert values, extract data, set environment variables, and log debug information.

Purpose

Test scripts are JavaScript that runs after the HTTP response is received. Use them to assert response values, extract data into variables, and log debug information.

Timing: Runs after GUI test rules and set variables have been evaluated. The response object is fully populated at this point.

Writing Scripts

Open the Scripts tab and write JavaScript in the test script editor (below the pre-request editor).

Same sandboxed VM as pre-request scripts
5-second timeout — same execution limit
Has access to both request (read-only) and response objects

Response Object

The response object is read-only and provides full access to the HTTP response:

PropertyTypeDescription
response.statusnumberHTTP status code (200, 404, etc.)
response.statusTextstringStatus text ("OK", "Not Found")
response.headersRecord<string, string>Response headers
response.bodystringRaw response body text
response.json()anyParsed JSON (throws if not valid JSON)
response.timenumberResponse time in milliseconds
response.sizenumberResponse body size in bytes

Assertions

Use console.assert(condition, message) to create test assertions. Failed assertions appear as test failures in the Results tab.

Example assertions

// Check status code
console.assert(response.status === 200, 'Expected 200');

// Check response body
const data = response.json();
console.assert(data.items.length > 0, 'Should have items');
console.assert(data.total !== undefined, 'Should have total count');

Setting Variables

Extract values from the response and save them to the active environment for use in subsequent requests.

Extract and save a token

environment.set('token', response.json().access_token);
Saved to the currently active environment
Available in subsequent requests (especially useful in collection runs with sequential mode)

Console Output

console.log() messages appear in the Test Results tab as "Script Logs". Useful for debugging script logic.

Debug logging

console.log('Status:', response.status);
console.log('Body length:', response.body.length);
console.log('Headers:', JSON.stringify(response.headers));

Output is limited to string values. Objects are automatically stringified.

Next

Ko-fi