# 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="https://1849561984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNrvYDqka9qiakmPqT4am%2Fuploads%2FugAf8NFEREyRctn4ERtw%2FScreenshot%202024-05-15%20at%2016.21.38.png?alt=media&#x26;token=6e885439-9b77-41d5-81de-5d08a60331a0" alt="" width="287"><figcaption></figcaption></figure>

### **Execute a script**

Use the [runScript command ](https://docs.moropo.com/creating-tests/commands/runflow-1)to call a script during a test run.

<figure><img src="https://1849561984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNrvYDqka9qiakmPqT4am%2Fuploads%2FsNnnFx7iL7KUEzGsa9PO%2FScreenshot%202024-05-15%20at%2016.23.33.png?alt=media&#x26;token=2d894900-9a80-45fb-855a-796e68c1cf13" 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="https://1849561984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNrvYDqka9qiakmPqT4am%2Fuploads%2FlFrtjphyc6T96V5cywS7%2FScreenshot%202024-05-15%20at%2016.31.44.png?alt=media&#x26;token=c56849f2-95fe-4074-a8bc-62ab999af778" 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="https://1849561984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNrvYDqka9qiakmPqT4am%2Fuploads%2FlFrtjphyc6T96V5cywS7%2FScreenshot%202024-05-15%20at%2016.31.44.png?alt=media&#x26;token=c56849f2-95fe-4074-a8bc-62ab999af778" 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
```
