Contents ▼

Code Blocks

If you need more control than you can get from the graphical scripting interface, throw in a freestyle code block! These code blocks are similar to the JavaScript Validation Rules and Capturing Rules, except that they can exist independently of any other step, and don’t have to be run on a response.

Code blocks give you more power than ordinary graphical scripting by allowing looping, conditionals, etc.

To add a code block to your script, just click Add Code Block.

Objects

Beyond all the usual JavaScript language constructs, Loadster code blocks expose a few important objects specific to Loadster that you can use in your scripts.

User

The user object is global within the context of a single v-user. It represents the v-user currently executing the code block. This user object exposes the following methods:

// All the standard HTTP methods
user.get(url, args);
user.post(url, body, args);
user.put(url, body, args);
user.patch(url, body, args);
user.delete(url, args);
user.options(url, args);
user.trace(url, args);

// Get and set user variables
user.getVariable(name);
user.setVariable(name, value);

// Wait (these are synonymous)
user.wait(milliseconds);
user.sleep(milliseconds);

It’s important to note that these methods are all synchronous, so there is no need to do a promise chain or callbacks or anything like that. The actual processing is done behind the scenes by Loadster’s v-user anyway, so synchronous programming is not the bad practice it might seem to be if you come from a background in ordinary JavaScript development.

Console

Code blocks also expose a simple console for logging:

console.log(message);

Messages written to the console show up in the script logs and aid in debugging the script.

JSON

If you’re testing APIs, you’ll often need to parse JSON to look at specific properties. You can use the ordinary JSON.parse for this.

var object = JSON.parse(json);

XML

Since XML parsing isn’t a standard language feature of JavaScript, Loadster includes the xmldoc parser. There are docs available on GitHub, but here’s a quick example:

var xml = XML.parse(response.bodyAsString);
var users = xml.childrenNamed("user");

Examples

An example’s worth almost as many words as a picture, so here are a few examples of things you can do in code blocks.

Normal HTTP Stuff

// GET
user.get("https://slothereum.cc");

// GET with additional page resources
user.get("https://slothereum.cc", {
    resources: [
        "/style.css",
        "/favicon.ico"
    ]
});

// POST with a JSON body and validator function that requires an HTTP 201 status
user.post(
    "https://api.slothereum.cc/transactions", 
    {
        source: "08c2d5",
        destination: "9a6bce",
        "amount": 1.05
    }, 
    {
        headers: [
            { "Content-Type": "application/json" }
        ]
    }
);

// POST with a form body and validator function that captures a variable
user.post(
    "https://admin.slothereum.cc/login", 
    "username=sloth&password=chunk", 
    {
        headers: [
            { "Content-Type": "application/x-www-form-urlencoded" }
        ]
    }
);

// DELETE
user.delete("https://api.slothereum.cc/receipts/1133423");

Validation & Capturing

// A POST with a JSON body and validator function that requires an HTTP 201 status
user.post(
    "https://api.slothereum.cc/transactions", 
    {
        source: "08c2d5",
        destination: "9a6bce",
        "amount": 1.05
    }, 
    {
        headers: [
            { "Content-Type": "application/json" }
        ],
        validators: [
            function (response) {
                return response.status == 201;
            }
        ]
    }
);


// A POST with a form body and validator function that captures a variable
user.post(
    "https://admin.slothereum.cc/login", 
    "username=sloth&password=chunk", {
    headers: [
        { "Content-Type": "application/x-www-form-urlencoded" }
    ],
    validators: [
        function (response) {
            var body = response.bodyAsString;
            var regex = /Welcome, (\S+)!/;
            var name = body.match(regex)[1];
            
            // If a name was found, store it in a variable
            if (name.length > 1) {
                user.setVariable("name", name);
                        
                return true;
            } else {
                return false;
            }
        }
    ]
});

Loops & Conditionals

console.log("We're in a nonsensical code step.");
console.log("It exists only to demonstrates random numbers, user vars, and conditionals.");

// Set a user variable to a random integer from 0 to 19
user.setVariable("random", Math.floor(Math.random() * 20));

// Loop and make 20 requests, with a random value and random wait times
for (var i = 0; i < 20; i++) {
    if (i % 2 == 0) {
        user.get("https://example.com/evens/${random}");
    } else {
        user.get("https://example.com/odds/${random}");
    }
    
    user.wait(Math.random() * 2000);
}

Limitations

At this time, Loadster’s JavaScript interpreter only supports traditional JavaScript syntax (var, function) and does not recognize most of the newer extensions like arrow functions, etc. That said, it’s a fast and simple way to get things done without being overly fancy.

Also, keep in mind that code blocks aren’t actually running in your browser, so be careful not to confuse them with on-page JavaScript (jQuery, React, etc).

Getting Help

We realize that Loadster’s JavaScript implementation is a bit non-standard, and we’re happy to help. If you get stuck, please contact support@loadster.app and we’ll do our best to assist.