Value Functions
ReAPI allows you to extend the no-code editor by registering Value Functions. These functions appear as options in the no-code editor’s context operations, enabling QA teams to dynamically generate and transform values during test execution.
Why Value Functions Are Essential
When running API tests, static data isn’t enough. Tests often need dynamic values to reflect real-world scenarios, such as generating timestamps, random user data, or transforming response data on the fly. Without value functions, QA teams face significant challenges:
- Data Inconsistencies: Hardcoded values can lead to inaccurate test results that don’t reflect real-world conditions.
- Complex Assertions: Deeply nested JSON responses are difficult to validate without proper data transformation.
- Limited Test Scalability: Static test data can’t adapt to different environments and payload structures.
Value functions solve these challenges by dynamically generating and transforming data during test execution, making tests more adaptable, readable, and maintainable.
What Value Functions Can Do
-
Generate Dynamic Test Data:
- Create timestamps in specific formats (e.g.,
getTimestamp('ISO')
→2024-01-01T12:00:00Z
) - Generate random user emails (e.g.,
generateEmail()
→user123@example.com
) - Produce sequential order IDs (e.g.,
generateOrderID()
→ORD-1001
)
- Create timestamps in specific formats (e.g.,
-
Transform Response Data to Improve Assertions:
- Extract fields from deeply nested JSON responses (e.g.,
extractValue(response, 'data.user.address.zip')
) - Convert date formats to ensure compatibility (e.g.,
convertDate(response.date, 'UNIX')
) - Aggregate values for complex calculations (e.g.,
sumTotal(response.items)
)
- Extract fields from deeply nested JSON responses (e.g.,
-
Enhance Context Operations for Better Workflow:
- Chain API calls using transformed values to validate entire workflows
- Prepare request payloads with transformed data for subsequent calls
- Maintain test state across multiple steps without manual intervention
With value functions, both developers and QA teams can create flexible, reusable test components without needing to modify test cases manually.
Rules for Value Functions
- Single Function Requirement: Each value function must be a standalone function created within the ReAPI web editor, ensuring easy management and clear function boundaries.
- Parameter Handling: Value functions can accept:
- No parameters for simple value generation.
- A single parameter, which must be JSON-compatible:
- Primitive types: string, number, boolean
- Complex types: object, array
- Native JavaScript types (like
Date
,RegExp
,Function
) are not supported as parameters
- Mandatory Return Value: Functions must return the generated value. If no value is returned,
undefined
will be assigned, potentially causing unexpected issues in test execution. - Asynchronous Support: Async functions are fully supported. Always use
await
when dealing with asynchronous calls to ensure smooth execution and prevent potential race conditions.
Because parameters are passed from the no-code editor where only JSON data is available, all function parameters must be JSON-compatible. If you need to work with dates, receive them as ISO strings or timestamps and convert them within your function.
Designing Function Parameters
When designing value functions, it’s important to ensure flexibility and compatibility across different use cases.
-
Parameter Flexibility: Value functions can accept either:
- No parameters, useful for simple generators.
- A single parameter, which can be a primitive type (string, number, etc.) or an object for more complex use cases.
-
Design for Compatibility: Design parameters to accommodate different input styles, ensuring ease of use across various test cases.
-
Pattern for Handling Different Parameter Types:
Here’s an example pattern to handle cases where the function may receive no parameter, a simple value, or an object:
function processInput(options) { // Check if the parameter is a primitive type (string, boolean, or number) and convert it to an object // Change 'type' to your expected key based on function requirements if (typeof options === 'string') { options = { type: options }; } // Handle cases where options is undefined options = options || { type: 'default' }; // Process the value based on type // ... handle options. return result; }
This pattern ensures compatibility with different input formats and improves usability.
Example: Generating Formatted Timestamps
In this example, we’ll create a reusable timestamp generator and register it as a value function.
Create Value Function
Now, create a value function item named getTimestamp
in the UI, then
write the function to call the reusable logic and return the transformed value.
- Name:
getTimestamp
- Description: “Generates a timestamp in specified format.”
function getTimestamp(options) {
// Handle different parameter types
if (typeof options === 'string') {
options = { format: options };
}
options = options || { format: 'ISO' };
const date = new Date();
let result;
switch (options.format) {
case 'ISO':
result = date.toISOString();
break;
case 'UNIX':
result = Math.floor(date.getTime() / 1000);
break;
default:
result = date.toISOString();
}
return result;
}
Test the Value Function
// Test different parameter styles and log actual values to UI console
// Default ISO format
$log(getTimestamp()); // Logs the ISO timestamp
// Unix timestamp format using simple string parameter
$log(getTimestamp('UNIX')); // Logs the Unix timestamp
// ISO format using object parameter
$log(getTimestamp({ format: 'ISO' })); // Logs the ISO timestamp
Use in the No-Code Editor
Once the function passes the test, you can use it in the no-code editor
by enabling the function. The getTimestamp
function will appear in the
Context Operations list alongside built-in operations.
QA teams can select it to generate timestamps without writing additional code.
Registering Value Functions from External Libraries
In addition to creating value functions within the ReAPI web editor, you can also register value functions from external libraries. This allows you to leverage your preferred development environment, utilize advanced tools such as AI-assisted coding, and maintain version control.
To use external libraries:
- Write your value function in your local IDE.
- Deploy the library to a public or private repository.
- Register the function in ReAPI via the external library management interface.
For more details, refer to the External Library Guide.
Next Steps
Start creating value functions to handle your specific data transformation needs. Whether it’s generating test data, transforming responses, or maintaining test context, value functions make your tests more dynamic and maintainable.