Tests and Script
Script
Javascript Reference

JavaScript API Reference

Here is the complete set of API reference for the scripting feature in Bruno.

Request

This req variable is available inside your scripting and testing context.

Below is the API documentation for the methods available on req

getUrl

Get the current request url

Example:

let url = req.getUrl();

setUrl

Set the current request url

Example:

req.setUrl("https://api.github.com/search/repositories?q=vue");

getMethod

Get the current request method

Example:

const method = req.getMethod();

setMethod

Set the current request method

Example:

req.setMethod("POST");

getHeader

Get the request header by name

Example:

req.getHeader("transaction-id");

getHeaders

Get the current request headers

Example:

const headers = req.getHeaders();

setHeader

Set the request header by name

Example:

req.setHeader( "content-type", "application/json");

setHeaders

Set the current request headers

Example:

req.setHeaders({
  "content-type": "application/json",
  "transaction-id": "foobar"
});

getBody

Get the current request body/payload

Example:

const body = req.getBody();

setBody

Set the request body/payload

Example:

req.setBody({
  "username": "john nash",
  "password": "governingdynamics"
});

setMaxRedirects

Set the maximum number of redirects to follow

Example:

req.setMaxRedirects(5);

Response

This res variable is available inside your scripting and testing context.

Below are the properties available on the res object.

PropertyDescription
statusThe response status code
statusTextThe response status text
headersThe response headers
bodyThe response body
responseTimeThe API response time

Below are the methods available on the res object.

getStatus

Get the response status

Example:

let status = res.getStatus();

getHeader

Get the response header by name

Example:

let transactionId = res.getHeader("transaction-id");

getHeaders

Get the response headers

Example:

let headers = res.getHeaders();

getBody

Get the response data

Example:

let data = res.getBody();

getResponseTime

Get the response time

Example:

let responseTime = res.getResponseTime();

Bru

The bru variable is available inside your scripting and testing context. It exposes methods that allow you to interact with, e.g., process variables, environment variables and collection variables.

Below is the API documentation for the methods available on bru

Helpers

sleep

Pauses execution for the specified duration. This is useful for introducing delays or waiting for a specific amount of time before proceeding with the next operation.

Example:

await bru.sleep(3000);

disableParsingResponseJson

To prevent the automatic parsing of the JSON response body and work directly with the raw data, you can use the expression below in the pre-request script of the request.

Example:

bru.disableParsingResponseJson();

Node process environment

Bruno allows you to get Node process environment variables on the fly.

getProcessEnv

Get the Node process environment variable. This allows secret token usage without committing secrets to version control.

Example:

let secret_token = bru.getProcessEnv("secret_access_token");

Environments

Bruno allows you to get and set Bruno environment variables on the fly.

getEnvVar

Get the Bruno environment variable

Example:

let token = bru.getEnvVar("access_token");

setEnvVar

Set the Bruno environment variable

Example:

function onResponse(res) {
let data = res.getBody();
let token = bru.setEnvVar("access_token", data.token);
}

Collection Variables

Bruno allows you to get collection variables on the fly.

getCollectionVar

Get the collection variable

Example:

let namespace = bru.getCollectionVar("namespace");

Folder Variables

Bruno allows you to get folder variables on the fly.

getFolderVar

Get the folder variable

Example:

let company = bru.getFolderVar("company");

Runtime Variables

Bruno allows you to get, set and delete runtime variables on the fly.

getVar

Get the runtime variable

Example:

let petId = bru.getVar("petId");

setVar

Set the runtime variable

Example:

let data = res.getBody();
bru.setVar("petId", data.id);

deleteVar

Delete the runtime variable

Example:

bru.deleteVar("petId");

Request Order

You can influence the order in which requests are being run by the request-runner (UI) or the CLI.

setNextRequest

By default, the collection runner (UI) and the CLI run requests in order. You can change the order by calling setNextRequest with the name of the next request to be run. This works only in a post-request script or test-script.

Example:

bru.setNextRequest("Get process status");

You can also abort the run by explicitly setting the next request to null Example:

bru.setNextRequest(null);  // aborts the run gracefully

runRequest

You can execute any request in the collection and retrieve the response directly within the script.

Syntax:

const requestResponse = await bru.runRequest('absolute/path/to/a/request/from/collection/root');

Example:

const requestResponse = await bru.runRequest('echo/echo json');

Collection Runner Utility Functions

setNextRequest

By default, the collection runner (UI) and CLI execute requests in sequential order. You can alter this order by invoking bru.runner.setNextRequest with the name of the next request to execute. This function is applicable only within post-request scripts or test scripts.

Example:

bru.runner.setNextRequest("Get process status");

skipRequest

To skip the execution of the current request, use bru.runner.skipRequest() in the pre-request script section. This function is valid only within the context of a collection run.

Example:

bru.runner.skipRequest();

stopExecution

You can terminate the collection run by using bru.runner.stopExecution() in the pre-request scripts, post-request scripts, or the test scripts. This function is effective only within the context of a collection run.

Example:

bru.runner.stopExecution();

Test Utility Functions

getTestResults

Obtain the test results of a request by calling bru.getTestResults within test scripts.

Example:

const testResults = await bru.getTestResults();

getAssertionResults

Obtain the assertion results of a request by calling bru.getAssertionResults withiin test scripts.

Example:

const assertionResults = await bru.getAssertionResults();