JavaScript

Moropo uses Maestro's GraalJS engine to execute JavaScript steps inside your tests.

This provides a similar JavaScript environment to NodeJS.

Note: it's not currently possible to import node_modules

Creating a script

Add or upload a script directly from the Test Editor

Execute a script

Use the runScript command to call a script during a test run.

Using Variables

output is a special JavaScript object which is shared by each test run and can be used to store variables for use in your tests.

For example, I could set output.name inside my JavaScript file called get name:

output.name = 'billy'

In my test, I can then access this by calling runScript and then using ${} to access the variable.

For example:

IMPORTANT NOTE Moropo does not currently persist JavaScript data between 'plays' in the Test Editor If a step requires a data set by JS, you must also select the dependent step when playing that step.

For example, in the below case step 3 requires a variable set by step 2. Every time I wish to play step 3, I must also select step 2.

Making HTTP Requests

You can make HTTP requests using the built-in library.

For example, to make a simple GET request, filter the data, and save it to a variable:

const response = http.get("https://jsonplaceholder.typicode.com/users");

output.name = json(response.body).find(user => user.id === 2).name

HTTP request methods

  • http.get("https://myendpoint.com")

  • http.post("https://myendpoint.com")

  • http.put("https://myendpoint.com")

  • http.delete("https://myendpoint.com")

Headers

Headers can be passed using the headers parameter

const response = http.get('https://myendpoint.com', {
    headers: {
        Authorization: 'Bearer myToken'
    }
})

JSON parsing

Use the json() built-in helper function to parse JSON responses.

const response = http.get("https://jsonplaceholder.typicode.com/users");
const users = json(response.body);
// users = [
//   { id: 1, name: "Clive" },
//   { id: 2, name: "Barbie" }
// ]

output.name = users.find(user => user.id === 2).name;
// output.name = Barbie

Last updated