- The jc Object
- Using Variables in Scripts
- Scripting with request and response data
- Sending Requests from Scripts
- Running Requests, Folders, and Test Suites from Scripts
- Writing Test Assertions
- Using External Libraries
The majority of the functionality within the JetClient JavaScript API is accessed through the jc.*
object. This object provides access to request and response data, variables, and more. For detailed type definitions, see JetClient Library Types.
Use the jc.variables
methods to access and manage variables at different scopes and setting runtime variables.
To check if a variable exists in the current scope:
jc.variables.has("variableName")
To get the value of a variable by its name:
jc.variables.get("variableName")
To set a runtime variable:
jc.variables.set("variableName", "variableValue")
To replace all variables in a string with their actual values:
jc.variables.replaceIn("Hi, my name is {{myName}}")
Use the jc.globals
methods to access and manipulate global variables that are available regardless of the selected environment.
To verify if a global variable exists:
jc.globals.has("variableName")
To get the value of a global variable:
jc.globals.get("variableName")
To replace all variables in a string with their actual values:
jc.globals.replaceIn("Welcome, {{userName}}")
To set a global variable:
jc.globals.set("variableName", "variableValue")
To delete a global variable:
jc.globals.unset("variableName")
To clear all global variables:
jc.globals.clear()
Use the jc.environment
methods to access and manage variables within a specific environment. If no environment group is specified, the Default group is used.
To check if a variable exists in the active environment:
jc.environment.has("variableName", "envGroup")
To get the value of an environment variable:
jc.environment.get("variableName", "envGroup")
To replace all environment variables in a string with their actual values:
jc.environment.replaceIn("Server URL is {{baseUrl}}", "envGroup")
To set or update a variable in the active environment:
jc.environment.set("variableName", "variableValue", "envGroup")
To remove a variable from the active environment:
jc.environment.unset("variableName", "envGroup")
To clear all variables in the active environment:
jc.environment.clear("envGroup")
Utilize the jc.folderVariables
methods to access and manipulate folder variables in your scripts.
To verify if a folder contains a specific variable:
jc.folderVariables.has("variableName")
To get the value of a variable:
jc.folderVariables.get("variableName")
To replace all variables in a string with their actual values:
jc.folderVariables.replaceIn("Hi, my name is {{myName}}")
To set a local folder variable:
jc.folderVariables.set("variableName", "variableValue")
To set a variable within the active environment of a specific environment group in the folder (defaults to the 'Default' environment group if none specified):
jc.folderVariables.setEnv("variableName", "variableValue", "envGroup");
To delete a specific folder variable:
jc.folderVariables.unset("variableName")
To delete a variable from the active environment of a specific environment group in the folder (defaults to the 'Default' environment group if no group is specified):
jc.folderVariables.unsetEnv("variableName", "envGroup");
To clear all local variables in the folder:
jc.folderVariables.clear()
jc.request
and jc.response
objects provide access to request and response data.
The jc.request
object provides access to the data for the request the script is running within.
For a Pre-request Script this is the request that's about to run, and for a Test script this is the request that has already run.
You can use the jc.request
object in pre-request scripts to alter various parts of the request configuration before it runs.
The jc.request
object provides multiple methods for accessing and manipulating request data.
See JetClient Library Types for type definitions.
The jc.response
object provides access to the data returned in the response for the current request in scripts added to the Tests.
The jc.response
object provides the following properties and methods:
The response status code:
jc.response.code
The status text string:
jc.response.status
The response headers:
jc.response.headers
The time the response took to receive in milliseconds:
jc.response.responseTime
The size of the response received:
jc.response.responseSize
The response text:
jc.response.text()
The response body as a JSON object:
jc.response.json()
The jc.sendRequest
method allows you to send requests asynchronously from Pre-request or Test scripts. This method returns a promise that resolves to the response object.
Create a request object with the HttpRequest
builder and pass it to jc.sendRequest
:
jc.sendRequest(new HttpRequest()
.setUrl("https://httpbin.org/anything")
.setMethod("GET"))
.then((response) => {
console.log(response.json());
});
Alternatively, create a request using a JavaScript object:
jc.sendRequest({
url: "https://httpbin.org/anything",
method: "GET"
})
.then((response) => {
console.log(response.json());
});
You can also utilize an existing request object retrieved with jc.findRequest
:
const response = await jc.sendRequest(
jc.findRequest("/folderName/requestName")
.addQueryParam('param', 'paramValue')
.setBodyJson({key: 'value'}));
console.log(response.json());
jc.sendRequest
returns a promise that resolves to the response object. Handle the response synchronously with the await
keyword or asynchronously with the then
and catch
methods.
When using the jc.sendRequest
method, associated Pre-request and Test scripts are not executed. To ensure these scripts are run, use the jc.runRequest
method.
jc.runRequest
is used to execute requests along with their associated Pre-request and Test scripts, as well as those of their parent folders.
Use jc.runFolder
to execute all requests within a specified folder and its subfolders, including their respective scripts:
await jc.runFolder("/parentFolder/childFolder")
Similarly, jc.runTestSuite
allows you to execute test suites:
await jc.runTestSuite("/folder/TestSuiteName")
Use jc.test
to write test specifications within Pre-request or Test scripts. Each test should include a name and assertion(s). JetClient will then output the test results as part of the response.
jc.test("response should be okay to process", () => {
jc.response.to.not.be.error
jc.response.to.have.jsonBody()
jc.response.to.not.have.jsonBody('error')
})
The jc.expect
method allows you to write more detailed assertions on your response data, utilizing the ChaiJS expect BDD syntax.
jc.expect(jc.response.code).to.be.eq(200)
You can also use jc.response.to.have.*
and jc.response.to.be.*
to build your assertions.
The require
function enables the use of external libraries in your scripts. JetClient comes with built-in libraries such as ajv
, atob
, btoa
, chai
, cheerio
, crypto-js
, csv-parse/lib/sync
, lodash
, moment
, tv4
, uuid
, and xml2js
, all of which you can import using require
.
Additionally, you can add your own libraries to the scripts. Navigate to Settings > Tools > JetClient
and set the Libraries directory
. If you are using npm libraries, specify the directory containing your package.json
and node_modules
. Otherwise, set the directory where your script libraries are located. You can then use require
to import your libraries.