Execute custom JS
Execute custom JavaScript or TypeScript code in isolation during your test flow.
The execute javascript
interaction step allows you to execute arbitrary JavaScript or TypeScript code as part of your test flow. This is useful for performing custom logic, calculations, or interacting with APIs that are not directly accessible through other test steps.
'execute javascript' interaction option in a test step, 05/2025
Isolation and limitations
- No direct access to Playwright or browser context: Your script cannot interact with page elements, cookies, or the DOM directly.
- Resource limits:
- Runtime: Scripts are limited in execution time to prevent infinite loops and slow tests.
- Memory: Scripts have a capped memory allocation.
- Network: Outbound network requests may be restricted or rate-limited.
Setting dynamic variables
You can create dynamic variables by printing a JSON object to stdout
using console.log
. These variables can be used in subsequent steps of your test.
For example, if your script creates a new document in your application and receives a document ID in the response, you can expose this ID as a dynamic variable:
After this script runs, you can reference the dynamic variable in later steps using $$documentId
.
Example: Exporting a dynamic variable from a JavaScript step
Best practices
- Keep scripts short and efficient to avoid hitting resource limits.
- Validate outputs before logging dynamic variables to prevent errors in later steps.
- Avoid sensitive operations since scripts run in an isolated environment with limited permissions.
- Use dynamic variables to pass data between steps and make your tests more flexible.
By leveraging the execute javascript
step, you can extend your tests with custom logic while maintaining security and reproducibility.