Scripts
Write pre-request scripts and build tests.
Hoppscotch provides ECMAScript APIs that can be used in writing pre-request scripts and building tests. You can enter your ECMAScript code and run the necessary scripts.
The pw
object
The pw
object provides access to request and response data and variables in your Hoppscotch instance.
The pw
object houses the following methods:
pw.env.set("variable", "value")
pw.env.set()
can be used directly for quick and convenient environment variable definition.
Here are some practical examples that show how you can use pw.env.set()
to encode and decode strings with Base64:
1. pw.env.set("variable", atob("value"))
Use the atob()
function to decode a Base64 encoded string and set it as an environment variable.
2. pw.env.set("variable", btoa("value"))
Use the btoa()
function to encode a regular string into Base64 and set it as an environment variable.
pw.env.unset("variable)
pw.env.unset()
can be used to remove the value of the variable present in the current active environment
pw.env.get("variable")
Retrieves the value of the selected environment’s variable. Accepts an environment variable as an argument.
pw.env.getResolve("variable")
Retrieves the value of the selected environment’s variable recursively. Accepts an environment variable as an argument.
pw.env.resolve("variable")
Retrieves the value of the selected environment’s variable recursively. Accepts an environment variable string as an argument.
pw.expect(value)
The expect method returns an expectation object, on which you can call matcher functions.
The example below calls the matcher function toBe
on the expectation object that is returned by calling pw.expect
with the response id, pw.response.body.id
as an argument.
Use pw.expect
directly for quick and convenient testing. Every pw.expect
statement will generate a line on the test report.
pw.test(name, function)
To create a group of tests, with the name as a string and fn as a callback function to write tests associated with the group. The test results will include the given name for better organization.
Let’s wrap expect statements with pw.test
to the group and describe related statements.
If neither a pw.expect
nor a pw.test
statement is present, no test reports will be generated.
pw.toBe(value)
Test for exact equality using toBe
.
toBe
uses strict equality and is recommended for primitive data types.
pw.not()
Test for negation by adding .not
before calling the matcher function.
pw.toBeLevelxxx()
There are four different matcher functions for quick and convenient testing of the http status code that is returned:
toBeLevel2xx()
toBeLevel3xx()
toBeLevel4xx()
toBeLevel5xx()
For example, an argument passed to expect must be within 200
and 299
inclusive to pass toBeLevel2xx()
.
If the argument passed to expect()
is a non-numeric value, it is first parsed with parseInt()
.
pw.toBeType(type)
Use .toBeType(type)
for type checking. The argument for this method should be either of the following string
, boolean
, number
, object
, undefined
, bigint
, symbol
, or function
.
pw.toHaveLength(number)
Use .toHaveLength(number)
to check that an object has a .length
property and it is set to a certain numeric value.
pw.toInclude(value)
Use .toInclude(value)
to check that a string/array has a value entry.
pw.response
Assert response data by accessing the pw.response
object.
Currently supports the following response values:
status
: -number- The status code as an integer.headers
: -object- The response headers.body
: -object- the data in the response. In many requests, this is the JSON sent by the server.
Setting Environment Variables from API Responses
By following these steps, you can store data from one API response and access it later using Environment Variables, making it available for use in subsequent API calls.
Assume in this example that the payload returns access_token
and id_token
as part of a JSON response.
-
Create Environment Variables without setting their values initially. For example:
idToken
accessToken
-
In the “Tests” tab of the first API request (that returns the tokens), add the following code:
- Use these Environment Variables in subsequent API calls within the same collection using the
<<variableName>>
syntax as usual.