Scripts
Customise requests, automate repetitive tasks and create custom workflows using Testfully's scripting feature.
Testfully supports Javascript (ES2020) for scripting. You can write scripts for your requests, folders, and the entire collection. You have the option to run your scripts before or after a request is sent. Also, Testfully offers a range of built-in functions that you can use in your scripts to interact with the request, response, and other parts of the application.
Attention! Postman Users
Our goal is to fully support Postman's scripting syntax. You can copy and paste your Postman scripts into Testfully and they will work without any changes. Although pm
object is available, we kindly ask you to take your time and change them to $
. When importing a Postman collection, Testfully automatically brings all of your scripts.
Features
The table below shows the list of features that are available/coming in Testfully's scripting environment and their status.
Feature | Status |
---|---|
Supports ES2020, write code in modern Javascript | Available |
All native APIs including JSON.parse() etc are available | Available |
Access & Modify Request | Available |
Access Response Data | Available |
Work with global variables | Available |
Work with environment variables | Available |
Send HTTP requests | Available |
Work with folder & collection variables | Coming Soon |
Import & use npm packages | Coming Soon |
Chain requests via code | Coming Soon |
API Validation | Coming Soon |
Cook dinner, take your dog for a walk | ??? |
Supported APIs
Testfully offers a range of built-in APIs to use for scripting. You can use these APIs to interact with the request, response, and other parts of the application.
You should know!
All of the APIs are available via the $
global variable. To use Testfully API, simply type $
.
Send a request
Use the $.sendRequest()
API to send a request. This API accepts a request object, or a URL, as its first parameter and returns the response.
Basic Get Request
The example below sends a request to the https://httpbin.org/anything
endpoint and stores the response in the response
variable. The response
variable is then used to log the response body.
const response = $.sendRequest("https://httpbin.org/anything");
console.log(response);
Request Object
The example below sends a request to the https://httpbin.org/anything
endpoint and stores the response in the response
variable. The response
variable is then used to log the response body.
const request = {
method: "POST",
url: "https://httpbin.org/anything",
headers: {
"Content-Type": "application/json",
},
body: {
mode: "raw",
raw: {
name: "Testfully",
},
},
};
const response = $.sendRequest(request);
console.log(response);
Setting up a request timeout
Testfully waits for the response to be received for 30 seconds by default. You can change this timeout value by setting the timeout
property of the request object. The example below sets the timeout value to 1 second.
const request = {
method: "POST",
url: "https://httpbin.org/anything",
timeout: 1,
};
Working with requests
Use the $.request
API to work with the current request. Testfully offers the following APIs to work with the request.
API | Description |
---|---|
$.request.method | Get the HTTP method of the request. |
$.request.url | Get the URL of the request. |
$.request.type | Get the type of the request. |
$.request.headers | Get the headers of the request. |
$.request.headers.add({ key, value }) | Add a new header |
$.request.headers.upsert({ key, value }) | Upsert a header |
$.request.headers.remove(key) | Remove a header |
$.request.params | Get the params of the request. |
$.request.body | Get the body of the request. |
$.request.body.json | Get the JSON payload for the request. |
$.request.body.query | GraphQL query for the request. |
$.request.body.variables | GraphQL variables for the variables. |
$.request.body.form | Form fields for both formdata and urlencoded requests. |
Working with response
Use the $.response
API to work with the response of the current request. Testfully offers the following APIs to work with the response.
API | Description |
---|---|
$.response.code | Get the HTTP status code of the response. |
$.response.status | Get the HTTP status of the response. |
$.response.headers | Get the headers of the response. |
$.response.body | Get the body of the response. |
$.response.responseTime | Get the response time of the response. |
$.response.responseSize | Get the response size of the response. |
Logging
Use the global console
object to log messages to the console. The logs are available in the "Logs" tab of the request. Testfully supports the following logging APIs:
API | Description |
---|---|
console.log(message) | Logs a message to the console. |
console.info(message) | Logs an informational message to the console. |
console.warn(message) | Logs a warning message to the console. |
console.error(message) | Logs an error message to the console. |
Example
console.log("Hello World!");
console.warn("request failed error was ->", $.response.code);
Working with globals
Use the $.globals
API to work with global variables in your workspace.
API | Description |
---|---|
$.globals.get(name) | Get the value of a global variable. |
$.globals.has(name) | Check if a global variable exists. |
$.globals.set(name, value) | Sets value of a global variable |
$.globals.unset(name) | Unsets value of a global variable |
$.globals.clear() | Removes all of the global variables in one go |
$.globals.toObject() | Returns all of the global variables as an object |
Working with environment variables
Use the $.environment
API to work with active environment (currently selected).
API | Description |
---|---|
$.environment.get(name) | Get the value of an environment variable. |
$.environment.has(name) | Check if an environment variable exists. |
$.environment.set(name, value) | Sets value of an environment variable |
$.environment.unset(name) | Unsets value of an environment variable |
$.environment.clear() | Removes all of the environment variables in one go |
$.environment.toObject() | Returns all of the environment variables as an object |
Working with async code
If you have experience with Javascript and async code, you may noticed that Testfully examples above for any kind of async code, including sendRequest
and $.globals.set()
, are written in a synchronous way. This is because Testfully automatically handles async code for you. You can write your code in a synchronous way and Testfully will take care of the rest.