ReAPI Exposed API
In your script, you can use ReAPI’s exposed APIs to integrate your custom logic seamlessly with the platform. These APIs provide utilities and objects to interact with test data, requests, responses, and logging.
1. Globally Available APIs
-
_
(Lodash): The Lodash library is built-in and available globally, providing a wide range of utility functions.
Example:const sortedArray = _.sortBy([3, 1, 2]);
-
$log
: An alternative toconsole.log
, this logs output in the ReAPI web UI, making it easier to debug directly in the platform.
Example:$log("Debugging log message");
2. APIs for Hooks
Before Request Hook (beforeRequest
)
$context
: Shared context variables accessible throughout the test flow.$request
: The current request object, which can be modified.
Example:
async function beforeRequest() {
$request.headers["Authorization"] = `Bearer ${$context.variables.authToken}`;
}
After Response Hook (afterResponse
)
$context
: Shared context variables accessible throughout the test flow.$response
: The current response object, which can be validated or transformed.$request
(Read-Only): The original request object for reference.$addAssertionResult
: Add custom assertions dynamically.
Example:
async function afterResponse() {
if ($response.status === 200) {
$addAssertionResult({ passed: true, message: "Response status is 200." });
} else {
$addAssertionResult({
passed: false,
message: `Unexpected status: ${$response.status}`,
});
}
}
3. Custom Assertion Functions
$addAssertionResult
: Add pass/fail results for custom validations.
Example:
async function isValidUUID(value) {
const uuidRegex =
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
if (uuidRegex.test(value)) {
$addAssertionResult({ passed: true, message: "Valid UUID." });
} else {
$addAssertionResult({ passed: false, message: "Invalid UUID." });
}
}
4. Reference Data Structures
$request
Object Structure
interface RequestData {
baseUrl?: string; // Optional base URL for the request
path: string; // Request path (absolute or relative to baseUrl)
method: string; // HTTP method (GET, POST, etc.)
query: Record<string, any>; // URL query parameters
headers: Record<string, string>; // HTTP request headers
body?: any; // Optional request body
withCredentials?: boolean; // Optional flag for cross-site credentials
timeout?: number; // Optional request timeout in milliseconds
}
$response
Object Structure
interface ResponseData {
status: number; // HTTP status code
statusText?: string; // Optional status text
headers: Record<string, any>; // Response headers
data?: any; // Optional response body
time: number; // Request duration in milliseconds
size?: number; // Response size in bytes
startedAt: number; // Request start timestamp
completedAt: number; // Response completion timestamp
}
AssertionResult
Structure
interface AssertionResult {
passed: boolean; // Whether the assertion passed
leftValue?: any; // The actual value obtained during the test
rightValue?: any; // The expected value to compare against
options?: any; // Additional options for the assertion
message?: string; // Description of the assertion result
meta?: any; // Optional metadata related to the assertion
}
Example usage:
$addAssertionResult({
passed: false,
leftValue: response.body.userId,
message: "User ID validation failed",
meta: { validationType: "uuid" }
});
ReAPI Exposed API Table
API | Description | Available In | Example |
---|---|---|---|
_ | Lodash utility library for operations like sorting, filtering, and more. | Globally | _.sortBy([3, 1, 2]) |
$log | Logs messages in the ReAPI web UI, useful for debugging scripts. | Globally | $log("Debugging message"); |
$context | Shared context variables accessible across the test flow. | Before & After Hooks | $context.variables.authToken |
$request | The request object; modifiable in beforeRequest , read-only in afterResponse . | Before & After Hooks | $request.headers['Authorization'] = 'Bearer token'; |
$response | The response object for validation or transformation. | After Hooks | $response.body.data |
$addAssertionResult | Adds a custom assertion result (pass/fail). | After Hooks, Custom Assertions | $addAssertionResult({ passed: true, message: "Valid UUID." }); |
$setGeneratedValue | Sets a generated or transformed value for later use in the test flow. | Value Functions | $setGeneratedValue("user@example.com"); |
These APIs provide a robust set of tools for scripting in ReAPI, empowering you to build dynamic, reusable, and scalable test logic. Let me know if you’d like more details or examples!