Runscope API Monitoring    Learn More →

Introducing Runscope Radar Scripts: Powerful Assertions and Variables with JavaScript

By John Sheehan on .


We've received a steady stream of feature requests for more powerful Assertions and Variables for Runscope Radar. API responses come in all shapes and sizes, and extracting the exact piece of data you need to assert against or pass to the next request can be tricky within the confines of HTML form fields.

Today we're introducing a new feature to give you much more flexible control over your Radar API test assertions and variables: Scripts.

JavaScript in your API Tests

Scripts are short snippets of JavaScript associated with a request within your API test (if you're unfamiliar with tests and requests, check out the Radar docs to get started). Scripts are executed once the request completes and have access to all the response data for a given request.

One of the most common requests we get is for more complex assertions against dynamic data or using conditionals. With Radar Scripts, assertions like this become easy:

Check that any item in an array matches criteria

var data = JSON.parse(response.body);
var exists = false;
var customers = data.customers;
for (customer in customers) {
    if (customers[customer].id === 123) {
        exists = true;
assert(exists, "Customer 123 exists");

Check the length of an array of items

var data = JSON.parse(response.body);
assert.equal(data.customers.length, 10, "Customer list is correct length");

The assertions in these examples use the Chai Assertion Library, which is available for all of your scripts. Assertions that fail will cause the test to fail. You can learn more about Chai and the other included JavaScript libraries below.

Not Just Assertions, Variables Too

Scripts aren't limited to just assertions, you can also extract and store data from API responses to use in later requests (e.g. newly-created resource identifiers, tokens and more). 

Save an value from JSON to use in a subsequent request

var data = JSON.parse(response.body);
variables.set("customerId", data.customers[0].id);

// retrieve value
var customerId = variables.get("customerId");

Included Libraries

Your script code has access to a variety of 3rd-party JavaScript libraries like Underscore.js and Moment.js to make common tasks easier. Here's our first example from above rewritten to take advantage of Underscore's some function:

var data = JSON.parse(response.body);
var exists = _.some(data.customers, function(customer) { return === 123; });
assert.ok(exists, "Customer 123 exists");

Initial Script

An additional script can be written to run before the first request in the test, but after Initial Variables are processed. This is convenient for generating hashes or signatures that are used throughout the test run. Create this script under 'Initial Variables & Scripts' in the test editor. Learn more.

What kind of scripts will you write?

Scripts have been available in preview over the past few months, but today we're graduating them to generally available for all customers. Check out the Scripts docs to learn more about what you can do with this powerful new feature. 

New to Runscope? Sign up for your free account to start monitoring your APIs and app backend services with Runscope Radar today.

Photo by liz west

Categories: announcements, product, testing

Everything is going to be 200 OK®