Runscope Documentation

Runscope Radar: Scripts

Scripts are snippets of JavaScript connected with requests to handle more advanced variable extraction and assertion scenarios. Standard JavaScript modules like Math and Regexp are available along with a set of included helper libaries. Scripts have access to the current variable context and complete request and response data.

Execution Order

After a request within a test is executed, each of the associated scripts will be processed. Scripts are processed in addition to Variables and Assertions defined through the test editor. The execution order is as follows:

  1. HTTP request is executed.
  2. Variables extractions defined in the editor are processed.
  3. Scripts are processed. Initial and request-specific variable values extracted from previous steps are available for use.
  4. Assertions defined in the test editor are processed.

Defining Assertions

Scripts allow for complex assertion definitions that are not possible to define in the test editor. Assertions are defined using the assert module of the Chai Assertion Library which is included for every script.

Common Assertion Syntax Reference
assert(expression, message)
Write your own test expressions.
assert('foo' !== 'bar', 'foo is not bar');
assert(Array.isArray([]), 'empty arrays are arrays');
assert.ok(object, [message])
Asserts that object is truthy with an optional descriptive message.
assert.ok('everything', 'everything is ok');
assert.ok(false, 'this will fail');
assert.notOk(object, [message])
Asserts that object is falsy with an optional descriptive message.
assert.notOk('everything', 'this will fail');
assert.notOk(false, 'this will pass');
assert.equal(actual, expected, [message])
Asserts non-strict equality (==) of actual and expected.
assert.equal(3, '3', '== coerces values to strings');
assert.notEqual(actual, expected, [message])
Asserts non-strict inequality (!=) of actual and expected.
assert.notEqual(3, 4, 'these numbers are not equal');
Additional Assertion Syntax Options

Chai offers additional assertion options including checking for nulls, strict equality comparisons, type checking, regex matching, deep object comparisons and more. The library also includes should and expect assertion styles.

View Complete Assertion Syntax Documentation

// check for specific status code
assert.equal(response.status, 200, "status was 200 OK");

// parse JSON response body into object
var data = JSON.parse(response.body);

// check for specific JSON attribute value
assert.ok(data.is_admin, "customer is an admin")

// check an array for the presence of an item
var exists = false;
var customers = data.customers;
for (customer in customers) {
    if (customers[customer].id === 123) {
        exists = true;
assert.ok(exists, "customer 123 exists");

// check that all items in a list contain a numeric id with regex and Underscore.js library
assert(_.every(data.customers, function(customer) { return^\d+$/); }), "IDs are all numeric");

// check for existence of key named id with Underscore.js library
assert(_.has(data, "id"), "contains 'id' key");

// check that a timestamp is less than now with Moment.js library
var created_at = moment.unix(data["created_at"]);
var now = moment();
assert(now.isAfter(created_at), "create date before now"); 

Getting and Setting Variables

Scripts have access to all Variables that have been defined in Initial Variables/Initial Script, the test editor (see: Execution Order) and previous scripts through the variables global object. Setting a variable value will make it available to subsequent scripts and requests.

Getting a Variable Value
var id = variables.get("id");
Setting a Variable Value
// grab a newly-created user ID and store for later
var data = JSON.parse(response.body);

Built-in Variables and Functions

Every script has access to the following values:

Response Data
response.body The response body as a string.
response.headers A dictionary of response headers. The keys of the dictionary represent the header names. The dictionary values are arrays of values for the given key.
response.reason The reason portion of the HTTP status line returned.
response.response_time_ms The time taken to execute the request and receive the response in milliseconds.
response.size_bytes The size of the response body in bytes.
response.status The response's status code.
Request Data
request.body The request body as a string.
request.form A dictionary of form parameters sent with the request. The keys of the dictionary represent the parameter names. The dictionary values are arrays of values for the given key.
request.headers A dictionary of the request headers. The keys of the dictionary represent the header names. The dictionary values are arrays of values for the given key. The hostname of the URL executed.
request.params A dictionary of URL parameters (querystring) sent with the request. The keys of the dictionary represent the parameter names. The dictionary values are arrays of values for the given key.
request.path The path segment of the URL without URL parameters.
request.scheme http or https
request.size_bytes The size of the request body in bytes.
request.url The fully-assembled URL that was accessed for the request.
Radar Test Variables
variables.get(name) Return a value from the current variable context. See: Getting and Setting Variables.
variables.set(name, value) Set a variable value. See: Getting and Setting Variables.
Helper Functions
log(Object|string) Write a string or pretty-printed version of an object to the output log viewable in test results.
encode_base64(value) Encode a string value as Base64.
decode_base64(value) Decode a Base64-encoded string.
runscope_bucket The key of the bucket the test belongs to and was executed in.
JavaScript Functions

The script interpreter also supports common JavaScript objects and functions:

  • Math
  • Date
  • RegExp
  • parseInt
  • parseFloat
  • decodeURI
  • decodeURIComponent
  • encodeURI
  • encodeURIComponent
  • escape
  • unescape
  • btoa (URL-friendly Base64 encode)
  • atob (URL-friendly Base64 decode)

Initial Script

For each test, you can specify a script that runs prior to the first request. If you have a complicated signature or other value that needs to be generated and is common to all requests (e.g. random number generator), this is the place to define it. The initial script is processed after the Initial Variables are processed and has access to those values through the variables global. Any variable values set with variables.set(name, value) will available to the requests in the test. Any assertions defined in the initial script will be ignored.

Initial Scripts can also be defined as part of a bucket's Bucket-Wide Settings. The Initial Script specified at the bucket level will be executed before the Initial Script defined for an individual request.

Included Libraries

The following libraries are automatically included for every script:

Library Version Restrictions  
Underscore.js 1.7.0   View docs
Chai Assertion Library 1.9.0   View docs
Chai JSON-Schema 1.1.0   View docs
Moment.js 2.8.4 Includes locales. View docs
CryptoJS 3.1.2 AES, SHA-1, SHA-256, HMAC-SHA1, HMAC-SHA256, MD5 only View docs
json2.js 2014-02-04   View docs
marknote XML Parser 0.5.1 HTTP retrieval disabled View docs

Next: Scheduling →

Everything is going to be 200 OK

Sign Up — Free