DEV Community

Cover image for Introduction to Bruno Scripting
Vikas Yadav
Vikas Yadav

Posted on

Introduction to Bruno Scripting

In the dynamic world of API development and testing, automation is key.

Bruno offers scripting support to help you add additional functionality to the tool such as data generation, validation, and integration with other tools and systems, including sending intermediate requests, parsing response data, updating environment variables, etc.

Secrets Management

Problem Statement

In any collection, there are secrets that need to be managed. These secrets can be anything such as API keys, passwords, or tokens.

A common practice is to store these secrets in environment variables.

There are two ways in which developers share Bruno collections:

  • Check in the collection folder to source control (like git)
  • Export the collection to a file and share it

In both these cases, we want to ensure that the secrets are stripped out of the collection before it is shared.

Solution

Bruno offers two approaches to manage secrets in collections:

  1. DotEnv File
  2. Secret Variables

Core Features of Bruno Scripting

Bruno is a scripting framework built to providing advanced functionality and automation capabilities. With Bruno, developers can elevate their API testing and development workflows by enabling tasks such as data generation, validation processes, integration with other tools and systems, and much more.

Let's explore some of the core features and functionalities that Bruno brings to the table:

1. Data Generation and Validation

Bruno allows for seamless data generation within the testing environment, enabling developers to simulate various scenarios and edge cases. Furthermore, it facilitates robust validation processes, ensuring that APIs behave as expected under different conditions.

2. Integration and Interoperability

One of Bruno's strengths lies in its ability to integrate with other tools and systems seamlessly. Whether you're orchestrating complex workflows or incorporating third-party services, Bruno streamlines the process, enhancing collaboration and efficiency.

3. Intermediate Requests and Response Parsing

With Bruno, developers can execute intermediate requests within their workflows, enabling sophisticated test scenarios and intricate API interactions. Moreover, Bruno simplifies the parsing of response data, making it easier to extract and manipulate relevant information.

4. Environment Management

Bruno empowers developers to manage environment variables effortlessly, facilitating configuration management and ensuring consistency across different testing environments. Whether it's updating variables or accessing environment-specific data, Bruno simplifies the process, enhancing workflow efficiency.

Translating Postman Scripts to Bruno Commands

To illustrate Bruno's capabilities further, let's translate some common Postman scripts into Bruno commands:

Description Bruno Command Postman Command
Tests confirm API is working bru.test("check status code", function() { expect(res.status).to.equal(200); }); pm.test("check status code", function() { pm.response.to.have.status(200); });
Set Collection Variable bru.setVar("auth_token", res.body.userid); pm.collectionVariables.set("auth_token", "abc123");
Get Collection Variable bru.getVar("auth_token"); pm.collectionVariables.get("auth_token");
Set Environment Variable bru.setEnvVar("api_key", "xyz1712"); pm.environment.set("api_key", "xyz1712");
Get Environment Variable bru.getEnvVar("api_key"); pm.environment.get("api_key");
Order Next Request bru.setNextRequest("Token_SSO"); pm.setNextRequest("Token_rock");

Conclusion

In conclusion, Bruno enables advanced automation, customization, and flexibility, helping developers deliver high-quality APIs with confidence.

Bruno supports loading any npm module to use in your scripting workflows.

You need to add a package.json file where your collection is stored.


json
{
  "name": "bruno_api_demo",
  "version": "1.0.0",
  "description": "With Bruno CLI, you can now run your API collections with ease using simple command line commands.",
  "author": "vikas.yadav",
  "license": "ISC",
  "keywords": [
    "bruno",
    "api testing"
  ],
  "scripts": {
    "test": "bru run Oauth-Login --env test_vikas --format junit --output results.xml"
  },
  "dependencies": {
    "@usebruno/cli": "~1.11.0",
    "@faker-js/faker": "8.3.1"
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)