# JavaScript

Moropo uses Maestro's GraalJS engine to execute JavaScript steps inside your tests.&#x20;

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

<figure><img src="/files/stgySaHchgiVl9JS1RlQ" alt="" width="287"><figcaption></figcaption></figure>

### **Execute a script**

Use the [runScript command ](/creating-tests/commands/runflow-1.md)to call a script during a test run.

<figure><img src="/files/lgMqHElfNEK03jwsFE9n" alt="" width="375"><figcaption></figcaption></figure>

### 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**:

```javascript
output.name = 'billy'
```

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

For example:

<figure><img src="/files/d5YFjE3TQ5zm6xyigiMd" alt="" width="563"><figcaption></figcaption></figure>

{% hint style="warning" %}
**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.<br>

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.<br>
{% endhint %}

<div data-full-width="true"><figure><img src="/files/d5YFjE3TQ5zm6xyigiMd" alt="" width="563"><figcaption></figcaption></figure></div>

## Making HTTP Requests

You can make HTTP requests using the built-in library.&#x20;

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 <a href="#headers" id="headers"></a>

Headers can be passed using the  `headers` parameter

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

### JSON parsing <a href="#json" id="json"></a>

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

```javascript
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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.moropo.com/creating-tests/advanced-use-cases/javascript.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
