In a mabl API test, custom scripts are composed of one or more API snippets. API snippets can be one-time or reusable, giving you the flexibility to manage scripts across tests and update when needed.
This article explains how to add request-level and test-level scripts to an API test.
Test size limits
API tests cannot exceed 1MB in size. Use caution when writing large snippets.
Request-level scripts
Request-level scripts apply to an individual request. You can add two types of request-level scripts:
- Pre-request scripts execute code before a request is made.
- Post-request scripts execute code after a response is returned.
Pre-request
To add a pre-request script to a specific request, open the API Test Editor and take the following steps:
- Click on the test step you’d like to edit on the left-side panel.
- In the Request details section, select the Pre-request tab.
- Click on the + Add snippet button
- Choose an existing reusable snippet or write a new snippet.
Post-request
To add a post-request script to a specific request, open the API Test Editor and take the following steps:
- Click on the step you’d like to edit in the left-side panel.
- Expand the Validation and variable assignment section.
- Select the Post-request tab.
- Click on the + Add snippet button
- Choose an existing reusable snippet or write a new snippet.
Test-level scripts
Test-level scripts apply to every request in the API test. You can add two types of test-level scripts:
- Test-level pre-request scripts execute before every request in an API test.
- Test-level post-request scripts execute after every response in an API test.
To add a test-level script, take the following steps:
- Click on the “Edit test properties” pencil in the top-left corner.
- In the edit test modal, select the Pre-request or Post-request tab.
- Click on the + Add snippet button
- Choose an existing reusable snippet or write a new snippet.