Writing Scripts
AREX allows you to add dynamic behavior to requests and collections through scripting (JavaScript
code snippets).
What is scripts used for?
- Test (assert) the correctness of the request return result (test script).
- Dynamically modifying interface request parameters, such as adding interface signature parameters, etc. (pre-request script)
- Passing data between interface requests (using scripts to manipulate variables).
You can add JavaScript code to execute during two events in the flow:
- Before a request is sent to the server, as a pre-request script under the Pre-request Script tab.
- After a response is received, as a test script under the Tests tab.
You can add pre-request and test scripts to a collection, a folder, or a single request within a collection:
- Set pre-request or test scripts for the entire collection or a folder under the collection. Once set, they will run for every request in the collection or direct child request in the folder.
- Set pre-request or test scripts for individual requests. Once set, they will run only for a single request.
Execution order of scripts
The script execution order for a single request looks like this:
- A pre-request script associated with a request will execute before the request is sent.
- A test script associated with a request will execute after the request is sent.
For every request in a collection, scripts will execute in the following order:
- A pre-request script associated with a collection will run prior to every request in the collection.
- A pre-request script associated with a folder will run prior to every direct child request in the folder.
- A test script associated with a collection will run after every request in the collection.
- A test script associated with a folder will run after every direct child request in the folder.
Writing pre-request scripts
Re-using pre-request scripts (to do)
Select Collection in the sidebar.
Select the collection or the folder you want to add Re-using pre-request scripts.
Select the Pre-request Scripts tab >> Add Script Block >> CustomScript.
Enter code that will run before every request in the collection or direct child request in the folder.
Scripting before your request runs
Open the request, then select the Pre-request Script tab.
Select Add Script Block and enter the JavaScript you need to process before the request runs, then select Save.
Select Send to send the request. The code will execute before Postman sends the request to the API.
Writing test scripts
You can use test scripts to assert
whether your API is working as expected or not, and set the result data returned by the request as environment variables, etc.
Validating responses
open the request and select the Tests tab.
Select Add Script Block and enter your code for your request.
Select Send and the tests will execute after the request runs.
The output is in the response's Result tab.
Reference
1. Using variables in scripts
Environment variables
Your scripts can use the pm.environment methods to access and manipulate variables in the active (currently selected) environment.
- Set the variable with the specified name and value in the active environment:
pm.environment.set("variable_key", "variable_value");
- Get the variable with the specified name in the active environment:
pm.environment.get("variable_key");
- Delete the variable:
pm.environment.delete("variable_key");
Temporary variables
- Set the temporary variables with the specified name and value:
pm.variables.set("variable_key", "variable_value")
- Get the temporary variables:
pm.variables.get("variable_key")
- Delete the temporary variables:
pm.variables.delete("variable_key")
2. Sending requests from scripts
- Sending GET requests
let response = await pm.sendRequest({method:"GET",url:"http://10.5.153.1:8090/api/config/schedule/useResult/appId/arex-0.2.4.test2"});
- Sending POST requests
let response = await pm.sendRequest({url:"http://10.5.153.1:8088/api/report/queryDifferences",method:"POST",data:"{"categoryName":"ServletEntrance","operationName":"/owners/{ownerId}","planItemId":"633184edc9af0157f44eaeba"}",headers:{"Content-Type":"application/json","access-token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpbmZvIjoidGVzdCJ9.YeLmUW--fqrtmag1QTDmL8U7RVZlb34xPAAxorxSCPM"}});
3. Validating responses
Test if the response status code is 200:
pm.test("Status code is 200", ()=> {
pm.expect(pm.response.status).toBe(200);
});
Test if the age in the result messages is 18:
pm.test("Check JSON response property", ()=> {
pm.expect(pm.response.body.age).toBe(18);
});